Skip to content

Instantly share code, notes, and snippets.

@flazer
Created November 18, 2013 09:09
Show Gist options
  • Save flazer/7524888 to your computer and use it in GitHub Desktop.
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
#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