const int buttonPin = 4;
const int LedR = 7;
const int LedG = 6;
const int LedB = 5;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// put your setup code here, to run once:
pinMode( buttonPin, INPUT );
pinMode( LedR, OUTPUT );
pinMode( LedG, OUTPUT );
pinMode( LedB, OUTPUT );
Serial.begin(9600);
Serial.println("BIANCO");
digitalWrite(LedR, HIGH);
digitalWrite(LedG, HIGH);
digitalWrite(LedB, HIGH);
}
void loop() {
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
//Serial.println(buttonPushCounter);
switch (buttonPushCounter)
{
case 1:
Serial.println("BLU");
digitalWrite(LedR, LOW);
digitalWrite(LedG, LOW);
digitalWrite(LedB, HIGH);
break;
case 2:
Serial.println("VERDE");
digitalWrite(LedR, LOW);
digitalWrite(LedG, HIGH);
digitalWrite(LedB, LOW);
break;
case 3:
Serial.println("VIOLA");
digitalWrite(LedR, HIGH);
digitalWrite(LedG, LOW);
digitalWrite(LedB, HIGH);
break;
case 4:
Serial.println("ROSSO");
digitalWrite(LedR, HIGH);
digitalWrite(LedG, LOW);
digitalWrite(LedB, LOW);
break;
default:
Serial.println("BIANCO");
digitalWrite(LedR, HIGH);
digitalWrite(LedG, HIGH);
digitalWrite(LedB, HIGH);
buttonPushCounter = 0;
break;
}
} else {
// if the current state is LOW then the button went from on to off:
}
// Delay a little bit to avoid bouncing
delay(150);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}