// constants won't change. They're used here to set pin numbers, etc.
const int buttonPin = 15;
const int redLedPin = 2;
const int greenLedPin = 4;
// variables will change:
bool currButtonStatus = false; // to store the current button state
bool prevButtonStatus = false; // to store the previous button state
int count = 0; // to store the amount of button pressed
void setup() {
// initialize the button pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED pins as output:
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
}
void loop() {
currButtonStatus = digitalRead(buttonPin);
if (currButtonStatus == HIGH) {
if (prevButtonStatus == false) {
// increment count when button pressed and set previous button state to on
count++;
prevButtonStatus = !prevButtonStatus;
}
if (count % 2 == 0) {
// turn on red LED
digitalWrite(redLedPin, HIGH);
}
else {
// turn on green LED
digitalWrite(greenLedPin, HIGH);
}
} else {
// turn off all LEDs and set previous button state to off
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, LOW);
prevButtonStatus = false;
}
if (count >= 10) count %= 10;
}