/* switch
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
David A. Mellis
21 November 2006
*/
int buttonPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
bool servoState = HIGH; // the current state of the output pin
bool reading; // the current reading from the input pin
bool previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long time = 0; // the last time the output pin was toggled
unsigned long debounce = 200UL; // the debounce time, increase if the output flickers
#include <Servo.h>
Servo servo;
int servoPin = 3;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
pinMode(outPin, OUTPUT);
servo.attach(servoPin);
}
void loop()
{
reading = digitalRead(buttonPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (servoState == HIGH)
servoState = LOW;
else
servoState = HIGH;
time = millis();
}
previous = reading;
if (servoState == true)
{
servo.write(0); // if not prerssed move servo to 180 deg
}
else
{
servo.write(180); // if pressed move servo to 0 deg
}
}