Arduino Sketch with millis() instead of delay()

  Рет қаралды 229,102

Programming Electronics Academy

Programming Electronics Academy

Күн бұрын

Пікірлер: 310
@programmingelectronics
@programmingelectronics 4 жыл бұрын
****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 жыл бұрын
please make a video on arduino sensor kit i need help on it btw your videos are amazing
@AlienRelics
@AlienRelics 5 жыл бұрын
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 жыл бұрын
Thanks for bringing this up Steve!
@AlienRelics
@AlienRelics 5 жыл бұрын
@@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.
@brucesmith9144
@brucesmith9144 4 жыл бұрын
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 2 жыл бұрын
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 6 ай бұрын
​@@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 6 ай бұрын
@@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 6 ай бұрын
@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.
@muhammadbilal2000
@muhammadbilal2000 3 жыл бұрын
Sweet explanation :) Thank you
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Glad it was helpful! Thanks for watching!
@o-h-1
@o-h-1 5 жыл бұрын
At <a href="#" class="seekto" data-time="413">6:53</a> 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 жыл бұрын
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.
@cschmitz
@cschmitz 5 жыл бұрын
This is a great explanation. The visuals really help wrap your head around the concept. Thanks for the vid!
@programmingelectronics
@programmingelectronics 5 жыл бұрын
Thanks for watching!
@jandobbelsteen8953
@jandobbelsteen8953 5 жыл бұрын
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;
@programmingelectronics
@programmingelectronics 5 жыл бұрын
That's a fantastic point - thanks for that adding that Jan!
@lindsaydempsey5683
@lindsaydempsey5683 3 жыл бұрын
Great video, thank you. Question, will that code work correctly when millis() overflows and starts counting up from zero?
@martinest9458
@martinest9458 4 жыл бұрын
what a Legend ! i wish i would of found your videos earlier! Absolutely the best explanation of things. Thank you Sir!
@programmingelectronics
@programmingelectronics 4 жыл бұрын
Thanks a ton! Glad they were helpful.
@johncorbishley1548
@johncorbishley1548 4 жыл бұрын
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 KZbin for Arduino. No.1 Numero uno!
@Jevajs
@Jevajs 4 жыл бұрын
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.
@stevenfordham2262
@stevenfordham2262 4 жыл бұрын
What no black holes and space time?? why bother making a video even?
@smeek111
@smeek111 2 жыл бұрын
So I was really interested in the black holes and space time talk.......
@Ivan-wb4id
@Ivan-wb4id 4 жыл бұрын
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?
@TrentSmith-m4h
@TrentSmith-m4h 11 ай бұрын
Same idea…. Slightly different solution: if(millis() % 1000 == 0)) { // make your fish taco } IOW use modulus operator… any number divided by 1000, then take the remainder. When remainder is zero, millis is a multiple of 1000. Also: You can make an LED ‘flicker’ by using the random function to change the 1000.
@edgarbonet1
@edgarbonet1 5 ай бұрын
This is a bad idea for two reasons: 1. If your loop() ever takes more than one millisecond to run, you may well miss the specific millisecond you where aiming for. 2. millis() doesn't count every single millisecond: as it is only updated every 1024 µs, it skips one value roughly every 42.7 ms (it skips 42, 85, 127, 170, 213, etc.).
@whitejtw
@whitejtw Ай бұрын
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 Ай бұрын
Great! Timing can be a bear. Another thing you might consider is learning about RTOS for scheduling tasks.
@davidmathejasteam9706
@davidmathejasteam9706 2 жыл бұрын
Well ok, you havent explained how to make DELAYED functions. What if I just want to press the button, and after EXACTLY 10ms to light up an LED
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Great question David! You could use a similar construct, making the time interval be 10ms, where then button press triggers the start of the count. I'll see if we can out a video together on this - thanks for the question!!
@horrorhotel1999
@horrorhotel1999 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 жыл бұрын
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 жыл бұрын
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.
@JohnSmith-lp5en
@JohnSmith-lp5en 2 жыл бұрын
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!
@Khan_Rahman
@Khan_Rahman 4 жыл бұрын
why cant every tutorial be like this. Love it best
@ankushchauhan5752
@ankushchauhan5752 3 жыл бұрын
Thank you sir i really appreciate that....😎😎
@johnrees44..G4EIJ
@johnrees44..G4EIJ 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
@programmingelectronics
@programmingelectronics 4 жыл бұрын
Excellent! Great to hear John!
@jesus2639
@jesus2639 5 жыл бұрын
Thanks you make it so easy to learn
@programmingelectronics
@programmingelectronics 5 жыл бұрын
Thanks - much appreciated!
@pedrofelipebezerra4491
@pedrofelipebezerra4491 3 жыл бұрын
The best explanation I've ever seen about millis()
@P44L
@P44L 3 жыл бұрын
true
@philipeanthonybattung3860
@philipeanthonybattung3860 2 жыл бұрын
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
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Great! Glad it helped!
@pixies4ever324
@pixies4ever324 8 ай бұрын
haha.. being mean with the carrot and millis() function. love it.
@programmingelectronics
@programmingelectronics 8 ай бұрын
Thanks so much for watching!!
@davidsvarrer8942
@davidsvarrer8942 Жыл бұрын
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.
@AbhishekKanti
@AbhishekKanti 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².💪
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Glad it helped :)
@lowiehojas5525
@lowiehojas5525 3 жыл бұрын
"4 Billion ang some change" :D
@HeadakusMaximus
@HeadakusMaximus Жыл бұрын
Would using a modulus comparison rather than subtraction be a bad thing to do?
@programmingelectronics
@programmingelectronics Жыл бұрын
I think that's a great idea. I really ought to use the modulus more often.
@DesertVox
@DesertVox 5 жыл бұрын
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 жыл бұрын
@ 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.
@romycruz4498
@romycruz4498 4 жыл бұрын
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.
@ChandanKumar-kr2kj
@ChandanKumar-kr2kj 2 жыл бұрын
Great video. Thanks a lot for making this video.
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thank you! Thanks so much for watching!
@omkarbansode6305
@omkarbansode6305 8 ай бұрын
thankyou so much ,it was a really big help
@programmingelectronics
@programmingelectronics 8 ай бұрын
Great to hear!
@annie_obi
@annie_obi 10 ай бұрын
I found your video very helpful. Thank you!
@programmingelectronics
@programmingelectronics 10 ай бұрын
Thanks so much for watching Anita!
@nanditofernandeshornay8967
@nanditofernandeshornay8967 2 жыл бұрын
Sr. please make the code bellow becomes millis: Void loop () { if (digitaRead (tombol)==0){ digitalWrite (led, HIGH); delay (3000); digitalWrite (led, LOW); delay (3000); digitalWrite (led, HIGH); delay (3000); digitalWrite (led, HIGH); delay (3000); } else { digitalWrite (led, LOW); } } How to be version millis Sr..???.
@shaswot
@shaswot 4 жыл бұрын
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...
@programmingelectronics
@programmingelectronics 4 жыл бұрын
Glad it helped!
@adadmohamedcherif192
@adadmohamedcherif192 4 жыл бұрын
Thanks, very clear
@emmanuelegel21
@emmanuelegel21 4 жыл бұрын
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!
@joelvale3887
@joelvale3887 5 жыл бұрын
To me this was like listening Chinese
@programmingelectronics
@programmingelectronics 5 жыл бұрын
我知道..这东西可能很疯狂!
@RixtronixLAB
@RixtronixLAB 11 ай бұрын
Nice info, well done, thanks for sharing it :)
@programmingelectronics
@programmingelectronics 11 ай бұрын
Thanks for watching!
@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; (place in one line ) 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.
@fullbridgerecrifier
@fullbridgerecrifier 4 ай бұрын
Thank you, That saved my life ❤
@programmingelectronics
@programmingelectronics 4 ай бұрын
Glad it helped!
@raafathabeeb6718
@raafathabeeb6718 3 жыл бұрын
It was very useful information thanks
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Glad it was helpful!
@ashwinpavithran8976
@ashwinpavithran8976 4 жыл бұрын
Serial.print("Clear explanation, Thank you");
@DrRusty5
@DrRusty5 2 жыл бұрын
Took a while to get millis() working in my code but so much better than using delay.
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Nice!
@DrRusty5
@DrRusty5 2 жыл бұрын
With millis() the serial data continues to be read, where previously it was clear to see on the Serial Monitor that delay had simply frozen it dead for the length of the delay.
@sugengutomo9147
@sugengutomo9147 3 жыл бұрын
What would happen if currentTime has reached the maximum 4B ??
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Great question! It starts over at 0. Which is called "rolling over".
@sugengutomo9147
@sugengutomo9147 3 жыл бұрын
@@programmingelectronics If currentTime would be 0, how about previousTime ?? I think previousTime still equals 4B even currentTime is rolledover to 0 ,, it means currentTime - previousTime = 0 - 4B = -4B ,, cmiiw
@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; }
@georgemugala4830
@georgemugala4830 2 жыл бұрын
Best explanation ever on millis. Thanks alot bro
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thanks! Glad it helped!
@vjdav6872
@vjdav6872 4 жыл бұрын
Very clear explanation - I like that. I have taught some stuff in my one past life...Good job!
@j.w.8663
@j.w.8663 9 ай бұрын
@<a href="#" class="seekto" data-time="331">5:31</a> Aah, I see what you did there: The Neutron Dance! 😆
@ebeb9156
@ebeb9156 3 жыл бұрын
Mmm what should i do to prevent my loop stop running right after 50 days? Cuz my millis() will overflow where is previous time will be lets say 4e9. I can think about abs() function. But is there any more elegant solution? Maybe reset once in a while millis() ?
@maxabeles
@maxabeles 3 жыл бұрын
Simply grand.
@wetsox278
@wetsox278 3 жыл бұрын
Is there an operational penalty doing currentTime - previousTime >= eventInterval where eventInterval = 1000 vs currentTime - previousTime > eventInterval where eventInterval = 999? Same goes for just putting 999 in the statement instead of using a variable?
@Simoooooooonn
@Simoooooooonn 3 жыл бұрын
Great video
@programmingelectronics
@programmingelectronics 3 жыл бұрын
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.
@mridulsarkar9015
@mridulsarkar9015 3 жыл бұрын
Very nice explaination. Thanks
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Glad you liked it! Thanks for watching!
@peterchrien
@peterchrien 2 жыл бұрын
the only problem I see is when it gets to owerflow, it will be like xyz - 4B > 1000? so I assume after reaching maximum previous time should be reseted to 0, right?
@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.
@hanac5586
@hanac5586 3 жыл бұрын
Thank you so much 🤍 + extra points from ice ice baby
@shrikantnikam2426
@shrikantnikam2426 4 жыл бұрын
Is it possible to make delay function in separate tab and use millis single line command like delay? For make code more simpler
@gianysabelsulla5558
@gianysabelsulla5558 3 жыл бұрын
Can i use switch button to control the millis? I used millisDelay. Like whenever i push on the button, the the LED will turn on and automatically off when millis end.
@deviid1857
@deviid1857 3 жыл бұрын
Hai... What's the difference with : if(millis()%1000) Shorter code.
@plugnugget3672
@plugnugget3672 Жыл бұрын
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
@programmingelectronics
@programmingelectronics Жыл бұрын
Appreciate that - thanks for your generosity J.T.!
@skhumbuzocele1330
@skhumbuzocele1330 2 жыл бұрын
... "Black hole can alter space time"
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Something about space always amazes me...
@ruwansamanpriya6085
@ruwansamanpriya6085 5 жыл бұрын
Wow.. That was the greatest tutorial I have ever seen on youtube...Thank you...
@programmingelectronics
@programmingelectronics 5 жыл бұрын
Glad you enjoyed it!
@neelmehta100
@neelmehta100 4 жыл бұрын
I like to imagine the millis being waves in the ocean and we are the Surfers trying to ride those waves.
@mykolasbauza
@mykolasbauza 3 жыл бұрын
Thank you for a wholesome explanation! Great stuff!
@hobarey5029
@hobarey5029 5 жыл бұрын
very informative...thanks a lot...
@lucienramono
@lucienramono 3 жыл бұрын
so nicely explain thank you :)
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Thanks for watching!
@reylepiten3722
@reylepiten3722 4 жыл бұрын
Thanks for this great explanation..it really helps a lot...
@Namai.
@Namai. 2 ай бұрын
very well explained, thx
@programmingelectronics
@programmingelectronics 2 ай бұрын
Glad it was helpful! Thank you for watching!
@johnmitchell4836
@johnmitchell4836 4 жыл бұрын
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!
@omkarkhade4602
@omkarkhade4602 3 жыл бұрын
You are my favourite teacher. Thanks a bunch 😀
@miftahfarid6112
@miftahfarid6112 Жыл бұрын
Millis just for looping.. Not for trigger push button and timmer end for trigger 🗿
@madhukeshnp
@madhukeshnp 4 жыл бұрын
The best explanation ever... the carrot example was just epic... every noob can understand..... thank you so much for the great tutorial.
@programmingelectronics
@programmingelectronics 4 жыл бұрын
Glad it helped!
@attalaw36
@attalaw36 4 жыл бұрын
thank you
@stevenstraker5105
@stevenstraker5105 2 жыл бұрын
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 2 жыл бұрын
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!
@JeThKo_MSW
@JeThKo_MSW 3 жыл бұрын
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!
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Thanks a ton for watching - I am glad it helped!
@simplyjust-6912
@simplyjust-6912 2 жыл бұрын
Tyyyy this was such a good video 😄
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Glad you liked it!! Thanks so much for watching!
@vasha1548
@vasha1548 3 жыл бұрын
love it
@ytfp
@ytfp 2 жыл бұрын
<a href="#" class="seekto" data-time="132">2:12</a> I think this should be it starts at zero and ends at 4,294,967,295
@michaelg6327
@michaelg6327 2 жыл бұрын
GOD BLESS YOU! You saved me from days of suffering! You are my Hero!!!
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Glad it helped!
@xsirfr1958
@xsirfr1958 2 жыл бұрын
What happens after 49 days? It seems it could get stuck, and the if statement never becomes true again.
@Darkstar327
@Darkstar327 2 жыл бұрын
Hi
@fcsbeast4164
@fcsbeast4164 9 ай бұрын
we should update previous time in every second.yes or no
@drdentin3215
@drdentin3215 2 жыл бұрын
Would you get stuck in a never ending loop in 49 days when the millis resets?
@triodit
@triodit 3 жыл бұрын
Why is your constant an Unsigned Long. it doesn't change it doesn't need to me long OR unsigned....
@ModelSceneryTutorials
@ModelSceneryTutorials 3 жыл бұрын
So where is the free course?
@programmingelectronics
@programmingelectronics 3 жыл бұрын
You can find it here: www.programmingelectronics.com/arduino-crash-course/ Hope this helps!
@ultraman9083
@ultraman9083 3 жыл бұрын
Can you teach me how to code with ultrasonic sensor (detects object and motor switch on after 5sec the motor turn off)?
@Sparky5
@Sparky5 2 жыл бұрын
I'll just use a timer library.
@programmingelectronics
@programmingelectronics 2 жыл бұрын
That's a great option!
@CommenterTheMan
@CommenterTheMan 3 жыл бұрын
This is very informative
@programmingelectronics
@programmingelectronics 3 жыл бұрын
Glad it was helpful!
@amajunrieldamalan2387
@amajunrieldamalan2387 2 жыл бұрын
what if you want to set for example a bulb to turn on every 1 minute then turn off 1 minute after turning on? please help me
@antmof94
@antmof94 2 жыл бұрын
whats the point of assigning a variable to millis rather than just calling millis?
@epartsacc
@epartsacc 2 жыл бұрын
This is the best "millis()" tutorial I have ever seen. Thanks so much for explaining everything clearly.
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Glad it was helpful! Thanks so much for the note!
@jameskalenga7546
@jameskalenga7546 4 жыл бұрын
😄 The millis code was smart. I love that logic
@conversationwithgod272
@conversationwithgod272 4 жыл бұрын
Really a great video.great people behind this Job...The visuals make everything so simple.thanks millions
@programmingelectronics
@programmingelectronics 4 жыл бұрын
Really appreciate that! Thanks!
@Sarvesh1110
@Sarvesh1110 5 ай бұрын
What happens when current time reaches 4 billion
@ensarija
@ensarija 2 жыл бұрын
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 2 жыл бұрын
Cool! Thanks for sharing that code!
@AdemOmerovicBA
@AdemOmerovicBA 2 жыл бұрын
Just one correction, define prevTime outside the function.
@cedbarrera7539
@cedbarrera7539 2 жыл бұрын
how can i use this for loop that needed a delay to display on the 7 segment a countup to 10;
Doing multiple timed things with Arduino: Unleash the millis()!
12:25
Programming Electronics Academy
Рет қаралды 124 М.
Arduino millis() function: 5+ things to consider
12:49
Programming Electronics Academy
Рет қаралды 140 М.
ОТОМСТИЛ МАМЕ ЗА ЧИПСЫ🤯#shorts
00:44
INNA SERG
Рет қаралды 4,7 МЛН
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 12 МЛН
规则,在门里生存,出来~死亡
00:33
落魄的王子
Рет қаралды 25 МЛН
Brawl Stars Edit😈📕
00:15
Kan Andrey
Рет қаралды 58 МЛН
Arduino MASTERCLASS | Full Programming Workshop in 90 Minutes!
1:25:31
Programming Electronics Academy
Рет қаралды 2,5 МЛН
How to Use Millis to Master Arduino Multi-tasking
50:17
Rachel De Barros
Рет қаралды 55 М.
I used to hate QR codes. But they're actually genius
35:13
Veritasium
Рет қаралды 943 М.
Optimizing Arduino Code: no setup(), no loop() ⛔
9:27
Wokwi
Рет қаралды 204 М.
How to do multiple tasks in Arduino | Beginners | millis() function
10:23
Using tabs to organize code with the Arduino IDE
12:18
Programming Electronics Academy
Рет қаралды 58 М.
#BB11 Create an Arduino Library😨 - A Real World Example (Easy)
25:54
Port Register Control | Increase speed of Read/Write - Arduino101
15:22
ОТОМСТИЛ МАМЕ ЗА ЧИПСЫ🤯#shorts
00:44
INNA SERG
Рет қаралды 4,7 МЛН