I created a flowchart for a program I would like to wright for the Uno. I would like to use the toggling digital output from an IR object detector to detect the presence of on object When an object is detected light LED1, if object is still being detected after 5 seconds, light an blink LED2, if the object is still detected after 10 seconds light LED3, turn all LEDs off and return to start.
This is far beyond my programming ability. Any thoughts on how to create this code would be awesome!!
Your flow chart does not directly produce code which can be efficiently implemented on an Arduino because you have blocking loops while checking if a timer has elapsed.
On a system with a multitasking operating system, that does not matter. With an Arduino (or other primitive microcontroller), however, not much else can happen during these blocking loops.
The usual solution on an Arduino is to have a single loop and have flags/status variables to activate bit of code in that loop as and when required.
Here is some pseudocode with a status variable which illustrates this:
bool objectInView = false ; // status variable
unsigned long objectCameIntoViewAtMs = 0 ; // timer
loop() {
if ( objectInView == false && <object detected by sensor> ) {
objectInView = true ;
objectCameIntoViewAtMs = millis() ;
}
else if ( objectInView == true && <object NOT detected by sensor> ) {
objectInView = false ;
switch off all leds
}
else if ( objectInView == true && <object detected by sensor> ) {
switch on Led 1
if ( millis() - objectCameIntoViewAtMs > 5000 ) switch on Led 2
if ( millis() - objectCameIntoViewAtMs > 10000 ) switch on Led 3
}
}
Indeed there is a duplicate. With user who has only 2 or 3 posts to their name, I always check what else they have posted exactly because of this. Mully911 has got over 30 posts so I did not check. Oh well. Some poor moderator will have to merge all this lot.