blincoln1534:
I need the sketch to do this:
Trigger a 3 minute timer once the car's power is turned on, and disabled when either power is cut off to the timer or the cars engine engages
-Then:
Trigger a beep after 3 minutes of drawing power from the battery when the car is on, but the engine is not on
-Run a constant loop of this so that one loop won't end the program
Inside of loop() you want to track and respond to changing conditions and while there are many ways to do it, a beginner might write an if(){} for each state so that only the code runs that's supposed to. Each if(){} has to be able to know when it's time to change state (like after 3 minutes, etc) and change the variable that holds the state value.
Loop() may run 1,000,000 times or more in one state before changing to another and note that state doesn't have to change in 1-2-3 order -- you can reset or redirect your process at any time on any input you code for (like a button or IR remote signal). State code can be extremely responsive.
You might get yours down to two states, I dunno.
Timing can be done many ways. Arduino runs two timers to count microseconds and milliseconds since the board started. The time values are returned by the functions micros() and millis(). Usually millis() is close enough. Both of those return unsigned long values.
unsigned long tStart, tInterval; // my two time-holding variables
...... further down in the code
tStart = millis();
...... doing things that take time
tInterval = millis() - tStart; // this value will always be right up to 49.7-some DAYS
To make a timer, this inside of loop() will do, consider tInterval was set to 3000 (3 seconds)
void loop()
{
// code
if ( millis() - tStart >= tInterval )
{
// wait is over, do whatever
// next line can be different
tStart += tInterval; // sets up another wait 3 seconds after the last was supposed to happen
}
// other code
}
If you want a 1-shot timer, make it only run if tInterval is > 0, control through tInterval.
void loop()
{
// code
if ( tInterval > 0 )
{
if ( millis() - tStart >= tInterval )
{
// wait is over, do whatever
}
}
// other code
}
Really your biggest problem may be not defining your process right before you write any code.
Hope this helps. It's nothing like all the answers, just some tips.