## The millis function on Arduino

### The millis() Function on Arduino

Below is a simple explanation to understand the millis() function, which allows you to do non-blocking delays with the Arduino. Let's start with an example based on real life: imagine finding a job as a worker in a ballpoint pen factory, like the classic BIC. The pay will be piecework, so the more pens produced, the more money you earn.

The first day of work, the foreman gives you a brief explanation: here are the refills filled with ink, here the tubes (the body of the pen), and here the front caps and the rear caps; Assembling a pen is simple, anyone can do it: insert the refill in the tube, put the rear cap and the front cap. The only point to pay attention to, this is the automatic machine that fills the ink refills: it does everything by itself, but every 27 minutes you have to fill the ink tank by pressing this button.
If you do it too soon the tank pours, and you have to stop to clean the machine, if you do it too late, not enough refills come out, ready to whip up the pens quickly, and you have to slow down. So try to be very precise, press it when the 27 minutes are up. Leave immediately, it is 8.03 am, the tank is full, so next refueling at 8.30 am.

You start assembling pens as fast as you can, but every now and then you look at your watch, and as soon as it is 8.30, or a few seconds later, you fill the tank with ink, and then you continue to assemble pens, not before you have calculated the next refill of ink, which will be at 8:57. A good idea is to mark the time of the next deadline on a piece of paper. You continue like this, throughout the work shift, sure that the battery of your watch will continue to keep it running. You just have to look at it every now and then.

Let's see how we could use Arduino to make the button press every 27 minutes. Just to complicate things, let's assume that Arduino is already doing other things, for example, interrogating a temperature sensor every 10 seconds and writing the value on a display.

Arduino has a watch inside, with slightly different characteristics from the one we wear on our wrist. Its hands do not make a revolution every 12 hours, but being a 32-bit counter, incremented every millisecond (hence the name), after about 1193 hours it reaches its maximum value (4294967295), and then returns to zero and starts again. increase.
Let's calculate how many milliseconds there are in the 27 minutes that pass until the next tank refill: PeriodRecharge = 27 x 60 x 1000 or 1.620.000 Let's write it for convenience in a 32-bit variable (unsigned long), so we can easily change it:

We define another variable of the same type, where we will write from time to time, the time of the next refill of ink.

unsigned long NextRefill;

Now in our setup we write the first value of the next refill

void setup() {
bla bla
// sum 27 minutes to the current value of the clock
NextRefill = millis () + PeriodRecharge;
}

As you may have guessed, writing millis () is equivalent to telling Arduino to look at his watch. At any point in the sketch you can write Serial.println (millis () , obtaining a printout of the value on the terminal.

In our cycle (loop), we insert among other actions, the Refill Control, what we do every now and then, when we look at the time waiting for 27 minutes:

void loop() {
Bla bla
ShowTemperature();
CheckRefill();
bla bla
}

Here is the function that controls the clock

void CheckRefill(){
if (millis() > NextRefill){
// if we enter here it means that the 27 minutes have passed
// we set the next deadline
// sum 27 minutes to the current value of the clock
NextRefill = millis () + ReloadPeriod;
// press the refill button
digitalWrite(ButtonPin, HIGH);
delay(200);
digitalWrite(ButtonPin, LOW);
}
}

We just have to test it.
Please note: this solution is affected by the Overflow problem, and could be wrong if the Arduino is kept constantly on for more than 49.7 days (4,294,967,295 ms correspond to 1193.05 hours, which is 49.7 days). In this case, a different type of check must be carried out to avoid the problem.
Find out more here:  https://www.realmeteo.com/arduino-english/millis_no_overflow/ .

Luciano Pautasso

If you liked this guide, click on one or more banner ads. You will help us pay for the server. THANK YOU

Cerca

Categorie Articoli

Ultimi articoli

Internal-or-External-Watchdog   APRI

Watchdog-interno-o-esterno   APRI

Ripetitore-di-allarme-su-Whatsapp   APRI

Bufala-in-crosta   APRI

Home-Automation-ESPeriment   APRI

ESPerimento-Domotica   APRI

Arduino-measures-liquid-level   APRI

Arduino-misura-livello-liquidi   APRI

finder   APRI

LORA-english-version   APRI

Pluviometro-LORA   APRI

Pillole_di_Promessi_Sposi   APRI

LORA   APRI

promessisposi-riscrittura   APRI

Arduino_crashes   APRI

Arduino_si_blocca   APRI

Arduino_e_Trenino   APRI

millis_no_overflow   APRI

millis   APRI

Arduino_millis_no_Overflow   APRI

millis   APRI

Impara-Giocando   APRI

Corso-di-elettrotecnica-ed-elettronica-3-volumi   APRI

Libro-tutto-sull-audio---inglese   APRI

Libro-ELETTRONICA-FONDAMENTALE   APRI

Il-timer-NE555   APRI

Audio-Handbook   APRI

bottiglia-solare-per-illuminare-le-case   APRI

Calcolare-la-rata-di-un-prestito   APRI

I-Distillatori-solari   APRI