I'm trying to see if there is a way to achieve taking an internal Boolean tag state change (False to True, or True to False) and being able to make another Boolean tag True for just one scan of the program, and then return it back to False?
I was hoping to avoid using a timer for this function to save processor loading.
I'm new to Arduino, but am familiar with Allen Bradley / Rockwell industrial PLC programming, which includes a One-Shot (ONS) function to achieve this.
Do any libraries contain such a function? It may be called something different in Arduino, as the term "One Shot" appears as a feature of the Buttons library, which is not what I'm looking for, as this project does not involve a real hardware input that needs debouncing.
It's simple. Keep two Boolean. One holds the state of the other from the last pass. If they're different then take your action. At the end of the loop, set them old one equal to the new one.
Groove:
What do you mean by"one scan of the program"?
One "Loop" I guess in Arduino speak (I'm still learning the language, Scan is a term used in PLC programming meaning to execute all functions of the program once, see here - meaning much the same thing.)
Delta_G:
It's simple. Keep two Boolean. One holds the state of the other from the last pass. If they're different then take your action. At the end of the loop, set them old one equal to the new one.
Excellent Delta_G, thanks.... I knew I was missing a simple trick!
I'm really looking forward to playing around with Arduino, and learning some real nuts & bolts programming!
The Arduino program starts at the first line in the loop() function, when it gets finished with the last line it jumps back to line 1, thats your "scan". Big difference is PLC reads all the INPUTs first, solves the logic, then writes all the OUTPUTs.
I guess Arduino could be programmed to do the same, I'm going to try that (one day).
Ah, so an Arduino processor only reads the inputs as and when called to by the program?
So if your code reads one pin at the start, and then a second one mid way through loop, you could end up with a few ms between pin reads, depending on the program... every day is a school day!
If timing is important to your application, it may be worth structuring your code to read all your pins to a set of internal tags at the start of the loop as a block, and writing all the outputs as a block from another set of internal tags at the end of the loop.
PLCs do so much of the background work for you, making it quite a high level language, but I guess that's reflected in the astronomical cost!