I was wrong about millis: how to write non blocking code: (Arduino Uno Programming for Beginners)

preview_player
Показать описание
I was wrong about millis: how to write non blocking code: (Arduino Uno Programming for Beginners)
In this video I really show you how to write non blocking code using millis. How you handle the overflow correctly.

0:00 intro
0:23 millis basics
2:23 non blocking code
5:25 talking about my mistakes
8:49 better way of using millis
10:01 exercise
11:36 outro
Рекомендации по теме
Комментарии
Автор

Very nice video. Also not just rerecording and only showing the correct code, but also talking about why the old code is wrong is a perfect solution. Guys can learn much more by such a code review. Maybe as a task for every viewer: rethink why the old problems do not appear anymore. Think exactly about the 2 situations that lead to the old bugs. The first one is easy to explain. The second one is more difficult to explain.

Volker-Dirr
Автор

Very helpful lesson. Thank you for sharing and correcting your mistake.

JageeAgain
Автор

Your new code will work just fine! However, if you don't fancy waiting almost 50 days for a rollover, do this In setup to adjust the millis counter.

extern unsigned long timer0_millis;
unsigned long untilOverflow = 1500;

noInterrupts();
timer0_millis = - untilOverflow;
Interrupts();

Now wait 1500 milliseconds and the millis count will overflow.


Cheers,
Norm.
(Author of Arduino Software Internals and Arduino Interrupts)

NormanNodDunbar
Автор

Great Thank u for sharing! Well done mate - NEW SUB 🙏🏻

shakejones
Автор

Its very good to know how the nuts and bolts of this stuff works so you know how to properly use it and know it's advantages. Now I'll shamelessly plug the NoDelay library that helps you use millis for timing without having to write that many lines of code yourself. Great for multiple timing loops

mrasrrcom
Автор

Blocking code is fine in a setup or a one time run. In a game or in data gathering, you get in trouble. The delay(n) can be needed with specific issues, but you can branch out with "modes" if one thing goes against another. A digital watch is a good example of modes.

elektronkim
Автор

I'm happy my head can content this. Very expensive. Keep it up

petersontechnicalconcept
Автор

This is one of the best videos on youtube! Congrats!

yoyohuba
Автор

Hello sir, how to execute four parameters on LCD at the same time using millis function ?

akash
Автор

Thanks. Now the LED starts off and it's take 1 sek to light up. If I want the LED to start at once and then starts to blink. I tried to move the !-sign first but it's not accepted. Can you suggest something?

larsniklassonhede
Автор

Hi, I have 2 blocking coded that both have delay
I have a servo code that goes from 30 to 60 and from 60 to 30 with a delay of (15) speed back an forth. And also a DF-Mini player that has delays..

I'm not to familiar with Arduino so am needing some assistance

cadillacescalade
Автор

There is actually a simple solution to this issue. If you make sure all the variable involved are Unsigned Long and you check new_time - old_time to get your duration, then if an overflow happens, the calculation will ALSO overflow, giving you the proper duration. Hope it helps. ^, .'., ^
Edit: I had the variable in the wrong order and corrected them.

glowpon
Автор

Will the code cause both LEDs to change, out of time, on the ~50day overflow ?

philtaylor
Автор

This would be better if you did direct but manipulation to remove all the overhead of the digitalWrite function.

REG
Автор

Before you deploy this to your nuclear power plant, what happens when currentMillis overflows and is less than the storedTimeStamp?

cchstechguy
Автор

You need to create a class so you can make timers without writing more code that is the same for each now timer.

longdarkrideatnight
Автор

// Blink an LED on PA2 on an ATTiny1614 MCU

// Pin definitions
// Important - always use the full pin definition i.e. PXn where X is the port letter and n the pin number
const int ledPinRed = PIN_PA2;// Pin 2 Port A
const int ledPinBlue = PIN_PB1;// Pin 1 Port B
// Variable definitions
unsigned int A = 888; //dividend
unsigned int B = 444; //divisor
unsigned int C = 666; //dividend
unsigned int D = 222; //divisor

void setDefaultPinStates() {
pinMode(ledPinRed, OUTPUT); //set as output
pinMode(ledPinBlue, OUTPUT); //set as output
digitalWrite(ledPinRed, HIGH); //start with LED off
digitalWrite(ledPinBlue, HIGH); //start with LED off
}

void setup() {
setDefaultPinStates();
delay (1000);
}

void loop() {
/*
Flash the LEDs on PA2 & PB1 with a single line of code.
The remainder of millis divided by A is compared to B
and when greater the result is 1 and when less is 0, this binary result
is written to the appropriate ledPin
*/
digitalWrite(ledPinRed, millis() % A > B);
digitalWrite(ledPinBlue, millis() % C > D);
}

Steven_Bennett_YT
join shbcf.ru