/*
Toggle LED’s state with the push button – first iteration
What we want to do is to toggle the LED’s state when you press + release the button.
So, the first time you release the button, the LED will turn on.
The second time, it will turn off. Etc.
*/
int LED_Pin = 8; //define pin 8 (digital pin)
int BUTTON_Pin = 2; //define pin 2 (digital pin)
int Current_Value = 1; //add additional variable
int Previous_Value = 1; //add additional variable
int LED_State = 0; //add additional variable
void setup()
{
Serial.begin(9600); //initialize serial communication
pinMode(LED_Pin, OUTPUT); //define LED_BUILTIN as output
pinMode(BUTTON_Pin, INPUT_PULLUP); //define pushbutton as input
}
void loop()
{
Current_Value = digitalRead(BUTTON_Pin); //Check ung state ni Push Button. Assign to Current Value
if ((Current_Value == 0) && (Previous_Value == 1)) //Boolean "AND" Operation
{
LED_State = 1 - LED_State; //This will be performed if Boolean "AND" Operation is SATISFIED
}
if (LED_State == 1) //Check state of LED_State. Note: Diresto na dito if %% is NOT SATISFIED
{
digitalWrite(LED_Pin, 1);
}
else
{
digitalWrite(LED_Pin, 0);
}
Previous_Value = Current_Value; //New value of "Previous_Value" will be the state of "Current_Value"
Serial.print("Push Button Current Value: "); // print a message to the serial monitor
Serial.println(Current_Value); //print the value to the serial monitor
Serial.print("Push Button Previous Value: "); // print a message to the serial monitor
Serial.println(Previous_Value); //print the value to the serial monitor
Serial.print("LED State Value: "); // print a message to the serial monitor
Serial.println(LED_State); //print the value to the serial monitor
delay(800);
}