Help with a Timer Program

Hello, let me preface this by saying I am new to Arduino. I’ve been watching a bunch of Youtube videos about the topic and the device makes sense its just I for the life of me cannot understand the coding aspect. That’s why I have come here:

A little background on my project:

-School project for a scale formula racecar
-12V battery on the car with a voltage step down to 5v

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

I have searched endlessly for a solution but all of the ones I have come across are either alarms or timers and not so much what I am looking to do…

Any help would be appreciated. More info can be provided if need be. I have attached my code thus far and my schematic sketch of how I plan to use the Arduino on the car.

Thanks!

formulatimer.ino (921 Bytes)

I have searched endlessly for a solution but all of the ones I have come across are either alarms or timers and not so much what I am looking to do...

So, write your own. It isn't rocket science.

You don't need the Alarm class.

You DO need to record when the power is turned on. To do that, you need to notice when the power BECOMES on (NOT IS on).

You need to notice when the power BECOMES off (not IS off).

You need to periodically (that is every pass through loop()) see if the power is on, has been on for n seconds, AND the engine is not running.

How will you determine that the engine is/is not running?

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.

To the OP, paste your code directly in the forum inside code tags (see “how to post to this forum”) instead of attaching it as a file. It makes it easier for us.

For others to read, here’s his original sketch:

//This sketch triggers a beep after 3 minutes of drawing power from the battery
//A Timer is triggered once the car is turned on, and disabled when either power is cut off to the timer or the cars engine engages.

#include <Time.h>
#include <TimeAlarms.h>

void setup()
{

 Alarm.timerRepeat(180, Repeats);          // timer for every 180 seconds    
}

void  loop(){  
 digitalClockDisplay();
 Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
 Serial.println("Alarm: - Sound Audible");    
}

void Repeats(){
 Serial.println("180 second timer");         
}

void digitalClockDisplay()
{
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.println(); 
}

void printDigits(int digits)
{
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

Here's how to determine when pins change state (in this case a button, but it is true for anything that changes the pin)

https://www.arduino.cc/en/Tutorial/StateChangeDetection

And here's how to time things using millis():

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay