Monitoring state change

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
  } 
}

I don't know, but calling 'loop()' recursively does take guts.

Post your code in code tags ( this button </> ).

You code is not even close to being able to be compiled... there are many fatal errors, particularly around how your functions are defined and the use of recursion throughout.

I suggest you start with some of the Arduino tutorials and slowly add functionality bit by bit.

OP did post the code but removed it. I don't know if you can see the orange pencil but clicking that will show the edit history.

You're missing a closing '}' at the end of the loop function. Easy to spot if you use tools -> autoformat in the IDE; each function definition should start at the beginning of a new line.

Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem :wink:

1 Like

You can save some code by comparing ToggleState and LastToggleState for 'not equal'. Now you know that the state has changed and you know what state it changed to. That's all you need to select the desired action. Two 'if' statements instead of four.

void loop()
{
  ToggleState = digitalRead(Toggle1);                   //Read the switch

  if (ToggleState != LastToggleState)
  {
    LastToggleState = ToggleState;                            //Record toggle state

    if (ToggleState)
    {
      digitalWrite(Relay2, HIGH);                       //Turn relay 2 on
      delay(100);                                       //Delay 1 sec
      digitalWrite(Relay2, LOW);                        //Turn relay 2 off
    }
    else
    {
      digitalWrite(Relay1, HIGH);                       //Turn relay 1 on
      delay(100);                                       //Delay 1 sec
      digitalWrite(Relay1, LOW);                        //Turn relay 1 off
    }
  }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.