Arduino Sketch with millis() instead of delay()

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ม.ค. 2025

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

  • @programmingelectronics
    @programmingelectronics  4 ปีที่แล้ว +15

    ****If you like this, I think you'll like the premium Arduino training we offer. You can check it out here**** bit.ly/3lHyzcB

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

      please make a video on arduino sensor kit i need help on it btw your videos are amazing

  • @AlienRelics
    @AlienRelics 5 ปีที่แล้ว +27

    Using
    previousTime = currentTime;
    will introduce an accumulating error. The time to process the code within the if statement will add to the time and accumulate. For just blinking LEDs or some other none critical stuff, no big deal.
    But if you simply use:
    previousTime += interval;
    You've just gotten rid of the cumulative error.

    • @programmingelectronics
      @programmingelectronics  5 ปีที่แล้ว +3

      Thanks for bringing this up Steve!

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

      @@programmingelectronics It probably won't affect most of what people will use this for, but it is just the sort of thing to drive you crazy later when you can't figure out why your timing is slipping.

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

    Smoooth animation bro. I love the homey type design, backdrop is really chill. Casually printing ice ice bb to the serial monitor lol. Explained it fantstic with the carrot comparison. Sending money but more importantly encouragment. Your great at this! keep going - seriously
    God Bless ;)
    - J. T

  • @johnrees44..G4EIJ
    @johnrees44..G4EIJ 4 ปีที่แล้ว +4

    I'm an 'old timer', and after a year of struggling with the theory and even the basic concept of 'millis', I've finally 'got it'!
    Thank you for the best Arduino 'millis' tutorial, and it's been my good fortune to find it..I look forward to watching more of your videos in future.
    John.. Bristol.. UK

  • @johncorbishley1548
    @johncorbishley1548 4 ปีที่แล้ว +22

    I have always struggled with this, even with your videos from years ago. I have watched numerous videos and still struggled. This video is simply THE BEST!
    You have done so well as an experienced programmer to be able to explain to us newby's how this work is such an easy to understand way.
    I have been copying & pasting other peoples bits of code randomly getting results and as my projects get more complicated, that approach does not work.
    Well done, you are the best teacher on TH-cam for Arduino.
    No.1
    Numero uno!

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

    I think Micheal is THE BEST TRAINER out there to learn the basics of programming in Arduino!!! I've spent many hours watching all kinds of Arduino videos before I found Micheal's - Programming Electronics Academy. I signed up and watched ALL of his videos and it was well worth the money!!!
    I highly recommend it to anyone that is getting into Arduino programming. He is really, really good!!!

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

      Thanks so much for the kind words John! I am glad you are finding the training helpful!

  • @cschmitz
    @cschmitz 5 ปีที่แล้ว +69

    This is a great explanation. The visuals really help wrap your head around the concept. Thanks for the vid!

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

    This is the best "millis()" tutorial I have ever seen. Thanks so much for explaining everything clearly.

  • @jandobbelsteen8953
    @jandobbelsteen8953 5 ปีที่แล้ว +22

    The way you show it will usually work nicely. However, the interval at which you trigger your event will be at least eventInterval, but often just a little bit longer. This is because the loop also takes a little time. Sometimes you may want to be more sure that you always trigger your timed event at an 'exact' multiple of 'eventInterval'. In such cases it's better to update your 'previousTime' with the 'eventInterval', like so:
    previousTime = previousTime + eventInterval;

  • @pedrofelipebezerra4491
    @pedrofelipebezerra4491 3 ปีที่แล้ว +10

    The best explanation I've ever seen about millis()

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

      true

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

    love the simplicity you bring into the explanation..... i had been reading up on example codes to understand millis function and scratching my head over and over. I wish i had found this video earlier...

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

    Wow this is so excellent. I've been bumping up against limitations imposed by my reluctance to learn this methodology but now I'm halfway through the video and excited about the possibilities this engenders.

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

      Great! Timing can be a bear. Another thing you might consider is learning about RTOS for scheduling tasks.

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

    GOD BLESS YOU! You saved me from days of suffering! You are my Hero!!!

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

    I like that the if statement works even when the counter overflows so it'll keep going after 49 days and not miss a beat.

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

    The best explanation ever... the carrot example was just epic... every noob can understand..... thank you so much for the great tutorial.

  • @kkaelyn002
    @kkaelyn002 14 วันที่ผ่านมา +1

    Amazing explanation and visuals to illustrate the more abstract concepts, cheers!

  • @martinest9458
    @martinest9458 4 ปีที่แล้ว +9

    what a Legend ! i wish i would of found your videos earlier! Absolutely the best explanation of things. Thank you Sir!

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

    Thanks a lot man!
    I watched numerous videos, but wasn't able understand the concept of space time along with black holes.
    But now I'm confident enough to derive E=mc².💪

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

    why cant every tutorial be like this. Love it best

  • @brucesmith9144
    @brucesmith9144 5 ปีที่แล้ว +13

    One thing that should be added to the code is a trap for when the value of the millis() function rolls over. Consider if your Arduino is being used on a long term basis exceeding the long int value maximum. The user would want a way to handle that condition and continue the event timer. One quick way would be to check if the new millis() time is less than the previous time then the event timer loop can be reset.

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

      The way the code is written, by subtraction of unsigned longs, it compensates for roll over. No additional checks are needed.

    • @____________________________.x
      @____________________________.x ปีที่แล้ว

      I was just wondering about rollover

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

      ​@@Ruudrad please explain a bit please. Is it in the code in your face code or firmware side that you simply do not see. As in...
      If I am using millis to run a tachometer on an engine, and using that tach value to hold ignition on (interlock if you will, if RPM drops it cuts ignition) it will not get the math wrong in seeing the correct rpm?
      The long term use case here is; the arduino is going to be powered on indefinitely monitoring a signal from inverter to start generator. Once the inverter calls, the arduino will sequence and start the generator. I dont want the roll over to cause am issue with a tach calculation inadvertently shutting down the generator.

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

      @@wadebrewer7212 I initially also thought roll over would cause problems, however if you use longs _and_ subtract the unsigned longs (holding the output of the millis() function and/or the millis() function itself) the calculation will also work when millis() rolls over (I.E. goes from almost its maximum to just over zero). Strange but true thanks to how subtraction works on a microprocessor, which is NOT exactly the same as mathematical subtraction.

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

      @Ruudrad Thank you tons. I was about to start researching for a work around. The community around this stuff is pretty great. Every once in a while you get the tool that talks down or "why do you want to do that project"....because I can....and at least for now...I am allowed.....lol.
      Even in my situation....reading and counting every quarter or half second for revolutions....even if it did send a call to shut off the system, the cycles are so quick I don't think it would actually shut down. But...sounds is like it's non issue anyway.
      Again, thank you for the input and much appreciated.

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

    When you saved me I really love that tutorial it's actually the best thing I've ever watched in the history of our doing it Tori old and I'm not even being hilarious that's the truth thanks man keep up the good work !

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

    Oh you did great. This will help me on the annoying issue between RF transmitter not sending data because pulse sensor eats all the event

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

    You make everything look simple .
    Thanks

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

    it was taking me forever to figure out why mine wasn't working, then i found out i had "==" instead of ">=" lol
    thanks for the very clear and concise explanation!

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

    Best explanation ever on millis. Thanks alot bro

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

    Hi! I'd like to thank you very much for the excelent explanation! I was struggling a bit with millis() until watched you videos. It saved me a lot of time and helped me to understand why my project wasn't working (that's the best way to learn something, by fixing it's bugs)! Keep on doing this awesome job, man!

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

    You are my favourite teacher.
    Thanks a bunch 😀

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

    First off, thank you for your very informative video. I know this video is a few years old but I just came across it so my apologies for such a late comment.
    From your video, and from others, I understand that millis() starts as soon as the Arduino starts running and it keeps running, and increasing, as long as the Arduino is up, at least until it reaches its maximum value and restarts. I'm not sure that you actually CAN reset millis() from within code but everyone seems to say you shouldn't. Apparently doing so can break some libraries that rely on it.
    In order to understand millis() better I made a little project using code similar to yours to blink the built-in LED on and off at a preset interval while displaying current_elapsed_time (read from millis()) and previous_time, something similar to what you did in your code, and everything ran as expected. However, I noticed that every time I closed the serial monitor and reopened it the current_elapsed_time, which should display the current millis() value, returned to its starting value. The LED continues to blink at the proper interval while the monitor is closed, so I know the code is still running properly.
    Thinking there was a bug in my code I created a new, stripped-down program which only makes a call to millis() and prints the retrieved value to the serial monitor (see code below) and ran my test again. Same behavior. The code calls for the millis() value to be sent to the serial monitor directly - no massaging or assigning to a variable - and every time I close and reopen the serial monitor it appears to reset millis(). Not sure what is happening.
    Here is the simplified code I used to test:
    void setup() {
    Serial.begin(115200);
    }
    void loop() {
    Serial.println(millis());
    }
    The program runs as expected and, as you can see, there is nothing but the call to print the current value of millis() but millis() seems to be affected by the status of the serial monitor window. Is this normal? Am I missing something? 🤔
    Appreciate your help on this.
    Thank you.

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

    Thank you for a wholesome explanation! Great stuff!

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

    😄 The millis code was smart. I love that logic

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

    Great video, thank you. Question, will that code work correctly when millis() overflows and starts counting up from zero?

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

    Thanks you make it so easy to learn

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

    Wow.. That was the greatest tutorial I have ever seen on youtube...Thank you...

  • @RamyHelmy-vv3gq
    @RamyHelmy-vv3gq 3 ปีที่แล้ว +1

    You really have easily way to explain each point,thanks

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

    This is awesome! the best explanation ever about millis function. Thanks a lot... Poor millis can't catch the carrot XD

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

      You're mean, Emmanuel!

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

    Very Cool!!
    You come out with this video just in time for me to use millis() to toggle between volts and amps on an OLED display (without using delays, delays that long when added up would have made the loop way too long for this project).
    It took me a bit to figure out how to write the second section (like this:) (currentTime - previousTime >= interval + interval_1) I added the second interval time so I could set the volts code to display less time than the amps code since that's the one that needs to be read the most, but I got it. I'm sure I will eventually figure out an easier way but it's good for now and I'm proud of it! :-D
    The most valuable information in your video series is the fact that the timer continues to count up until the microcontroller is reset or turned off and on again.
    I now also realize that I can run more than one program from the same millis() function.
    It's kind of like calculating what needs to happen at different times of the day with a certain amount of time for each task. Yeah, I know, probably a screwed up analogy but there it is LOL :-)
    Until now I thought that the timer reset each time it was called for.
    Thanks!!
    Joe

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

      Thanks also for sharing your addition on the code - very cool!

  • @o-h-1
    @o-h-1 5 ปีที่แล้ว +2

    At 6:53 I was wondering why eventInterval was an unsigned long. Isn’t it a const with a value of 1000? Can’t it be a int?

    • @alejandroperez5368
      @alejandroperez5368 5 ปีที่แล้ว +4

      You need to use the same type of variables when working with mills, so it needs to be an unsigned long. When doing some math with those variables, you need to use the UL suffix in the numbers (otherwise they are treated as integers and you get into trouble. Just check his first video about mills() to understand this.

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

    Very clear explanation - I like that. I have taught some stuff in my one past life...Good job!

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

    These lessons are so well done!!

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

    It becomes very easy to learn from you. You make everything looks so simple. Thanks

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

    the best tutorial on millis() ever... thanks...

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

    Really a great video.great people behind this Job...The visuals make everything so simple.thanks millions

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

    Excellent explanation and coding, with great visuals 👏

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

    Very entertaining channel, love your videos, keep them coming

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

    I found your video very helpful. Thank you!

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

    wow that's awesome and well explained ! The presentation is very nice and clean and understandable ! but... what is it again ? ah, needs to rewind this utube vlog again & again.

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

    Thank you,
    That saved my life ❤

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

    Very nice and easy to understand video, thanks!

  • @ChandanKumar-kr2kj
    @ChandanKumar-kr2kj 2 ปีที่แล้ว +1

    Great video. Thanks a lot for making this video.

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

    Great. first time explained, first-time understood. thank you very much

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

    Simply grand.

  • @j.w.8663
    @j.w.8663 ปีที่แล้ว +1

    @5:31 Aah, I see what you did there: The Neutron Dance! 😆

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

    enjoying this lessons. motivated to keep moving further. thanks a bunch.

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

    haha.. being mean with the carrot and millis() function. love it.

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

    Very clear and simple explanation . Thank you!!

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

    this is legendary! massive Thanks

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

    I think this is good idea if you need to step multiple stepper motors at the same time. When using delay() for moving stepper motors you can only move one stepper motor at a time

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

    Great video

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

    Amazing explanation, great thanks. Thus, there is something I don't manage to understand. If I want a start point not equal to the interval, I mean for example an event to start at 30 seconds, and to be repeated 60 seconds later on. Do I need one more variable to be created? Kind regards.

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

    Great video, thanks! One comment though, I think we could also use a modulus operator to manage the event intervals. This would also cause it to run at millis() == 0, but we can use an additional clause to limit this. Perhaps something like:
    if( currentTime > 0 && currentTime % eventInterval == 0 ) { ...

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

      Actually, it seemed like this would work in theory, but there needs to be a buffer, as the code would run multiple times at each interval. I guess this is why the lastTime variable is introduced, so that we can ensure the code runs only once per interval. Makes sense - I stand corrected!

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

    Thanks for this great explanation..it really helps a lot...

  • @simplyjust-6912
    @simplyjust-6912 2 ปีที่แล้ว +1

    Tyyyy this was such a good video 😄

  • @vamshikp.j.8592
    @vamshikp.j.8592 8 หลายเดือนก่อน +1

    amazing explanationnnnnn

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

    The best lesson!

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

    This is very informative

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

    This is a great explanation.

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

    thankyou so much ,it was a really big help

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

    Great explanation sir thank you 😁❤

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

    you could do the same logic and prob have better runtime using (inside the loop)
    if (millis() % interval == 0) {
    // do whatever you want here for every x amt of milliseconds
    // where x = interval
    }

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

      In an Ideal situation, you're right. But in reality millis would almost never hit exactly your interval. That's why >= is used in the video, but with your code >= wouldn't work.

  • @JohnSmith-lp5en
    @JohnSmith-lp5en 2 ปีที่แล้ว +1

    Little help or suggestion. I'm new to the Arduino programing or Arduino as a whole but I've running into a dilemma in which I'm looking for some guidance. So let me set the stage, I have a sketch that constantly flashes LED lights (Nav Lights) then I have a function that when a button is pushed it should play some music and flash another LED while the other LEDs (Nav Light) should continue flashing. However when I the delay function, the sound plays and the second LED flashes but the first flashing LED's (Nav Lights) pause. I have tried multiple different ways to try an use the millis() function but when I push the momentary button I can't get the SD card to play sound and the second LED to flash. Is using the millis() function the right thing to use or do I need to do something else. your lesson 4 started to discuss this but it didn't give an example.

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

      Hi John, great question here, and just so you know, timing can be super tough sometimes, so if you have a difficult time with it, you are in good company.
      I could be wrong here, but it sounds like you are using delay() in the function you are calling from your loop. If this is the case, when your function gets called, it will delay your loop, until you function is done running, and then return to the loop. Maybe you have realized this already...
      Another caveat to note here is that if you are using a library to play noises from an SD card, some of the functions in that library *may* be blocking code -> that is, code that holds up the program for a bit, while it executes. This may, or may not be the case, but it is something you'll have to figure out.
      The millis() construct that I go through in these lessons is definitely a way to handle timing different events but sometimes it depends on situation. Not sure this helps much or not!

  • @DesertVox
    @DesertVox 5 ปีที่แล้ว +3

    Is there a way to reset millis() without turning the Arduino off? That could be integrated into the code so that the project can go on for more than the 49 days limit.

    • @programmingelectronics
      @programmingelectronics  5 ปีที่แล้ว

      From what I can gather, the best way to "keep it going" is to not reset the timer, but to handle in code the overflow event.

    •  5 ปีที่แล้ว

      @@programmingelectronics Thanks. I understand you keep easy for beginners. But what you have shown will stop working after 49 days. Is that good programming? At the very least, you should have cleary mentioned this.

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

      @ If you do the comparison correctly, the overflow calculation can be handled. Consider
      if( millis()-Timer >= INTERVAL ) // will fire every INTERVAL milliseconds
      {
      // Place code here
      Timer += INTERVAL; // Move Timer to the location that should have fired .
      }
      If you test millis() - Timer will work around a wrap.

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

    Very nice explaination. Thanks

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

    Why not use the modulus function? If (currentTime % eventInterval == 0). It wouldn't be precise on the rollover (unless the interval was a power of 2) but it allows for lots of different intervals with minimal code.

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

    thankyou, I needed this

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

    Such a nice video 🙂

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

    very interesting, a good teacher

  • @horrorhotel46290
    @horrorhotel46290 5 ปีที่แล้ว +5

    How do you handle the overflow of the millis function ? Like this, code will run fine for 49 days and then stop working.
    You cannot be sure what exactly previoustime will be the last time the event occurs before overflow because of small inaccuracies that will sum up until the 49 days mark
    Somehow you need to know when to reset previous time without loosing information about the time lapsed since the last event
    This is a purely hypothetical example, but lets say ypu are building an arduino based pacemaker - you absolutely need to always reliably trigger a pulse at the same intervals, while sending some data like battery percentage etc to an external host or whatever
    How do you prevent the device from missing or falsely timing an event after 49 days?

    • @howardhecht1783
      @howardhecht1783 5 ปีที่แล้ว +3

      See forum.arduino.cc/index.php?topic=122413.0, in particular message #2 & #10 and also www.baldengineer.com/arduino-how-do-you-reset-millis.html

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

      The fact that you are using unsigned long for currentTime, previousTime, and interval makes the subtraction wrap around and work correctly. No need to reset the millis() timer.
      if (currentMillis - previousMillis >= interval) {

      //when currentMillis wraps around and previousMillis subtracts, the
      //answer is likewise wrapped back around and the difference is correct
      previousMillis += interval; // does not accumulate timing errors

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

      Helloo, if currentTime = "MAXIMUM of millis()" then next currentTime(or next value of millis() ) will be : Case 1 : or remaining to "MAXIMUM of millis()" or Case 2: next value will be 0.Which case do you think that will happened?.
      If Case 1: currentTime = "MAXIMUM of millis()" minus previousTime (which it will be olso "MAXIMUM of millis()") = 0 (so, the difference will be zero and all the time will not >= SetTime.
      IfCase 2: if millis after 49 days will be automatically 0 then difference 0 - "MAXIMUM of millis()" ('the last value of previusTime) will be negative , so, in this case it a must to use all the time ABS(currentTime-PreiousTime) >=SetTime.
      So which case it happening!?
      An alternative solution is to use a function to reset automatically arduino ( I tested this function:
      void(* resetFunction) (void)=0; and, after, in loop rutine call this function using: resetFunction();
      if millis()>=4000000000UL ....resetFunctio() .. .This resetFunction is very fast.Write , as example, Serial.print(F("abc..")); after calling resetFunction(); and you will see that Serial.print(F("abc..")); will not happened because arduino it was reset be resetFunction();
      OR, if someone cand wait ~ 49 days to see what happend with value of millis() function after this period , please let me know :)) .Maybe will happened one of both cases 1 or 2 ...or neither.

  • @Namai.
    @Namai. 5 หลายเดือนก่อน +1

    very well explained, thx

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

    fantastic video!thank you!

  • @Ivan-wb4id
    @Ivan-wb4id 4 ปีที่แล้ว +1

    After some time , millis wont be precise. Is there a way to restart millis after 1 second has passed. Like count up to 1000 milis, and then restert it to count again to 1000?

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

    Thanks for the extensive explenation.
    I made function out of this, which can receive interval in milliseconds and counter number, so it can be called multiple times and keep prevousTime in array for multiple counters.
    if(execDelay(2,1000)) {
    // Do stuff
    }
    bool execDelay(int instance, unsigned long v) {
    unsigned long prevTime[] = {};
    if(currTime - prevTime[instance] >= v) {
    prevTime[instance] = currTime;
    return true;
    } else {
    return false;
    }
    }

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

      Cool! Thanks for sharing that code!

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

      Just one correction, define prevTime outside the function.

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

    Sweet explanation :) Thank you

  • @kpro-ke5hd
    @kpro-ke5hd ปีที่แล้ว

    That actually make sanse,Thanks for the vod.

    • @kpro-ke5hd
      @kpro-ke5hd ปีที่แล้ว

      We dont have to crate a variable(previousTime) if we only wanna execuit the if once.but to update the timing we have to substract the (currentTime from the previousTime)

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

    You are awesome, I can't wait for the next video! 👍👍

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

    Great video! Going to implement it to my project ASAP

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

    Nice info, well done, thanks for sharing it :)

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

    Is it possible to make delay function in separate tab and use millis single line command like delay? For make code more simpler

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

    the best explain tutorial lesson thanks a lot 👍

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

    very informative...thanks a lot...

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

    Thank you sir i really appreciate that....😎😎

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

    Hey Programming Electronics Academy. Your code is good if you want 1000 milliseconds between events, which ie. takes 10 milliseconds to execute. However. If you want things to happen every 1000 milliseconds, sharp, then you will need to add 1000 to the carrot, instead of moving ahead with the relative time. You can say, that you would add 1000 to the limit of 1000 every time, making an absolute goal, rather than adding to the relative currentTime.

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

    I love the animnations, which program do you use for it?

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

    Thanks for video. I'm trying to understand this;
    Considering that we use delay depending on the if function;
    sample;
    sensor = 8;
    led out = 11;
    if(sensor == 1)
    {
    digitalWrite(sensor, HIGH);
    delay(10000);
    }
    I wrote it roughly like this. In this case, let's assume that the sensor in pin is active and the delay function starts. In this case, the system is in standby. In the first 5 seconds of this wait, if the value of the sensor pin changes, will the delay be disabled? that is, does the system exit without waiting at 5 seconds and start working again? otherwise, even if the pin value changes and if equality is broken, will the system continue to wait until 10 seconds is up?

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

      Great question Ahmet!
      Once the program hits the delay - it stops in its tracks - nothing will happen - no new sensor readings, no anything, until that delay time has passed.
      Let's say what you want is to wait until the sensor reading changes to some value/range, what you might try is something like a while loop that uses the sensor reading as the condition, and then inside the while loop keep reading and updating the sensor value. Not sure that is what you're after, but just a thought.
      Best of luck!

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

      @@programmingelectronics What I'm trying to do is that the data from the sensor is contuinue without stopping. That is, the leds turn on according to the data from the sensor, however, when the sensor data changes, the leds turn off momentarily. I guess this can be done with the millis function.
      Also, I was able to understand the millis function for the first time when I listened to your videos.
      Thank you for this valuable explanation.

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

    wow its amazing...

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

    Wow, you're the best!!! thanks!

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

    What a great video.

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

    love it

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

    Would using a modulus comparison rather than subtraction be a bad thing to do?

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

      I think that's a great idea. I really ought to use the modulus more often.

  • @Val-eq4sg
    @Val-eq4sg 5 ปีที่แล้ว +1

    Muchas gracias, At last someone took the time to explain this, I really needed it thank you kind GOD of code..

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

    So there are simpler forms, no?
    as:
    if (millis() > event) {
    // action
    event = millis() + interval; // interval milliseconds
    }
    or
    if ((millis() % event) == 0) {
    // action
    }
    this second option requires tweaking if you want it to run the first time:
    force = true;
    if ((millis() % event) == 0 || force) {
    // action
    force = false;
    }