2

Code 1 (here unsigned long currentmillis = millis() is in loop area):

const int LED = 2;
unsigned long previousmillis = 0 ;
const long delaytime = 1500;
int ledstate = HIGH;
void setup() {
 pinMode(LED,OUTPUT);
}
void loop() {
 unsigned long currentmillis = millis();
 if (currentmillis - previousmillis >= delaytime) {
 previousmillis = currentmillis;
 if (ledstate == HIGH) {
 ledstate = LOW;
 } else{
 ledstate = HIGH;
 }
 digitalWrite(LED,ledstate);
 } 
}

Code 2 (here unsigned long currentmillis = millis() is above void setup()):

const int LED = 2;
unsigned long previousmillis = 0 ;
unsigned long currentmillis = millis();
const long delaytime = 1500;
int ledstate = HIGH;
void setup() {
 pinMode(LED,OUTPUT);
}
void loop() {
 if (currentmillis - previousmillis >= delaytime) {
 previousmillis = currentmillis;
 if (ledstate == HIGH) {
 ledstate = LOW;
 } else {
 ledstate = HIGH;
 }
 digitalWrite(LED,ledstate);
 } 
}

Code 1 is working and Code 2 is not working. What is the significance of the position of unsigned long currentmillis = millis() here and why?

ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Dec 25, 2021 at 11:42
0

1 Answer 1

4

This is a common misunderstanding for Arduino beginners.

In the second code you do

unsigned long currentmillis = millis();

outside of any function, meaning at global scope. These variables are declared at the very start of the program, even before the initial Arduino stuff is configured (like the configuration of Timer0 for getting millis() to work). The variable will have the value zero. And variables are not magically tied to a source. What you are doing there is a one-time assignment. currentmillis will not have future values of millis(). It will stay the same until you change it in your code. In your second code you do not ever change currentmillis, thus is will stay at the same value forever, preventing the code from working correctly.

In the first code the value is assigned at every loop() iteration, so you can be sure that it has a very up-to-date value of millis() when you use it afterwards.

This coding principle works exactly like you would bake a pizza, regularly looking at the clock to check the time while you are doing other things. With your first code you look very regularly at the clock to not miss the time to get the pizza out of the oven. With the second code you only look once at the clock, even before you have put the pizza in the oven, and then never again; so your pizza will burn.

answered Dec 25, 2021 at 11:59
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.