How to use millis() function to multitask in arduino code.

preview_player
Показать описание
In this video I am looking at using millis function in the code.
Describing the advantages it has over using delay function.

This video is also introducing the concept of multitasking in Arduino code.

The list of components used:
- Arduino Nano
- Couple of LEDs
- Couple of 220Ohm resistors
- 2 Potentiometers

The code can be accessed here:

If you like this content and you want to support me in creating similar videos go to my Patreon webpage
Or

Music credits:

––––––––––––––––––––––––––––––
Track: Secret To Happiness — JayJen [Audio Library Release]
Music provided by Audio Library Plus
––––––––––––––––––––––––––––––
Рекомендации по теме
Комментарии
Автор

Good presentation....!
I come from C#... C# has a function called, "background worker".
I believe this millis() is just what I need for some of my code in Arduino..

arliewinters
Автор

Brilliant. Thankyou. Just the depth of explanation I was looking for.

CdrCARN
Автор

much better than the "paid course I've enrolled"

altair
Автор

a simple idea but absolutely KEY to doing complicated projects involving reading multiple sensors with different accuracies (at different sample rates).... 

I was doing it this way before but I like your idea better: 

if (millis() >= time_HS + HS_INTERVAL) { //as soon as time ('millis()') greater than the time of the last heart beat check plus 20 milliseconds run another heartbeat check
time_HS += HS_INTERVAL; //add (another) 20 milliseconds to time_HS to mark the time of the last heart beat check

obviously instead of adding the HS interval "manually" to a running total you can just use millis() :p. thanks a lot for the tip!!

mtb_zen
Автор

very well explained, just what I was looking for. Thanks!

dvshkbm
Автор

I feel like It can be explain in more detail but I manage to understand and this is what I was looking for

michaelperez
Автор

This video has solved many doubts. Thanks You Sir.

vikasahuja
Автор

Explained in good manner with example 👍👍

SantoshBhagwat
Автор

Very useful project helping me understand programming. I am just starting using and programming arduino. I will download and study the code. Many thanks.

bernym
Автор

Hi Mario. Your filmy are very helpfull and made in very interesting way. Thank You!

hosemorelas
Автор

one new subscriber.. I was using until today.. thank you a lot.. by the way you gained a new subscriber

karounkapedrokilombo
Автор

Thanks exactly what I’ve been looking for!

plokijij
Автор

Amazing knowledge sir....you are great..

yousaftoki
Автор

Cool, thx for shearing ur knowledgewith us, this tutorial was very useful

agusnoviana
Автор

Well, I was able to understand the video. Now, I have to try and adjust so I can get the right frequency for the first set of LEDs. If it doesn't work, I may have to get a separate board so I have better control of what I want. I'm trying to make a costume and I have get the frequency of the lights just right in accordance to the show.

seekertosecrets
Автор

With millis() you can use unsigned ints to measure intervals up to 65.535 seconds. 8 bit millis only returns 250 different values, the low 8 bits never == 255 but you can use type byte for fast calculating led fades for instance.

Because the millis() clock low 8 bits takes 250ms to roll over, the next bytes count 1/4 seconds exactly.

If I want a 1 second repeat I watch the bit that changes every half-second, it is 1 every second for 1/2 second.
Use in a state machine.

These cooperative tasking techniques are old. When i was beginning in the 80, s I was reading Creative Computing and Intel design notes from the late 70's... AVR core at 16MHz is about 20x as fast as a C64.

Count how many times void loop() runs every second and print it. Is it more than 60, 000?

buzzwerd
Автор

What if I just want to print something on lcd 16x2 using millis? I want to have alternate of 5secs clock display and 3secs temperature humidity display.. I dont have int LED1_state=0 and just using lcd.print functions. I am having difficulty in the part 5:31 on the video.. And planning to add also temperature controlled switch, to turn on the fan in certain temperature.. Pls help :(

elzjonz
Автор

What if in the first if statements the start - previous = interval instead of using > ? In that case, would the LED blinking be precise?

republicofvegans
Автор

where in the code do he define pin 3 as output?? i se he uses define but i cant get it to work

Zampther
Автор

how would we add more leds to the code?

johnconnor