Arduino – Servo – Switch 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <Servo.h> Servo myservo; int pos=0; int btnLeft=4, btnRight=5; unsigned long cnt=0; void setup() { myservo.attach(3); pinMode(btnLeft, INPUT); pinMode(btnRight, INPUT); } void loop() { if (digitalRead(btnLeft) && cnt==0) { pos-=5; cnt=millis(); } else if (digitalRead(btnRight) && cnt==0) { pos+=5; cnt=millis(); } else if (millis() > cnt+200) { cnt=0; } if (pos > 180) { pos=180; } else if (pos < 0) { pos=0; } myservo.write(pos); } |