Skip to main content
Post Undeleted by Tahseen
bug fixed in initilization
Source Link
Tahseen
  • 51
  • 1
  • 6

I was wrong being too confident about my whole code. When people suggested posting my whole code, I should have just done it!!!

Anyway, here is the mistake I made. I initialized the variable flash_time as aan 8 bit-bit integer. OfcourseOf course, 8 bit can only hold up to 255. I am trying to store 9000!!!. The following code has this bug fixed.

const long sleep_interval = 3000; 
const long awake_interval = 250; 
const int ledPin = 3;

const uint16_t flash_time = 9000;  //bug fixed by using uint16_t instead of uint8_t
const uint16_t t1 = 5; 
const uint16_t t2 = 495;   

void setupflash() {
    
  Serial.begin(57600);  
 unsigned long x = pinModemillis(ledPin, OUTPUT);
}

void loop   while(millis() - x < flash_time){ 
    unsigned long x = millisdigitalWrite(ledPin, HIGH);
    while(millis() - x < flash_timedelay(t1){;
        Serial.printlndigitalWrite("flashledPin, time!"LOW);
        delay(t2);
    }   
}   

void setup() {
    digitalWriteSerial.begin(57600);  
    pinMode(ledPin, HIGHOUTPUT);
}

void loop() {
    unsigned long x = delaymillis(t1);
    while(millis() - x < digitalWriteawake_interval){
        Serial.println(ledPin,"flash LOWtime!");
        delayflash(t2);    
    }

    unsigned long y = millis();; 
    while (millis() - y < sleep_interval) {  //sleep_interval = 3 secs
        Serial.println("Sleep time!");
    }
}

So, nested loops that use millis() to determine the terminating conditions work just fine, even if loop 1 runs for 250 ms and loop 2 (that's inside loop 1) runs for much longer, like 9 seconds.

Apologies if I have wasted too much of people's time.

I was wrong being too confident about my whole code. When people suggested posting my whole code, I should have just done it!!!

Anyway, here is the mistake I made. I initialized the variable flash_time as a 8 bit integer. Ofcourse, 8 bit can only hold up to 255. I am trying to store 9000!!!. The following code has this bug fixed.

const long sleep_interval = 3000; 
const long awake_interval = 250; 
const int ledPin = 3;

const uint16_t flash_time = 9000;  //bug fixed by using uint16_t instead of uint8_t
const uint16_t t1 = 5; 
const uint16_t t2 = 495;   

void setup() {
     Serial.begin(57600);  
     pinMode(ledPin, OUTPUT);
}

void loop() {
    unsigned long x = millis();
    while(millis() - x < flash_time){
        Serial.println("flash time!");
              
        digitalWrite(ledPin, HIGH);
        delay(t1);
        digitalWrite(ledPin, LOW);
        delay(t2);
    }

    unsigned long y = millis();; 
    while (millis() - y < sleep_interval) {  //sleep_interval = 3 secs
        Serial.println("Sleep time!");
    }
}

So, nested loops that use millis() to determine the terminating conditions work just fine, even if loop 1 runs for 250 ms and loop 2 (that's inside loop 1) runs for much longer, like 9 seconds.

Apologies if I have wasted too much of people's time.

I was wrong being too confident about my whole code. When people suggested posting my whole code, I should have just done it!!!

Anyway, here is the mistake I made. I initialized the variable flash_time as an 8-bit integer. Of course, 8 bit can only hold up to 255. I am trying to store 9000!!! The following code has this bug fixed.

const long sleep_interval = 3000; 
const long awake_interval = 250; 
const int ledPin = 3;

const uint16_t flash_time = 9000;  //bug fixed by using uint16_t instead of uint8_t
const uint16_t t1 = 5; 
const uint16_t t2 = 495;

void flash() {   
    unsigned long x = millis();
    while(millis() - x < flash_time){ 
        digitalWrite(ledPin, HIGH);
        delay(t1);
        digitalWrite(ledPin, LOW);
        delay(t2);
    }   
}   

void setup() {
    Serial.begin(57600);  
    pinMode(ledPin, OUTPUT);
}

void loop() {
    unsigned long x = millis();
    while(millis() - x < awake_interval){
        Serial.println("flash time!");
        flash();    
    }

    unsigned long y = millis();; 
    while (millis() - y < sleep_interval) {  //sleep_interval = 3 secs
        Serial.println("Sleep time!");
    }
}

So, nested loops that use millis() to determine the terminating conditions work just fine, even if loop 1 runs for 250 ms and loop 2 (that's inside loop 1) runs for much longer, like 9 seconds.

Apologies if I have wasted too much of people's time.

Post Deleted by Tahseen
Source Link
Tahseen
  • 51
  • 1
  • 6

I was wrong being too confident about my whole code. When people suggested posting my whole code, I should have just done it!!!

Anyway, here is the mistake I made. I initialized the variable flash_time as a 8 bit integer. Ofcourse, 8 bit can only hold up to 255. I am trying to store 9000!!!. The following code has this bug fixed.

const long sleep_interval = 3000; 
const long awake_interval = 250; 
const int ledPin = 3;

const uint16_t flash_time = 9000;  //bug fixed by using uint16_t instead of uint8_t
const uint16_t t1 = 5; 
const uint16_t t2 = 495;   

void setup() {
    Serial.begin(57600);  
    pinMode(ledPin, OUTPUT);
}

void loop() {
    unsigned long x = millis();
    while(millis() - x < flash_time){
        Serial.println("flash time!");
              
        digitalWrite(ledPin, HIGH);
        delay(t1);
        digitalWrite(ledPin, LOW);
        delay(t2);
    }

    unsigned long y = millis();; 
    while (millis() - y < sleep_interval) {  //sleep_interval = 3 secs
        Serial.println("Sleep time!");
    }
}

So, nested loops that use millis() to determine the terminating conditions work just fine, even if loop 1 runs for 250 ms and loop 2 (that's inside loop 1) runs for much longer, like 9 seconds.

Apologies if I have wasted too much of people's time.