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