//int = integer
//const = constant, cannot change
const int LED_1 = 2; //LED 1 is set to pin 2
const int LED_2 = 8; //LED 2 is set to pin 8
int ButtonPin = 3; //Button read is set to pin 3
int ButtonPushCounter = 0; //Counts how many times button was pressed
int ButtonState = 0; //Read state of button (HIGH or LOW, On or Off)
int LastButtonState = 0; //Saves previous state of button
void setup() { //sets up pins and variables
pinMode(ButtonPin, INPUT); //sets Button read pin to Input to read
pinMode(LED_1, OUTPUT); //sets pin to LED 1 as output
pinMode(LED_2, OUTPUT); //sets pin to LED 2 as output
Serial.begin(9600); //sets up the transmission of data in 9600 bits per second (baud) for communicating with the Serial Monitor
}
void loop() { //start loop
ButtonState = digitalRead(ButtonPin); //ButtonState = Reads state of button (on or off/high or low)
if (ButtonState != LastButtonState) { //if button state is not equal to last button state
if (ButtonState == HIGH) { //and if the button is pushed
ButtonPushCounter++; //then add +1 to button push counter
Serial.println("on"); //Display "On"
Serial.print("pushes #: ");
Serial.println(ButtonPushCounter); //Display the numbe of pushes
}
else
{
Serial.println("off"); //otherwise, say that it's off
}
delay(50);
LastButtonState = ButtonState; //last button state stores last button state
//If it does not compare the new vs old state in condition, as long as you keep pressing the botton it will repeatedly say "on"
if ((ButtonState == HIGH) && ((ButtonPushCounter % 2) == 0)) { //If button is pressed and the push count is even then...
digitalWrite(LED_1, HIGH); //Turn LED 1 on
}
else {
digitalWrite(LED_1, LOW); //If not, leave it off
}
if ((ButtonState == HIGH) && ((ButtonPushCounter % 2) != 0)) { //If button is pressed and the push count is odd then...
digitalWrite(LED_2, HIGH); //Turn LED 2 on
}
else {
digitalWrite(LED_2, LOW); //If not, leave it off
}
}
} //end loop