Sort of Debounce ?

One of my first posts here, so be gentle :slight_smile:

Basically, I need to check a variable, easy enough :slight_smile:

However, when this variable (pinNum == 10), I need to send serial.println("Hello"); for example if that state is received.

As this variable may be at this state for any length of time, if this is in a loop, the serial command is constantly executed.

I need to check the pinNum variable each time the loop is executed, however, only send the serial command once if that state is received.

Once the state is no longer present, I effectively need to reset and await for that state to be present again and send the serial command once more.

I hope that makes sense and I would appriciate guidance on how to put this into code.

Many thanks.

You don't say what changes this variable.

However what you need is a flag (another variable) that you change once you have seen the monitoring variable hit the required number. The flag variable can be a Boolean type (that's a logic variable) called unfound set to true in the setup(), an included in your comparison with a logical and operation:-
if(pinNum == 10 && unfound) {
// do your stuff here
unfound = false
}
Then the '// do your stuff here' will only be executed once, until something else sets it to true again.

Hope that helps.

Many thanks for your reply.

Sorry, I should have said that pinNum is an Integer and not a variable.

This is a piece of the code:

      if (pinNum == 11)
      {
        Serial.println("Alarm Activated (pin 11)");
        Serial.println("Alarm_1"); 
      }
      else if (pinNum == 12)
      {
        Serial.println("Alarm Activated (pin 12)");
        Serial.println("Alarm_2");
      }

I should have said that pinNum is an Integer and not a variable.

Sorry I don't know what you mean, it can be an integer variable but not just an integer.
There is no need to use the else, just use two ifs.
It would be better if you included all your code so we can see what is going on.

Sorry, should have made it clearer.

Basically this Arduino is receiving a serial command over RF from another Arduino.

It will either receive an "11" or "12" and then perform a task depending on what has been received.

As this "receiving" is in a constant loop, it will either be receiving or not as the case may be, I'm having difficulty understanding how the boolean function can be added :~

In summary, if it receives an "11" for example I only want it to perform the function once despite the amount of consecutive times it receives this serial string. However, if an "11" is not received for 20 seconds for example, then the next time it is received, the function can be performed once more. Like I say a sort of debounce.

Does that help or does that confuse the matter even more !

Yes it is a bit clearer but still a few point I don't get yet.
This 11 or 12 what form its it. You say it would be received but is it a string? It have to be received into a variable and it is that variable you use in the if statement.
As to the repeat only after 20 seconds, is this to be only if 11 has not been received or 20 seconds after the last one. This is very much like what I told you in the first post:-

long int timeout;
// the bit that does the stuff once
if(pinNum == 10 && unfound) {
// do your stuff here
unfound = false;
timeout = millis();  // reset the time to set the unfound variable
}

// in the loop now check if unfound needs to be reset
if(millis() > timeout + 20000) unfound = true; // allow the stuff to be done again

Thanks for your help Mike, the code now works fine.

I have realised when using this code, i also need the addition of checking that if the required pin is still LOW (after it is pushed and held down), do not perform the function until it goes HIGH (released) and then LOW (pushed) again.

How would you advise is the best way to manage this.

I currently have:

long int timeout;
boolean unfound = true;

void setup()
{
//  Setup variables etc are here
}

void loop()
{
  if (millis() > timeout + 5000) unfound = true;  // 5 seconds = 5000

  // check if the pushbutton is pressed.
  while (Pin1 == LOW && unfound) {
      function1();                    // Calls required function
      unfound = false;
      timeout = millis();
  }
}

Sorry to be a pain, but with a little help I am learning :slight_smile:

See if this works for you:-
replace this line:-

if (millis() > timeout + 5000) unfound = true;  // 5 seconds = 5000

with this one:-

if ((millis() > timeout + 5000) && Pin1 == HIGH ) unfound = true;  // 5 seconds = 5000

Sort of simple when you see it in black & white :blush:

Works like a charm, thank you so much for your help in solving this issue Mike.

Kind regards,

Steve