#include <Servo.h>
Servo myservo; //servo1
Servo myservo1; //servo2
int val; // variable for reading the pin status
int val2; // variable for to check consistency
int buttonState;
int servostatus = 0;
int switchPin =2; // Switch connected to digital pin 2
int ledPin = 5; // led array 1 to pin 5
int ledPin2 = 6; // led array 2 to pin 6
void setup() // run once, when the sketch starts
{
myservo.attach(9); //link servo 1 to pin 9
myservo1.attach(10); //link servo 2 to pin 10
pinMode(switchPin, INPUT); // button as input
pinMode(ledPin, OUTPUT); // led as output
pinMode(ledPin2, OUTPUT);// led2 as output
buttonState = digitalRead(switchPin);
myservo.write(0); // Closes the faceplate if forgot open when turning the circuit off.
myservo1.write(170); // ^
}
void loop() //main body:
{
val = digitalRead(switchPin); // read input value and store it in val
delay(10);
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (servostatus == 0) { //if leds off and faceplate open
servostatus = 1; // close the faceplate and turn leds on
myservo.write(0);
myservo1.write(170);
delay(1000);
//fade sequece
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(10);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(10);
//change the value for a faster or slower fade effect
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=10) {
analogWrite(ledPin, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(30);
}
} else {
servostatus = 0;
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(15);
myservo.write(170);
myservo1.write(0);
}
}
}
buttonState = val; // save the new state in our variable
}
}