below code is very useful from marcello.romani in http://forum.arduino.cc/index.php?topic=124707.0 please help me…
i want add one servo motor and one more push button (total 2) 1 push button for select mode
2nd push button for start the servo, main objective is, user select mode every LEDs have 1 mode, LED1 represent for 2 minute, LED2 is represent 5 minute, the servo will start rotate 90deg and return back, time interval according to mode selected.
int switchPin = 2; // switch is connected to pin 2
int led1Pin = 8;
int led2pin = 9;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int Mode = 0; // What mode is the light in?
boolean modeChanged = false;
const int NUM_MODES = 4;
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1Pin, OUTPUT);
pinMode(led2pin, OUTPUT);
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
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
Mode++;
if (Mode >= NUM_MODES) {
Mode = 0;
}
modeChanged = true;
}
}
buttonState = val; // save the new state in our variable
}
if (modeChanged) {
modeChanged = false;
// Now do whatever the lightMode indicates
switch(Mode) {
case 0:
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, LOW);
break;
case 1:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2pin, LOW);
break;
case 2:
digitalWrite(led1Pin, LOW);
digitalWrite(led2pin, HIGH);
case 3:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2pin, HIGH);
}
}
}