# Please help me rewrite delay with millis

**URL:** <https://forum.arduino.cc/t/please-help-me-rewrite-delay-with-millis/1139190>\
**Category:** Programming\
**Created:** [June 18, 2023, 3:54pm UTC](https://forum.arduino.cc/t/please-help-me-rewrite-delay-with-millis/1139190 "2023-06-18T15:54:30Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 18, 2023, 6:54pm UTC](https://forum.arduino.cc/t/please-help-me-rewrite-delay-with-millis/1139190/10 "2023-06-18T18:54:50Z")

</div>

> [@scinceman](#):
>
> This code works fine. All I want is someone to rewrite my delays as millis. I fully understand the millis function but not in a row (one after another).

Interesting chosen words "rewrite my delays as millis".

Yes sure using millis needs to some extent rewriting the code.

You can see your functionality as different modes of operation are needed

## if left bumper is hit

change from  
mode 1: go\_forward  
to  
mode 2: go\_backwards for a 1000 milliseconds  
after that change to  
mode 3: spin\_right for 400 millisecods  
after that change back to  
mode 1: go\_forward

## if right bumper is hit

change from  
mode 1: go\_forward  
to  
mode 2: go\_backwards for a 1000 milliseconds  
after that change to  
mode 4: spin\_left for 400 millisecods  
after that back to  
mode 1: go\_forward

This means you have two sequences of certain modes of operation  
that shall be run trhough always in the same order

These sequences with different modes of operation are invoked by the event  
left bumper hit / right bumper hit

In Programming something like this is called a state-machine.  
Each mode of operation is a "state" the "machine" is in.

There are events like

- "sensor-value becomes "HIGH"  
or
- sensor-value goes above a threshold-value  
or
- sensor-value goes below a threshold-value  
or
- a certain amount of time has passed by

This is what your code is doing depending on events happening change the mode of operation  
= change the state

The event a certain amount of time **has** passed by (since a reference-point in time)  
is something that you know from your everyday life  
example  
baking a frosted pizza. The cover says "baking time 10 minutes"

You can start at any time of the day to put the frosted pizza into a hot oven  
You take a look onto your watch and make a snapshot of time inside your head  
= the reference point in time  
for example putting the frosted pizza into the oven at the time 13:05  
baking time is ten minutes means at time 13:15 pizza is ready to eat

another day you do it at time 14:37  
baking time is ten minutes means at time 14:47 pizza is ready to eat  
etc. etc.  
you check the time that has passed by since a reference-point in time  
The rest of the details is explained in this tutorial

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/topic/974017/1):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

How to code different modes of operation named state-machine are explained here

> [@A Demo-Code explaining the switch-case state-machine and how to do things (almost) in parallel](https://forum.arduino.cc/t/a-demo-code-explaining-the-switch-case-state-machine-and-how-to-do-things-almost-in-parallel/888172/1):
>
> Hi everybody, if you look at the end of this first posting you see a list of links that show where this tutorial was used as a link. Go through this list to find additional information on how to apply this code This democode requires only the pure microcontroller without any hardware. You just open the serial monitor to watch the behaviour of the code. From time to time I enjoy writing demo-codes that want to explain programming-functionalities. There are a lot of different approachs to expl…

So start reading in these tutorials and ask questions if you have any question

best regards Stefan

---

_[View the full topic](https://forum.arduino.cc/t/please-help-me-rewrite-delay-with-millis/1139190)._
