7896| 13
|
[求助] 数字震动传感器 |
本帖最后由 1973742214 于 2018-2-12 17:04 编辑 你可以将它看作一个开关 连5v的LED试试 此外有程序的话更好 沙发舒服 |
1973742214 发表于 2018-2-12 17:03 这是代码 #include<Servo.h> int sensor=2; Servo myservo; int pos=0; void setup() { Serial.begin(9600); pinMode(sensor, INPUT); myservo.attach(9); } void loop() { int sensorState = digitalRead(sensor); Serial.println(sensorState); if(!sensorState) { pos = pos +2; if(pos >=180){ pos = 180; } myservo.write(pos); Serial.println(pos); delay(100); } else { pos = pos - 2; if(pos <=0){ pos = 0; } myservo.write(pos); Serial.println(pos); delay(100); } delay(100); } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed