I am looking to monitor a state change. When the state changes to high I would like to fire an output, when that state changes to low I would like to fire another output. I was trying to use the code below but I am getting 'Onstate' was not declared in this scope'? It's been a while since I used ARDUINO. Just looking for some advise.
**Update. I went with something a little simpler. My original concern was the state at startup but I guess the state is always off. Just as a hypothetical, could you check the startup state in the setup loop with an if statement? If the state was high at startup set LastToggleState to high?
// IO CallOuts
const bool Toggle1 = 0; //Toggle switch wired to digital 0
const bool Relay1 = 13; //Output relay wired to digital 13
const bool Relay2 = 12; //Output relay wired to digital 12
int ToggleState; //Global variable for toggle button state
int LastToggleState; //Global variable for toggle state tracking
void setup()
{
// Initialze IO
pinMode(Toggle1, INPUT); //Define pin0 as input
pinMode(Relay1, OUTPUT); //Define pin13 as output
pinMode(Relay2, OUTPUT); //Define pin12 as output
LastToggleState = LOW; //Define last state as off at startup
}
void loop()
{
ToggleState = digitalRead(Toggle1); //Read the switch
if (ToggleState == HIGH && LastToggleState == LOW) //
{
digitalWrite(Relay2,HIGH); //Turn relay 2 on
delay(100); //Delay 1 sec
digitalWrite(Relay2,LOW); //Turn relay 2 off
LastToggleState = LOW; //Record toggle state
}
if (ToggleState == HIGH && LastToggleState == HIGH) //
{
LastToggleState = HIGH; //Record toggle state
}
if (ToggleState == LOW && LastToggleState == HIGH) //
{
digitalWrite(Relay1,HIGH); //Turn relay 1 on
delay(100); //Delay 1 sec
digitalWrite(Relay1,LOW); //Turn relay 1 off
LastToggleState = HIGH; //Record toggle state
}
if (ToggleState == LOW && LastToggleState == LOW) //
{
LastToggleState = LOW; //Record toggle state
}
}