Doing multiple timed things with Arduino: Unleash the millis()!

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 มิ.ย. 2024
  • 🤩 FREE Arduino Crash Course 👇👇
    bit.ly/get_Arduino_skills
    Want to learn more? Check out our courses! bit.ly/33ceYv4
    Want to do multiple thing with Arduino? Then master the Arduino millis function!
    This video will walk step-by-step through how to use millis with Arduino.
    **Get the code, transcript, challenges, etc for this lesson on our website**
    bit.ly/2WecXIl
    Hello, I hope you’re doing fantastic. In this series, we’ve been talking about using the Arduino millis function to create timed events. Like maybe every three seconds you want a server to move or maybe every minute you wanna send a status update to a web server.
    Whatever it may be, you want something to occur at a timed interval. Now, if you haven’t seen the previous lessons in this series, I highly recommend that after this lesson you go back and check ’em out, I think you’re gonna find them very helpful. Now in this lesson, we are going to cover creating multiple timed events using the Arduino millis function, specifically in this lesson, we are going to map out a program that will execute two independent timed events.
    We’ll code from scratch a program that reads and displays two sensor values at different intervals and finally we’ll drink Kool-Aid and watch the sun go down. Every good program has an even better algorithm to go along with it, so before we start typing away at the Arduino IDE, first I wanna write out our plan of action.
    This is kind of like what we’re trying to accomplish. Okay, I’ve got two sensors. One is a light-dependent resistor or LDR and the other is a temperature sensor. And what I wanna do is I wanna read these values and I wanna display them to the Serial Monitor window on my computer but I don’t wanna do it all the time.
    I want to have it happen at two separate intervals. I want my light-dependent resistor to read and display every second and I want my temperature sensor to read and display every five seconds. So, I’ve got two separate events. So, I’m just gonna lay it out like event one that happens every second.
    I wanna read my LDR and then I wanna display the LDR value and then event two, this is gonna happen every five seconds, I wanna read a temperature sensor and then display that temperature sensor. And I’m gonna display them to the serial monitor window on the Arduino.
    Okay, so this is kind of the plan of action and you know, looking at this, I figure we could use the Arduino millis function to set up the timing for these events, I could use analogRead to read the LDR and the temperature sensor values and then we could use the Serial library to display those values to the Serial Monitor Window on our computer. When you’re creating a program that has repetitive timed events, it also doesn’t hurt to kind of lay out the timing of the events on a piece of paper.
    If we lay this out on a timeline, we can see that we have two events that overlap every five seconds. Luckily for us, we are using the millis function as opposed to trying to make this work with the Arduino delay function, so this should definitely be achievable.
    Now in this lesson, we are focusing on demonstrating how to code timed events with Arduino. So, I’m not gonna go into setting up the LDR and temperature sensor circuits, so just assume that I have them wired up. But if you’re wondering how to do that, definitely check out the ProgrammingElectronics.com website for lessons on setting up different circuits. All right, so let’s go ahead and jump into the Arduino IDE and start coding this thing from scratch.
    So, here we are in the Arduino IDE and I’m gonna start by just making myself a little to-do list in comments, so let me do that. Okay, so I’ve got a little to-do list written out here and I’m gonna kinda work through ’em. I won’t hold myself tight to there but I just thought I’d give it a shot, it might help me kind work through the program.
    The first thing I’m gonna do is set up some pins for where I’ve got my sensors attached, so let me do that. Okay, so I’ve got two constants and I made these constants because they’re not gonna change, these sensors are always gonna be at these pins and I used analog pin A2 and analog pint A4 for the LDR and temp sensor respectively, so that’s pretty straight forward.
    I’ve got my circuit set up there. Actually I’m using Kit-on-a-Shield to kind of work through this program and now what I’m gonna do is set up variables for the timed events so I’ll do that now. All right,
    ...
    **About Us:**
    This Arduino tutorial was created by Programming Electronics Academy. We are an education company who seek to help people learn about electronics and programming through the ubiquitous Arduino development board.
    **We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.**

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

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

    This what you call High Quality Video.

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

    Man this millis series of you saves my life. Thanks a lot.

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

    Really like your videos!! My 12 yr. old and I are learning together. My goal is to totally automate a greenhouse using Arduino. This video is exactly what we need.

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

      Sounds like a cool project! It's great you and your son are learning it together.

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

      I couldn't have said it better.@@programmingelectronics

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

    You're the hero we need.

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

    That was a cool series, I learnt a load of new stuff .. will go and do your full course now I think. Super clear!

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

    the only person that explained millis the right way...thanks bro

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

    Dude I would never have figured this out without you.. thank you

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

    Thank you for the nicely laid out tutorial. You showed a simple but effective way to plan out what to do, and then carry it out. While I have programmed in other environments before, I’m new to the Arduino, and you have made it easy to pick up the new parts. Cheers!

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

    best millis explanation on tintanet understood instanly,, total new to arduino, never done any code, now ive got 6 led running without delay ... thks

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

    Awesome! I find your lectures easy to understand. Thanks!

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

    Absolutely amazing videos, i can do more about millis function by now, thank you for making this videos and keep it up! 😁😁😁

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

    Thank you so much. I’m working on a walking 4 legged robot and I didn’t want to use the delay function. This video saved my butt.

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

    You explained it really well, thank you, it really helped. Thanks for making this and all your videos!

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

    Can't explain how much I love these videos ;~; luv u

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

    Very nicely done with Millis function. Thank you.

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

    Excellent, I really enjoyed the millis series, I hope you can explain state machines in another video using the millis function.

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

    This has to be best described video tutorial I have seen on almost any topic, very clear descriptions and well thought out content. First time visiting and cant wait to learn more from this channel.

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

    Got my first Arduino yesterday. Thought it could be fun to play with a fairly modern system. A lot to learn.
    Made embedded real time multiprocessor systems decades ago. Both hardware and software, Intel 4040, 8051 8085, 80188. and Z80. Assembly & high level languages like PL/M and Modula-2.
    This video made it clear how to structure a small stepper motor project.

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

    Very high quality instruction. Thanks

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

    This is some great stuff. Thanks!!

  • @ThuanDuong-pv3xu
    @ThuanDuong-pv3xu 4 ปีที่แล้ว

    Thank so much for your lensson in my a late night , it is my event !

  • @Robin-mi1zj
    @Robin-mi1zj 3 ปีที่แล้ว +1

    hi! thank you so much for this series (btw I think you forgot to add this video to the playlist lol), it's super clear and your explanation is amazing

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

    Right traight to the point and it works!

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

    Thanks man, amazing video

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

    I saw an example code where someone created an array to hold the millis readings per item you want to manage, and the condition logic was within a function. That way the code was quite minimal. That was in the Doomsday Communicator TH-cam video where he demonstrated building a Lora chat device.

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

      That sounds a lot cleaner, will have to check it out

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

    Thanks Michael. A very well made tutorial and very helpful to me right now as I am writing my first code for an Arduino (aged 63 !) which will strike a bell every hour exactly. I would like to expand the program to move a stepper motor driven hour hand. A stepper motor tutorial would be great ;-). Best regards, Mike

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

    Thanks very much!

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

    Thank you. Just what I needed

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

    Fantastic video. I've been watching the series and hoping you'd cover timed events using the Millis() function. Very difficult to get your head around that function and you have definitely helped.

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

      Thanks Jake - I appreciate that! I often still scratch my head on using it well.

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

      @@programmingelectronics i guess millis still sucks because if you have any delay()s in those timed events your entire timeline will be screwed up. i think to use millis everything has to be perfectly synchronized

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

      @@yashaswikulshreshtha1588 I don't believe delay() effects the background count of the millis() function - millis() will keep counting. You might consider some timer libraries that trigger interrupts at specific times...

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

      @@programmingelectronics I wanted to use millis for timed events but let's say if one event which turns on led has a delay in it then It won't affect millis counts but yes it will act as blocking code for other code pieces sometimes like permanent blockage, you can a event which happens every one second and then an event which happens every 3 seconds, so ofc the first will execute first but if u put delay of 3000 In it then the second event won't happen at all, it never will. I thought maybe I could use millis to run big code blocks having lot of functionalities. At timed intervals

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

      @@programmingelectronics I would be really grateful if you can make video of classes and objects in Arduino. Probably no has explained good on TH-cam, but if u can make it, it will be on top and it will be helpful to all of us, cuz some of us are trying to learn actual programming concepts using arduino

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

    Thank you so much this is the best arduino video

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

    thank you very much
    That was very helpful and clear
    I was struggling to understand the mills function until I saw your videos
    thanks

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

      Glad it helped - thanks so much for watching!
      Timing can be confusing as all get out and it's not always easy to think through, let alone code.

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

    Great lessons; Easy to understand!
    (I'm trying to develop multiple debounce sensors for my model railroading, using millis).

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

      Thanks for the note Larry! Have you checkout Geoff Bunzas TH-cam Channel?
      th-cam.com/channels/KzeYLMEPxWGilqZTNuG0JA.html He uses Arduino in model railroading. Pretty neat stuff.

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

      @@programmingelectronics
      Thanks so much!
      Thanks for the tip.
      I'll check it out.

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

    wow , the quality of your videos have improved alot

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

    If you have 2 events like on this example, but the first one takes a considerable time to execute, because there are many instructions in between, I guess updating the currentTime variable with the newer mills value wouldn't harm and it's actually recommended to do so before every event.

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

    super helpful, thank you!

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

    Wonderful as always...

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

    Well done!

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

    Amazingly taught.

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

    Brilliant tutorial 👏

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

    Just what I was looking for ! I needed to slow down the up and down increment of the set rpm programme on an Arduino that uses a touchscreen display without using the despicable delay function , managed to slot this into one of the if statements inside aviod ReadControls function - works a treat and as this programme is interrupt based ( rpm sensor ) the delay is a big no no . 100 milliseconds did the trick .
    Thank you !

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

    Amazing free content. Very well explained

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

    Reall great.🙌 Thank you.❤

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

    Well explained.

  • @MrAce-rs6ju
    @MrAce-rs6ju 5 ปีที่แล้ว +4

    You are good teacher *****

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

    Amazing. Subscribed

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

    really helpful. thanks a lot.

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

    You saved me thank you!!!!

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

    very nice tutorial!

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

    you're a genius

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

    I like the concept. Just not sure how you handle the rollover of the millis() count? In the reference documentation, millis() will rollover in about 50 days. Seems you should have to check for a rollover condition.

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

    Thank you.

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

    Hey thanks! I didn't think I would need the millis function till I saw your videos about it.
    I'm building a miniature dollhouse that I want to have music and dimmed lights. Using delay would have caused me some problems.

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

    Love ALL your videos. You mentioned one on interfacing the LDR and Temp sensor but couldn’t find it on your site. Could you supply me a link. Does it also deal with scaling analog readings to displayed units? Thanks so much. Wayne

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

    Excellent description. I just wanted to know weather i can use delay function in side the loop without affecting the over all working? Thanks.

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

    Great video and love the humor sprinkled in there. In your closing remarks relative to the IF statement……it would be nice to say you set the first previous time to zero….you did say it in the up front part. That is when “I got it”…..like you said walk thru it.

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

    Can you explain how this method avoids overflowing of a unsigned long variable?

  • @danielbernhard1250
    @danielbernhard1250 10 วันที่ผ่านมา

    Nice Video. Do you wanna explain how to use the actuall timer to do such things faster than every millisecond?

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

    nice, this what i was looking for.
    intuitively I would have updated the previousTime with the interval, instead of the currentTime...
    previousTime_1 += eventTime_1
    but i expect differences to be minimal, a small (yet slowly increasing) delay in the currentTime version compared to my idea...

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

    Great explanation. I was wondering what happens when milis overflows. Would this stop working around day 49?

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

    great!

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

    thank you so much for this useful narration!
    Let's say i want to control the digital output with "millis" function. How do i set the exit working time without stopping the loop or other ?

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

      What exactly do you want to use and for how long? You can use this same sketch he shows but instead of reading values and printing them, you can just set up a digital pin to become high or low. You'll need all the code without the display part for just one digital pin. I might have said it a weird way so if its confusing, ask again.

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

    Can you explane how to use millis() for pausing someting ? Like having a little wait loop for ex. 0.5 seconds before making the next without using delay().

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

    Hi there, your videos helps me a lot since I'm new for this, thank you a lot.
    Quick question about millis(), is it possible to make a wave of leds fading on and off on different LEDs at the same time like for example chroma lighting for keyboards?
    I try the whole week to learn how to create a code for this issue and it's very clear with the delay() function it's impossible to do so. Thank you beforehand!

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

    Nice video. I've got a use case that requires both event-driven and timer-driven functions so this was helpful.
    Regarding your timer update, though, I think I would have updated previousTime_1 & previousTime_2 by ADDING their respective intervals back into them rather than updating to the current time. loop() is subject to blocking and there are other reasons that your loop might lag, so your implementation might creep (e.g. 0... 1003... 2003... 3005... 4006... 5007 vs 0... 1003... 2000... 3002... 4001... 5001). Staying truer to your intervals might be important.

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

    this is wonderful, i would like to program a variable time input so when the button-A is pushed for 1 second something will happen or when button-A is pushed for 2 seconds something else will happen, then maybe a 3rd event? thanks a lot...:)

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

    You are really good. How night celt you explain anybody can understand great job I too started learning don’t be surprised

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

    Great tutorial!!! Two questions 1) the event's happen exactly in one and five seconds?? Instead of const uns... can I put define ...? Thanks

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

    Sir keep making awesome courses . Soon will take your paid course its awesome . Love from india

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

    This vdo is really helpful for me understand to millis function. Just one question sir am using millis function for one LED blink off time and on time is same. How to change off time only means 1000 Ms is off time and on time is 500 ms. I think you you understand my question sir.

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

    Thank you very much for this tutorial. Theoretically soon after millis() overflows, previousTime variable will be larger than currentTime and the currentTime-previousTime will be a large negative value. Will not be this an issue?

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

    There are several event manager and timer libraries for Arduino which are much easier and more powerful than manually writing lots of millis() code.

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

      Very true! It's fun to dive into some details. Do you have any specific libraries you would recommend?

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

    I also like the title from the videos: "Unleash the millis()!" - Awesome! - not just the content :) Thanks very much!

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

    So good! Thank you. Are you guys still uploading new content?

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

      Yes! We have taken a break and been focusing on our member content for quite some time though.

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

    I really need help because i need to submit my assignment in 3-4 more days, how to do it if we want to read button anytime while the sensor display the values every some times? thank you

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

    Veru nice i would like to explain how to setup 8relay depending on reading sensor ac voltage as example

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

      I'm attempting that very thing right now!

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

    What if i want to also print the last update of the lm35 in the serial monitor for example you set a time interval of 1sec in LDR and 5sec in LM35 but what if i want to print the last update of LM35 while the LDR prints simultaneusly

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

    Hi. What about overflow?

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

    Could this be used to call lcd.clear without using delay?

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

    Can you explain how this method avoids overflowing of a unsigned long variable?
    I'm taking 4 readings from 4 different Distance Sensors and lighting up the LED on the basis of output I gathered from each sensor .But the values which is coming from distance sensors are overflowing the functions .
    I appreciate if you help me out.

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

    How large in value can the current time variable grow to before it causes issues in the register?
    It is not a wiser choice to utilize a Count++ function that grows on every loop until it reaches the largest required count and then reset the count to zero and start over?

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

    good to know things are:
    making a variable a constant with "const" will prevent accidentally changing the value later in the program and the compiler will generate an error: "error: assignment of read-only variable 'YourVariable' "
    "long" = -2,147,483,648 to 2,147,483,647
    "unsigned long" = 0 to 4,294,967,295
    unsigned just means that the value stored will never be smaller than 0

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

      Great notes! I think the numbers on long and unsigned long need switched though in your comment:
      "long" = -2,147,483,648 to 2,147,483,647
      "unsigned long" = 0 to 4,294,967,295

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

      @@programmingelectronics yes you are correct i have mistakenly swapped the values ^^

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

    hmm what about pressing a button, is there any delay effect?

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

    sir I have 1 question:
    can we run both tasks at same time means both tasks at per second speed????????

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

    Hello.. can using currentTime_1=millis();, currentTime_2=millis(); ... etc . because if use many library using millis to. sory for my bad english

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

    hi, is there a way to have time events, but when the event happens, let it continue longer.
    For example, I want to turn on LED1 for 21 seconds straight, at the same time, turn on LED2 for 3 seconds then turn off LED2 for 4 seconds, then turn LED2 back on for 3 seconds and turn LED2 off for 4 more seconds, until the 21 seconds is up.

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

      Yes, this is definitely possible. You could potentially break the 2nd LED event into two separate timed events, and set up different event variables for the turning on and turning off of the LED.

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

    Please help me :::::
    I want to start my loop only when my Ultrasound sensor detects the object
    and if the object is not present then it should directly stop all the operations until the ultrasound sensor detect another (different or same) object

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

    9:20 We can write this line as " if(currentTime >= previousTime_1 + eventTime_1_LDR)"
    That way it is easy to understand!

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

      You de best my man!!. What a way to explain things

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

    Can "uint32_t" be use to replace "unsigned long" ?

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

    For everyone worrying about the currentTime rollover after 3 weeks:
    If currentTime is smaller than previousTime, a rollover has happened. Check for this condition and then just set previousTime to zero (basically reset your program this wasy).

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

      The rollover is handled automatically by this code. Do the maths in binary instead of thinking in decimal and you will find it works correctly as is. The key is to subtract the times rather than using addition.

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

    lol how funny is that. I have not long finished writing code very similar to this, except I used a DHT22 and a HC-SR04. I timed three events, temp, humidity and distance (at 5 seconds, 7.5 seconds and 10 seconds). Threw in a millis reading at the end of each loop just to see how often it was going through the loop (and any delay processing). I prefer to update the previous time immediately after the If statement, just in case you are not putting the millis value into a variable like "currentTime" if(millis() - previousTime_1 >= eventInterval_1){ previousTime_1 = millis();

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

      Thanks for sharing that! That makes for more succinct code for sure.

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

      @@programmingelectronics I knew I was on the right track when I saw what you wrote was very similar to what I had come up with. I initially had down what you wrote, but then changed it at the last minute to just tighten it up a little. Doing three different events one after another, I played with both ways. The way you did it might be slightly more memory friendly, the way I did it will keep the timed events slightly more accurate. If we weren't playing around with milliseconds which increment pretty rapidly, I would have normally done it the way you did it. Thinking about it now, I wonder if I can test the value, and pipe it into the variable in the same action to try cut down the time difference even more? Hmmm another thought just occurred to me. If I put it back to the way you have it, and just reset the currentTime before the start of each event it might be even better still.

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

      Yep I think just resetting the currentTime is a winner. That way it isn't so time critical when previousTime_x is updated. Although it is good to get it done out of the way and not forgotten.
      currentTime = millis()
      if(currentTime - previousTime_3 >= eventInterval_3){
      previousTime_3 = currentTime;

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

      Great thought - so adding that currentTime = millis() line before each if condition. Thanks again for sharing!

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

      @@programmingelectronics Yeah I think that is the way to go... You are capturing the value immediately before you do the If statement, so if it goes into the If (i.e the right amount of time has elapsed), you don't have any lag between the millis at the time of checking the If statement, and the time of storing in the previousTime variable.
      I think resetting the currentTime just before the next If can potentially make the difference of it stepping into the If statement and it not stepping in, on projects that require a very minimal delay between checks. If you only set it once and it takes say 50ms to run through the first If statement, in real time the internal counter has increased by 50ms but you are still using the old value.
      By putting a println of millis at the end of each loop, I noted that the average loop time for a non activity loop was about 7ms, but when it did a sensor check and printed to screen it was around 40-45ms. I have a 10ms Delay between LOW and HIGH Pin to get the reading.
      Since I am using 5, 7.5 and 10 second intervals (just random values) some loops will do nothing, some loops one activity, others two, and some three. On the ones that do three, the initial time set with currentTime = millis() will be way out by the time it enters the third If statement.

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

    Hi,
    Please let me know I have 5 events and I want to run them sequentially but 2 events are working in manner but when I add 3rd event the sequence got disturbed.
    unsigned long ct, pt=0, pt1=0, pt2=0;
    void loop() {
    ct = millis();

    if(ct - pt >= 1000)
    {
    lc.setChar(0,0,'a',false);
    pt = ct;
    }

    if(ct - pt1 >= 2000)
    {
    lc.setRow(0,0,0x05);
    pt1 = ct;
    }

    if(ct - pt2 >= 3000)
    {
    lc.setChar(0,0,'d',false);
    pt2 = ct;
    }
    }

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

    Itss showing previousTime is not declared
    When previousTime is normally initialized using unsigned long ,
    It's value is not changing on the execution of 2nd for loop.
    Any solution

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

    Is there a potential problem, when millis() rolls over after 49 days, that the sensor updates to serial could stop when the if statement is not true and is there a way to avoid this happening?

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

      Hi Steve, Great question. I am planning on releasing a video about the roll over (at some point...) In the meantime, you can check out this article on stack exchange arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588 Hope this helps some!

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

      @@programmingelectronics sir please do a tutorial about this rollover problem of millis in a code. It is easier to understand your explaination than reading a book :)

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

      I think with another if statement ; if (previousTime_1 > 49days || previousTime_2 > 49days) {previousTime_1 = 0; previousTime_2 =0}

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

    is there any function called micros? I mean what if I need the values in micro second. Can arduino produce that precise data?

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

      Yes, there is a micros() function as well, that returns the number of microseconds:
      www.arduino.cc/reference/en/language/functions/time/micros/

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

    This is an old video you probably won't respond, but if you see this then...
    I need help creating a photogate I know the equations etc. but I don't want to use an Arduino Zero or buy and RTC module, I have a short deadline for a project so I'm not sure if I have time to buy anything else online. I need the photogate to find the time it was broken then do an if statement like
    /=Divided By
    Time Broken Phototrsnisotr = millis() or something to get time between beam is broken and restored like a stopwatch
    Speed = Time Broken Phototransistor / Object Diameter
    if(Speed == 2 Meters Per Second) {Speed up or something}

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

    So what happens in 49 days when current time rolls over?

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

    Hi, i need a code for a servo motor. Which move 0 to 180 forward and wait 2 hours and again it reverse and wait 2 hours and it is to be continued, can you help me.

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

      I can help you a little, if you need more specification let me know:
      -Do exactly what he did in the video
      -For event 1, change it to your servo doing 0 to 180
      -For event 2, change it to your servo doing 180 to 0
      -You also going to need to think of a way to put a delay for event 2
      -In other words: from start, 2 hours has past (servo do 0 to 180), an additional 2 hours has past (servo 180 to 0)
      -To be more precise, event 1 will START at 2 hours and event 2 will START at 4 hours
      -Their interval will be 14 400 000 millis (4 hours) if i did the math correctly

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

    Sir, can I use milis() if say, I want to analogRead from my sensor every 16 ms? I have project to read voltage and current sensor from charging capacitor which is happend about 800 ms to fully charge. I want to measure 50 data point (AnalogRead()) from my sensor during 800 ms. I really appreciate your help Sir?

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

      Cool project - > Yes, using millis() would allow you to sample something everything 16ms. If you look at the blink without delay example in the Arduino IDE, or watch this series, you should get a thorough idea of how to implement that in code.

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

      @@programmingelectronics Thank you Sir. I am still to follow along series videos that you provide in this channel, Sir. very helpful. Many thanks Sir.

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

    sir i want this code : zero crossing fan speed control and 4 relay control using ir remote controller

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

    Awesome! So... you're obviously not guaranteed that your function is getting called "on time". If your other operations aren't too taxing on the processor, you'd be fine. Maybe it'd be interesting to display the millis or the variance to see how far off it was when the function was executed.