Skip to content

Instantly share code, notes, and snippets.

@hezhao
Created April 17, 2013 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hezhao/5406187 to your computer and use it in GitHub Desktop.
Save hezhao/5406187 to your computer and use it in GitHub Desktop.
toggle servo movement (CCW and then CW) with momentary push button switch.
#include <Servo.h>
Servo myservo;
int servoPin = 9;
int switchPin = 2;
void setup()
{
myservo.attach(servoPin);
pinMode(switchPin, INPUT);
pinMode(13, OUTPUT);
//Serial.begin(9600);
}
void loop()
{
int switchState = digitalRead(switchPin);
//Serial.print(switchState);
if (switchState == HIGH) {
digitalWrite(13, HIGH);
spray();
}
else {
digitalWrite(13, LOW);
}
}
void spray()
{
int pos;
for(pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(8);
}
myservo.write(90);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment