How do I code a timer?

So attached I have a small snippet of code for a robot car project to turn right or left. I need to write some code that will count to .7 seconds when the car starts to go right or left, and after that amount of time has gone by, the robot car stops. (I calculated that that's how long the car needs to turn for it to go about 90 degrees).

Yes, I'm aware there are other ways to go about this (magnetometer, etc...) but I'd like to do it this way. If anyone can help with how the logic could go, that would be extremely helpful!


Can't offer any suggestions with only snippets of code. You do know how to use millis() don't you and how many milliseconds there are in .7 seconds?
So, once you begin the turn, save the current millis() value and set a boolean to true so you know the vehicle is turning this direction. While the boolean is true, keep checking the millis() to see if the .7 has passed. Then turn the boolean back to false and stop the vehicle.

This may help you with some concepts:

1 Like

It is always a good idea to post the full code. And to give an overview about your project.

I assume that your function left() / right() switches two motors each motor connected to a H-bridge on / off

This means your functions left() / right() switches on the motors and after 0,7 seconds you want to switch off? Is ths right?

As it is a robot car that will do multiple things simultaniously avoid using delay().
Delay() does what its name says delaying. Your program can do nothing else as long as a delay() gets executed. But the finished robot-car will do multiple things simultaniously.

Imagine yourself as a "human microcontroller" that controls the car over a remote-control:
in analogy to delay() this would mean as long as you execute a delay your complete attention is absorbed by writing lines on a sheet of paper until you have 70 lines.

You can't watch at the car at the same time measuring the distance to an obstacle or switching on of a buzzer or a LED.

Your function loop() can be seen as the person that is watching and remote-acting the car
repeating different things in a fast manner:

  • watching
  • press/release or push/pull remote-controll buttons or sticks
  • switching on / off a buzzer ot LEDs
  • etc.
  • etc.

Writing 70 lines on a sheet of paper does not allow watching the car at the same time.

Now clever timer-programming works a different way

Take yourself time to read this. It will help to understand the basic concept because
it describes the functionality in normal words. This means there is no "translation" from code to normal language needed.

as an everyday example with easy to follow numbers
delay() is blocking. As long as the delay is "delaying" nothing else of the code can be executed.
Now there is a technique of non-blocking timing.
The basic principle of non-blocking timing is fundamental different from using delay()

You have to understand the difference first and then look into the code.

otherwise you might try to "see" a "delay-analog-thing" in the millis()-code which it really isn't
Trying to see a "delay-analog-thing" in millis() makes it hard to understand millis()
Having understood the basic principle of non-blocking timing based on millis() makes it easy to understand.

imagine baking a frosted pizza
the cover says for preparation heat up oven to 200°C
then put pizza in.
Baking time 10 minutes

You are estimating heating up needs 3 minutes
You take a look onto your watch it is 13:02 (snapshot of time)
You start reading the newspaper and from time to time looking onto your watch
watch shows 13:02. 13:02 - 13:02 = 0 minutes passed by not yet time
watch shows 13:03. 13:03 - 13:02 = 1 minute passed by not yet time
watch shows 13:04. 13:04 - 13:02 = 2 minutes passed by not yet time

watch shows 13:05 when did I start 13:02? OK 13:05 - 13:02 = 3 minutes time to put pizza into the oven

New basetime 13:05 (the snapshot of time)
watch 13:06 not yet time
watch 13:07 not yet time
watch 13:08 not yet time (13:08 - 13:05 = 3 minutes is less than 10 minutes
watch 13:09 not yet time
watch 13:10 not yet time
watch 13:11 not yet time
watch 13:12 not yet time
watch 13:13 not yet time
watch 13:14 not yet time (13:14 - 13:05 = 9 minutes is less than 10 minutes
watch 13:15 when did I start 13:05 OK 13:15 - 13:05 = 10 minutes time to eat pizza (yum yum)

You did a repeated comparing how much time has passed by
This is what non-blocking timing does

In the code looking at "How much time has passed by" is done

currentTime - startTime >= bakingTime

bakingTime is 10 minutes

13:06 - 13:05 = 1 minute >= bakingTime is false
13:07 - 13:05 = 2 minutes >= bakingTime is false
13:14 - 13:05 = 9 minutes >= bakingTime is false
13:15 - 13:05 = 10 minutes >= bakingTime is TRUE time for timed action!!

So your loop() is doing

void loop()
// doing all kinds of stuff like reading the newspaper

if (currentTime - previousTime >= period) {
previousTime = currentTime; // first thing to do is updating the snapshot of time
// time for timed action

it has to be coded exactly this way because in this way it manages the rollover from Max back to zero
of the function millis() automatically has a very good tutorial about timing with function millis() too .

There is one paragraph that nails down the difference between function delay() and millis() down to the point:

The millis() function is one of the most powerful functions of the Arduino library. This function returns the number of milliseconds the current sketch has been running since the last reset. At first, you might be thinking, well that’s not every useful! But consider how you tell time during the day. Effectively, you look at how many minutes have elapsed since midnight. That’s the idea behind millis()!

Instead of “waiting a certain amount of time” like you do with delay(), you can use millis() to ask “how much time has passed”?

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.