// FACEPLATE PUSHBUTTON CONTROLL by Zabana
// This code moves one servo when a push-button is pressed from 0 to 180
// the code have an antibouncing system to avoid wrong instructions to arduino
// Another servo can be added easily following same pattern, just declare a
// second Servo myservo2; Maybe, depending on servo´s placement for the helmet
// you will need to declare also a new pos2 that is opposite to pos
// the pinout can also be changed easily if user needs to.
#include <Servo.h>
boolean buttonState = LOW;
boolean estado=LOW;
int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
ledPin=LOW; // we starts with faceplate open and eyes off
myservo.write(pos); // we assume pos=0 as faceplate open, change to 180 before if needed
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
delay (50); // we wait a little to avoid bouncing period in pushing
// Now we check if the pushbutton have been pressed.
// if it is, the buttonState is HIGH and estado is low,
// it mean pushbutton was not pressed till now (we avoid multiple pushing and let the servo moves right)
if (buttonState == HIGH & estado==LOW)
{
if (pos=0)
{
pos=180;
myservo.write(pos);
delay (500); // we wait for servo to lift down faceplate to light on eyes
digitalWrite(ledPin, HIGH);
}
else if (pos=180)
{
pos=0;
myservo.write(pos);
delay (150);
digitalWrite(ledPin, LOW); // Faceplate lift up and eyes light off
}
estado = HIGH; // we change estado, so we will not move servo till it´s not pressed and pressed again
}
else if (buttonState == LOW & estado==HIGH) // We do a "reset" to allow now pushing
{
estado = LOW;
delay (500);
}
}