mishap_n
mishap_n

Reputation: 618

Arduino While loop and if statement logic

Looking for some help with some loop logic. Right now, this just gets stuck in a while loop but doesn't break. Mostly just curious, but how could I do this so the Serial write continues to just write every time, but the lightThis() function is only called once?

void loop(){
    if ( (x >= 245 && x <= 260) && (accelDifference > STAB_THRESHOLD)){
    // write a command to serial
    Serial.print("p1stab");

    // trace this for now
    Serial.print("\t");
    Serial.print(x);
    Serial.print("\t");
    Serial.print(y);
    Serial.print("\t");
    Serial.print(z);
    Serial.print("\n");

    // keep the LED solid 
    while(true) {
        lightThis();
        if (false){
            break;
        }
    }

    } else {
        Bean.setLed(0, 0, 0);
    }

}

Thanks!

Upvotes: 0

Views: 1395

Answers (1)

ergonaut
ergonaut

Reputation: 7057

Assuming you are calling loop() somewhere else in your code, you could call lightThis() at that location, and track that it's been called only once.

boolean doneOnce = false;
for (...){
   loop();
   if (!doneOnce){
     lightThis();
     doneOnce = true;
   }
}

Excuse the code, I'm not sure what you're coding with, but you get the idea.

Upvotes: 1

Related Questions