/*
DigitalReadSerial
Reads a digital input on pin 6, prints the result to the serial monitor
This example code is in the public domain.
*/
#define SYSTEM_ON 3 //Blue push button
#define PANEL_POWER 6 //Red LED
int latchState = LOW; //to latch the input of SYSTEM_ON
int currentState = LOW; //to indicate the current state of SYSTEM_ON
int lastState = LOW; //to indicate the last state of SYSTEM_ON
void setup()
{
Serial.begin(9600);
pinMode(SYSTEM_ON, INPUT);
pinMode(PANEL_POWER, OUTPUT);
}
void loop()
{
currentState = digitalRead(SYSTEM_ON); //reads the current input of push button ,i.e, SYSTEM_ON
if (currentState == HIGH && lastState == LOW) //detects a low to high transistion of input SYSTEM_ON, ie, when button is pressed
{
latchState = !latchState; //negates the latch state when the button is pressed
}
lastState = currentState; //stores the current state as last state and the loop executes again
if (latchState == HIGH)
{
digitalWrite(PANEL_POWER, HIGH); //PANEL_POWER ,ie, red LED glows when the switch is pressed once and goes off the next time
}
else
{
digitalWrite(PANEL_POWER, LOW);
}
/*
Prints the current state and latch state of PANEL_POWER in the serial monitor for reference.
Added a delay of 500ms to see the values displayed in serial monitor.
*/
Serial.print("currentState =");
Serial.println(currentState);
Serial.print("latchState =");
Serial.println(latchState);
Serial.println();
delay(250);
}