Created
November 18, 2013 09:09
-
-
Save flazer/7524888 to your computer and use it in GitHub Desktop.
Script to handle a servo via button-press on arduino-controllers:
usage-example: youtube.com/watch?v=ghyxaQc5-Xg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo myservo; | |
int pos = 0; | |
int btn = 13; | |
int maxAngle = 180; | |
int minAngle = 40; | |
int speed = 5; | |
void setup() | |
{ | |
pinMode(btn, INPUT); | |
myservo.attach(7); | |
myservo.write(maxAngle); | |
} | |
void loop() | |
{ | |
if(digitalRead(btn)){ | |
handleServo(); | |
} | |
} | |
void handleServo(){ | |
if(pos <= minAngle){ | |
for(pos = minAngle; pos < maxAngle; pos += 1){ | |
myservo.write(pos); | |
delay(speed); | |
} | |
}else{ | |
for(pos = maxAngle; pos>=minAngle; pos-=1){ | |
myservo.write(pos); | |
delay(speed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment