wonderful millis timing video... now to use it with 3 switches time short, time long 6 actions... thanks a lot...:)
@socalledengineer5301
Жыл бұрын
Thank u so much I was facing this problem due to dilay
@didiersee
6 жыл бұрын
Very good but could you make demonstration with arduino uno please?
@roadeycarl
7 жыл бұрын
Antony this is really helpful! I understand the concept of now but I am still struggling to use it in a practical application - especially to use it as a replacement for Delay. For an example - I want an output to go high 2 Seconds after it the input threshold was reached... // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(13,OUTPUT); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage); if (voltage >= 4.0) { delay(2000); // This is the point the output should wait 2 seconds AFTER the voltage is >=4.0 , then pin 13 goes HIGH - // but using Millis!!! digitalWrite (13,HIGH); } else { digitalWrite (13,LOW); } } Thanks for talking the time out to make these videos, once again, they are very helpful!
@AntonyCartwright
7 жыл бұрын
Hi, I think you want this: unsigned long triggerSetMillis= 0; // Time to turn on! bool triggerSet = false; //Is there an imminent turn on time? // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); pinMode(13,OUTPUT); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage); if (voltage >= 4.0) { if (!triggerSet) { //If there is currently no trigger set triggerSetMillis = millis() + 2000; //Set trigger time to now + 2 sec. triggerSet = true; //Yes, there is a trigger set. } else { if (millis() => triggerOn){ //It's time to turn on! //You need something here to test if already HIGH. digitalWrite (13,HIGH); //Turn on. } } } else { digitalWrite (13,LOW); triggerSet = false; //Terminate any triggers. triggerSetMillis = 0; //Reset trigger. } }
@AntonyCartwright
7 жыл бұрын
Code completely untested, and not compiled! I did it quickly in notepad, so please test it! Lol
@AntonyCartwright
7 жыл бұрын
I think the nature of timing in the Arduino is difficult to understand, but try to think of it like a person who is constantly checking the clock every 1 sec waiting for the time when he has to start a task. It's kind of like that. The delay function is like where a person freezes completely and his brain is totally paused!
@roadeycarl
7 жыл бұрын
Thanks Antony, Yeah I get that now, thanks to your last video! That code doesn't quite work - I've been working it for ages and the "delay" is something I've found very useful to prevent that stage when two voltages are so similar that the Arduino doesn't know if it's high or low... a couple seconds of delay is a good way to stop the relay chatter! on my last project I ended up using the Delay function because I couldn't understand millis... I'm really trying to not let it beat me this time - you know what its like!!
@AntonyCartwright
7 жыл бұрын
Carl Bartlett The code I sent will be somewhere near. See if you can finish it off. I understand the chatter. I get it too with different things. In my solar panel system, I get the relays turned on when there is over 5000 lux. But sometimes light can wobble around a certain value! I haven't addressed it yet, but I will do at some point soon. The way I'll address it is to block state changes for an amount of ms after each change... If last change < millis()+timeout then block, else, make the change...
@salc9593
7 жыл бұрын
Thank you Antony for posting this video on Managing Arduino Timing: Delay, Millis & Micros, it's been helpful to me as I am a Neophyte to Arduino and Coding. I would like to know if it is possible to Fade/Ramp up and down a single LED while the a couple other LEDs are blinking at different rates & times. Complements to your video I am able to blink several LEDs at different rates and times. Thank you for any help or direction you can point me to. Thanks again for your instructional video.
@AntonyCartwright
7 жыл бұрын
Hi Sal, thanks for your comment. You can fade in/fade out an LED in two common ways: You can use something called PWM, or you can use a capacitor. PWM basically switches the LED on and off with extreme speed, and you can trick the human eye into thinking that it is fading - I have a video on that in a playlist. If you wish to take the capacitor route, you'd basically parallel a capacitor in with the LED. Capacitance is measured in Farads, and 1F is equal to 1 Coulomb for 1 Second. 1C is equal to 1 Volt and 1 Amp for 1 Second. An LED is around 3V, 0.02A. So, to hold for 1 Second, you'd need 0.06C, or 0.06F. You'd probably need to hold it for 1ms in order to get the fade effect, so 0.06F / 1000 = 0.00006F, which is 60uF. So if you paralleled a 60uF capacitor in with your LED, you should get a fading effect! The capacitor will act as a mini battery!
@salc9593
7 жыл бұрын
Thank you for your direction Antony... I will look up the Video on your playlist and let you know how it works. Thanks again for your help and direction.
@gary14004
6 жыл бұрын
Hi Anthony, good explanation, what is the max number the Millis counter will reach before it resets?
@AntonyCartwright
6 жыл бұрын
Gary Wessel Hi Gary, I believe it is 4,294,967,296. (2^32)
@Mr.Leeroy
5 жыл бұрын
instead of storing "lastRun", calculate and store "nextRun" at the end of run() to avoid the sum of millis() and "interval" in every single loop()
@AlienRelics
5 жыл бұрын
I came to comments to say this.
@AlienRelics
5 жыл бұрын
Your method will have a creeping error. Rather than making lastrun = millis now, use lastrun = lastrun + interval and at worst, there will be a little uncertainty but it will not accumulate the error.
@Omarbg95
6 жыл бұрын
i have a question what happens if different task need the same time? for example: you have 2 IR sensors sensing different objects, say the first one needs to be read at 20s and the other one too, would this this affect the arduino?
@AntonyCartwright
6 жыл бұрын
Hi Omar, in this case, in the loop, you'd simply group both of the reads together into the same block of code... So for example; Is it 20 sec yet? No! Is it 20 sec yet? No! Is it 20 sec yet? No! Is it 20 sec yet? No! Is it 20 sec yet? YES!!! Read sensor 1.. Read sensor 2... DONE. Is it 20 sec yet? No! Is it 20 sec yet? No! Like that...
@Omarbg95
6 жыл бұрын
thanks for answering, greetings from mexico!
@AntonyCartwright
6 жыл бұрын
@@Omarbg95 thanks. :)
@mugzwood
6 жыл бұрын
I ran a code for a project running a clock from a stepper motor. I modified it to "aLastRun += aInterval;" in order for it to be more accurately. Now I'm having all sorts of problems when it gets to 32 seconds. any ideas?
@AntonyCartwright
6 жыл бұрын
Hi, it depends on your exact code, but on the face of it, you're trying to say that your last run was in the future! Last run + some time doesn't make last run, it makes the last time something was run, plus an interval to make a future time. Antony...
@mugzwood
6 жыл бұрын
Sorry I wasn't very specific. I mean that I replaced "aLastRun = millisNow' with "aLastRun += aInterval;" after the if statement. So on the first run threw on your sketch it would be 0 += 250 = 250. at around 251 milliseconds or something real time. Thanks for the reply and video by the way, I had come across millis before but didn't understand it at all but had a feeling it would be useful.
@AntonyCartwright
6 жыл бұрын
@@mugzwood why would you want to change millisNow to interval? I'd say it'd not recommended because last run is supposed to be accurate. :)
@shashank_1180
5 жыл бұрын
What if i want to add delay of one minute?? delay(60,000); is it correct ??
@AlienRelics
5 жыл бұрын
60 seconds in a minute, so yes. But your Arduino will be paused and do nothing, not respond to anything during that delay(60000). Note: do not use a comma.
@shashank_1180
5 жыл бұрын
@@AlienRelics thank you 😃
@soufidz800
4 жыл бұрын
What I should do if I want to delay by 10 minute? please help me urgently I'm confusion😖
@AntonyCartwright
4 жыл бұрын
Delay(600000);
@soufidz800
4 жыл бұрын
Can I get your mail please to explaine my issue?
@JDI4DAVID
6 жыл бұрын
Does the lastrun need to be an unsigned long int for the comparison to work?
@AntonyCartwright
6 жыл бұрын
David Tucker Unsigned long, yes.
@JDI4DAVID
6 жыл бұрын
Antony Cartwright I found that comparisons have to be between identical types. The IDE doesn't warn the user if apples are being compared with oranges.
@maneeshmondkar1562
6 жыл бұрын
Thank you for the post, How can we use millis function in for loop..? Can you please help me with that?
@AntonyCartwright
6 жыл бұрын
Inside the for loop, you'd probably say: If millis()> time to do a certain task then do the task. This will allow you to perform a task a certain amount of times at certain intervals. :-)
@maneeshmondkar1562
6 жыл бұрын
Thanks alot Antony for your guidance.. I'll try it and let you know..
@AntonyCartwright
6 жыл бұрын
No worries...
@maneeshmondkar1562
6 жыл бұрын
Hello Antony, I worked on your given suggestion, I think i have some understanding gap and so its not working for me. I want counter to decrement every second with using milis function, meanwhile counter gets decrement till Zero arduino will able to take input from the sensors/pushbutton. Thank You in advance.. :) // unsigned int interval = 1000; unsigned int millis_now = 0; int lastrun = 0; void setup() { Serial.begin(9600); } void loop() { int incomingByte = 0; millis_now = millis(); incomingByte = Serial.read(); if (incomingByte == 'i') { for (int a=5; a >=0 ; a--){ if (millis_now > (interval+lastrun)){ Serial.println(a); } } lastrun = millis_now; } }
@AntonyCartwright
6 жыл бұрын
Hi, sensors aside; you'll want to do something like this: (Not compiled or tested) const int interval = 1000; unsigned long millisNow; unsigned long lastCheck; const int timerStart = 5; int timerRemainingSeconds; void loop() { if(millisNow>lastCheck+interval){ //Check is due! timerRemainingSeconds--; if(timerRemainingSeconds
@jyothsnareddi9921
7 жыл бұрын
Thank you so much. It is really helpful.
@AntonyCartwright
7 жыл бұрын
Thank you! More videos to come!
@korishan
7 жыл бұрын
Another nice video. Came in at about the time when someone was asking about this over at diypowerwalls. Now I can post the video for the explanation :) Like how you did miss a beat when the little-un started makin a fuss. Must be used to it by now, eh? :)
@AntonyCartwright
7 жыл бұрын
Hey Korishan, thanks for your comment! :-) Haha! I'm used to it, yes! Sometimes I have to stop recording and restart, but most of the time I can carry on. Now, I try to record all of my videos at night time when they are in bed though!
@avejst
7 жыл бұрын
Thanks for sharing 😀👍
@AntonyCartwright
7 жыл бұрын
Thanks!
@MRNOTE145
6 жыл бұрын
thanks
@k44d1
6 жыл бұрын
Good explanation mate, but please get a different pen because this is causing me pain!!!!
@AntonyCartwright
6 жыл бұрын
Haha! Ok!
@neagoecatalin879
6 жыл бұрын
The video was more than useful but, man, how many freaking ads it has???
@AntonyCartwright
6 жыл бұрын
It should have three of those little bottom ad things?
@riseoftech4k332
7 жыл бұрын
Cool man!!! im making videos like these.. could you come and check them out? thanks
Пікірлер: 62