Go Down

Topic: Problem regarding the use of the function millis() (Read 184 times) previous topic - next topic

mayur63690

//the code is used to know the time elapsed for a communication project.
//It doesn't get compiled and uploaded.

Code: [Select]

int period = 1000;
unsigned long time_now = 0;
 
void setup() {
    Serial.begin(9600);
}
 
void loop() {
    time_now = millis();
   
    Serial.println("Hello");
   
    while(millis() < time_now += period){
        //wait approx. [period] ms
    }
}

pert

Your code doesn't make any sense. Please study this tutorial to learn how to do it correctly:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

DrAzzy

Your code doesn't make any sense.
This.

First, using a while() loop to wait until millis gives the right time defeats the whole purpose.

Secondly, the += will increase time_now by period every pass through the while loop test.

There is a specific way to use millis() correctly - the guide per linked describes it.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Go Up