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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ธ.ค. 2024

ความคิดเห็น • 115

  • @arliewinters2776
    @arliewinters2776 9 หลายเดือนก่อน +2

    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..

  • @CdrCARN
    @CdrCARN ปีที่แล้ว +6

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

    • @marios_ideas
      @marios_ideas  ปีที่แล้ว +2

      Glad it was useful to you. Consider supporting my channel:)

  • @mtb_zen89
    @mtb_zen89 9 หลายเดือนก่อน +1

    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!!

  • @altair6218
    @altair6218 4 ปีที่แล้ว +3

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

  • @dvshkbm
    @dvshkbm 3 ปีที่แล้ว +8

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

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      Thank you. Glad it is helpful.

  • @vikasahuja1175
    @vikasahuja1175 3 ปีที่แล้ว

    This video has solved many doubts. Thanks You Sir.

  • @hosemorelas2439
    @hosemorelas2439 ปีที่แล้ว

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

    • @marios_ideas
      @marios_ideas  ปีที่แล้ว

      Cheers:) Consider supporting my channel

  • @SantoshBhagwat1976
    @SantoshBhagwat1976 3 ปีที่แล้ว

    Explained in good manner with example 👍👍

  • @karounkapedrokilombo2374
    @karounkapedrokilombo2374 4 ปีที่แล้ว

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

  • @bernym4047
    @bernym4047 2 ปีที่แล้ว

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

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว +2

      Don't forget to give the video a like:)

  • @FrankLackenby
    @FrankLackenby 16 วันที่ผ่านมา

    Brilliant tutorial. Thank you 🙏

  • @michaelperez6811
    @michaelperez6811 3 ปีที่แล้ว

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

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      Glad my video was helpful

  • @plokijij7856
    @plokijij7856 3 ปีที่แล้ว +1

    Thanks exactly what I’ve been looking for!

  • @elzjonz
    @elzjonz 3 ปีที่แล้ว

    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 :(

  • @buzzwerd8093
    @buzzwerd8093 ปีที่แล้ว +2

    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?

  • @seekertosecrets7618
    @seekertosecrets7618 8 หลายเดือนก่อน

    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.

  • @yousaftoki1599
    @yousaftoki1599 2 ปีที่แล้ว

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

  • @agusnoviana
    @agusnoviana 2 ปีที่แล้ว

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

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว

      Great that it was useful. Like the video if you enjoyed it.

  • @johneagle4384
    @johneagle4384 3 ปีที่แล้ว

    Good explanation!

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      Thx:)Give video a like if you enjoyed it. It helps to grow my channel

    • @johneagle4384
      @johneagle4384 3 ปีที่แล้ว

      @@marios_ideas Yes, and I've subscribed too!

  • @republicofvegans712
    @republicofvegans712 ปีที่แล้ว

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

  • @ianthehunter3532
    @ianthehunter3532 3 ปีที่แล้ว +1

    Is there a benefit to declaring pins with #define instead of the standard "int somepin = somenumber;"?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว +1

      define - defines a constant that has a fixed value througout the whole runtime of the program. int something declares variable that can change the value during runtime. The pins should really be declared with define

    • @ianthehunter3532
      @ianthehunter3532 3 ปีที่แล้ว

      @@marios_ideas what about const int then?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      With the define, before your program is compiled into machine runnable form, all instances of somepin in your text are replaced with the somenumber. With the const int you are saying "I have an object somepin, which is an integer, and which cannot be changed, and its value is somenumber. The difference is subtle but the both do the same thing

    • @MyNotSoHumbleOpinion
      @MyNotSoHumbleOpinion 2 ปีที่แล้ว +1

      With constant int you are using precious ram and program space, with define not because compiler changes values for you! You can directly put the number of pin in pinMode, but with define you can change pins more easily and quickly during prototyping!

  • @Zampther
    @Zampther หลายเดือนก่อน +1

    I cant get this to work in the tinkercad simulator

  • @Zampther
    @Zampther หลายเดือนก่อน

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

  • @johnconnor6735
    @johnconnor6735 2 ปีที่แล้ว

    how would we add more leds to the code?

  • @TON-vz3pe
    @TON-vz3pe 2 ปีที่แล้ว

    Is it possible to blink both leds at exactly the same time using two different pins?

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว

      Sure you just duplicate digitalWrite commands. Then you use classic blink sketch without millies

    • @TON-vz3pe
      @TON-vz3pe 2 ปีที่แล้ว

      @@marios_ideas yeah right. I forgot that without delay they both will blink in an instant. The only problem Arduino drives me off is that it can't do real multitasking as it has only one core. Blinking leds might be easy to switch like you did, but doing things like read and write using Esp8266 and parallely running servo, leds, lcd, distance measure etc all of them at the same time will become a nightmare to implement, don't you think?

  • @benbenameur8429
    @benbenameur8429 ปีที่แล้ว

    hello
    Is there a way with the millis() for the LED tourn ON OFF only 5 ones?

    • @marios_ideas
      @marios_ideas  ปีที่แล้ว

      You need to better explain what you want to do

    • @benbenameur8429
      @benbenameur8429 ปีที่แล้ว

      millis() infinite loop . i want led turn on off by millis() but just 5 iteration ?

  • @metametax
    @metametax 3 ปีที่แล้ว

    Thanks for the video. Very good

  • @boopeshkumarprabhakaran
    @boopeshkumarprabhakaran 3 ปีที่แล้ว

    this is cool..but i cant define my outputs as im using shift register..soo can i do the millius which shift register

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      What exactly you are trying to achieve. Not sure I understand what your setup is.

  • @ztiger09
    @ztiger09 7 หลายเดือนก่อน

    Hey there I think you had the right right idea but i noticed there was a slight error with your code when you showed to the example of two leds with separate frequencies. It should look something like this
    unsigned long ms_from_start = 0;
    unsigned long ms_previous_read_LED1 = 0;
    unsigned long LED1_interval= 1000;
    unsigned long ms_previous_read_LED2 = 0;
    unsigned long LED2_interval=250;
    #define LED1 2
    #define LED2 3
    int LED1_state =0;
    int LED2_state =0;
    void setup() {
    // put your setup code here, to run once:
    pinMode(LED1,OUTPUT);
    pinMode(LED2,OUTPUT);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    ms_from_start = millis();
    if (ms_from_start-ms_previous_read_LED1>LED1_interval){
    ms_previous_read_LED1 = ms_from_start;
    if(LED1_state == 0){
    LED1_state =1;
    digitalWrite(LED1,LED1_state);
    } else{
    LED1_state = 0;
    digitalWrite(LED1,LED1_state);
    }
    }
    if (ms_from_start-ms_previous_read_LED2>LED2_interval){
    ms_previous_read_LED2 = ms_from_start;
    if(LED2_state == 0){
    LED2_state =1;
    digitalWrite(LED2,LED2_state);
    } else{
    LED2_state = 0;
    digitalWrite(LED2,LED2_state);
    }
    }
    }

  • @0124akash
    @0124akash ปีที่แล้ว

    Hello sir, good evening,
    Am creating a program 20Khz PWM signal for pump. It's absolutely working fine, when I wrote 2nd millis function and upload same board but 2nd program is not properly worked. Any solution for this sir ? Am using Arduino UNO board.

  • @damutds
    @damutds 3 ปีที่แล้ว

    hi I used your code its work good ,I need a help after reaching the some counter value the both blink need to stop and jump to next set of led or port

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      This video shows you how you can control 2 leds independently of each other. I am not sure what you want to achieve , but by the sound of it you need to introduce additional variable and when certain time passes you send high low to a different pins connected to different set of leds. Should not be difficult to achieve

  • @markgreco1962
    @markgreco1962 2 ปีที่แล้ว

    Can you give me a hint. How can I add a pause where nothing happens for 40 seconds then the program starts your code.

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว

      To pause for 40 second before the loop function starts you have to put delay(40000); line in setup function

    • @markgreco1962
      @markgreco1962 2 ปีที่แล้ว

      @@marios_ideas that seemed to easy! THANKS

  • @uploadideaswithitamar
    @uploadideaswithitamar 2 ปีที่แล้ว

    Great. Thank you

  • @srm-escapes6
    @srm-escapes6 3 ปีที่แล้ว

    very well explained. now i have a confusion if the on & off delay time is different in both cycle. like for a 7 sec cycle o/p-A will on for 1st 5sec and off for last 2sec and o/p-B will off for 1st 2sec and will be on for last 5sec. how i can code this using millis() in UNO?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      You lost me there in that question. Can you explain more clearly

  • @markgreco1962
    @markgreco1962 2 ปีที่แล้ว

    Oh yea I’m a new subscriber

  • @dytwtilak4607
    @dytwtilak4607 ปีที่แล้ว +1

    thank you a lot.

  • @polmourgos
    @polmourgos 3 ปีที่แล้ว

    Thanks for the video. Very good. I have a question. I want to use a push button and trigger a relay for 100 ms or other value of ms. For the project of one battery spot welder. Do you have any project similar? Thank you for your time.

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว +1

      I have a project showing how to use a relay. This would give you an idea how this should be done. I think checking my video about interrupts maybe helpful

  • @zulkarnainkarim
    @zulkarnainkarim 3 ปีที่แล้ว

    Wondering how to make the led1 blinks only 5 times and then switch off forever until certain activity execute again

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      Check this video of mine. th-cam.com/video/kMtRmj9DS2I/w-d-xo.html Maybe it will give you some idea. Wheneve you incounter change of signal at digital pin 2 or 3 you can fire the interrupt that will blink LED 5 tmes

  • @smacks999
    @smacks999 3 ปีที่แล้ว

    Great! Ahhh, but can we do that method and change the duty cycle, for instance, on 100ms, off 900ms?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว +1

      You can define any duty cycle. Just slight adjustment to the code

  • @omkarpasalkar1760
    @omkarpasalkar1760 4 ปีที่แล้ว

    Such a gr8 video it is...thank you so much💯

  • @renderuthis
    @renderuthis 11 หลายเดือนก่อน

    I wounder what real world applications I could do with this? maniacal robotic applications sounds fun.

  • @heshankalpa3909
    @heshankalpa3909 3 ปีที่แล้ว

    please i need this code with different on & off time used to two LED..please can you?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      If you watch my video and try to understand the code you should have no problem adjusting the code for your needs. Please try

  • @ChrisHalden007
    @ChrisHalden007 3 ปีที่แล้ว

    How do you deal with the situation where millis() overflows and is reset to 0? I mean in a life system that would be ON for more than 50 days.

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      I personally did not encounter this situation as I am a hobbiest. I think any project I use millies I would be able to write some if statements to address the situation where the number is reset. I checked arduino forums and there are plenty of disussions out there

    • @MyNotSoHumbleOpinion
      @MyNotSoHumbleOpinion 2 ปีที่แล้ว

      I use a if statement that triggers the reset of arduino when the millis approach the unsigned long overflow. But for me is not important the (not so) small lag of reboot!

  • @GorkemYildirim
    @GorkemYildirim 4 ปีที่แล้ว

    Thanks a lot, very clear.

  • @gopalakrishnand8696
    @gopalakrishnand8696 5 หลายเดือนก่อน

    Good 👍

  • @utsavdaga7590
    @utsavdaga7590 3 ปีที่แล้ว

    Best vid

  • @3736june
    @3736june 4 ปีที่แล้ว

    Thank so much I am very Enjoy it.

    • @marios_ideas
      @marios_ideas  4 ปีที่แล้ว

      Thanks. Check my other videos:)

  • @manfromthemist1958
    @manfromthemist1958 4 ปีที่แล้ว

    Hi Mario, great vids, and another subscriber,
    I've merged to codes one using buttons and the second to run a sequence of RGB Leds using millis, both functions work beautifuly seperatly but when I want the buttons to turn the code on and run the sequence I have to push and hold the button until it completes or it stalls when I release the button, any ideas why

    • @marios_ideas
      @marios_ideas  4 ปีที่แล้ว

      What do you mean the button work without leds. If you have momentary push button it changes the signal on arduino pin only when it is pressed and you need to program in some logic there so I guess this is where your problem is.
      Are you using interrupts with your push button. Check my video about interrupts, and also I have two videos with simple led projects. Two of them if I remember right are using buttons. There is no millis there but I think your problem is with the way you use buttons and that has nothing to do with millis

    • @manfromthemist1958
      @manfromthemist1958 4 ปีที่แล้ว

      thanks Mario, I'll check out the vid's@@marios_ideas

  • @reaction_topping
    @reaction_topping 4 ปีที่แล้ว

    Thank you, easy to understand,
    Auto sub...

    • @marios_ideas
      @marios_ideas  4 ปีที่แล้ว

      I am glad you find it useful:)

  • @nadunchanaka2295
    @nadunchanaka2295 3 ปีที่แล้ว

    Hi, Great explanation. But I have a question, when i put same time interval for the both millis functions, is possible to run both functions in same time without lagging?

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว +1

      If you run both functions at the same time then you need one interval. After which both actions would execute in sequence, right?

    • @nadunchanaka2295
      @nadunchanaka2295 3 ปีที่แล้ว

      Thank you for your response. I will try that.

  • @0124akash
    @0124akash ปีที่แล้ว

    Sir, how to make timer ? Specifications are
    20 second on
    60 minutes off
    This on and off in looping.
    On-off switch for timer stop.
    Keypad required for setting timer.
    Parts available
    Arduino UNO
    Keypad
    LCD 16*2
    On-off switch for timer

    • @marios_ideas
      @marios_ideas  ปีที่แล้ว

      I am not going to write the code for you. All the info required to write a timer code you will find in this video. th-cam.com/video/9a5q-xDvZQs/w-d-xo.html. Then you need to adjust the code to meet your needs

  • @metametax
    @metametax 3 ปีที่แล้ว

    pls multi step motor ( 4 pcs )

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      I will get to controlling stepper motors but not just yet

    • @metametax
      @metametax 3 ปีที่แล้ว

      @@marios_ideas ok

  • @cosminboglis5160
    @cosminboglis5160 2 ปีที่แล้ว

    thanks!

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว

      Glad you enjoyed it. Give video a like

  • @Medievalfan94
    @Medievalfan94 3 ปีที่แล้ว

    this just saved me another nano for a project... boy, and I thought every task needs another µC... those little bastards are plenty capable

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว

      Give the video a like If you enjoyed it:)

  • @GoatZilla
    @GoatZilla ปีที่แล้ว

    the arduino ecosystem needs a scheduler. without it all you can do is wait for your lowest allowable jitter, and that's awful for trying to write low power applications.

  • @bernym4047
    @bernym4047 2 ปีที่แล้ว +1

    You could simplify
    ' if (LED2_state==0) LED2_state=1; else LED2_state=0;'
    with
    'LED2 = !LED2'
    and similarly with LED1 😀

    • @marios_ideas
      @marios_ideas  2 ปีที่แล้ว +1

      Funny you mention this . Obviously you are right. And in my last video about shift register I was revisiting my old code and there I was working with common anode 7 7 segment display which have a reverse logic. 0 lights segment and 1 turns it off. And there I can see that I applied exactly this improvement to the code:) Thanks:)

  • @leviathancorporationfr92
    @leviathancorporationfr92 3 ปีที่แล้ว

    This is not multi tasking, it is one task, to be a real multi-tasking, both loop need to be in different function

    • @marios_ideas
      @marios_ideas  3 ปีที่แล้ว +1

      You are right . It is not multitasking. But you are wrong about the reason . I could easily rewrite the code So lines reffering to each process are in separate functions. It will be the same thing. For the end user it feels like multitasking, So lets call it software multitasking. The Real multitasking would be on the hardware Level where you configure two tasks to be served by two different cores of the processor. But this would be a little over Arduino Nano’s head:)

    • @leviathancorporationfr92
      @leviathancorporationfr92 3 ปีที่แล้ว

      @@marios_ideas i am an OS développer and i know how to do real multi-tasking, I didn't did it but ik, if you want to see my Kernel : github.com/Pokecraft-exe/LeviathanKRNL
      Some functions are in French or with an orthography fault 😂😅I will correct it

  • @TheGad999
    @TheGad999 ปีที่แล้ว

    Well explained , Big Thanks Bro .