while loop skipping

hello,
i have an issue with while loop

Serial.println("Started");
  waitUntil=millis();
  do
  {
  encoderWrite(1, 1500);
  encoderWrite(2, 1110);
  encoderWrite(3, 1980);
  Serial.println("sending stuff..");
  }
 while(millis()-waitUntil<=4000);
  Serial.println("Done");

so sometimes it works fine, does loop for 4 seconds and writes values fine. but sometimes in serial monitor it prints “Started” and immediately after it “Done” (yeah it prints sending stuff for just one time because it’s do while loop) without looping for 4 seconds, without printing Sending stuff… it’s weird :astonished: this millis() while loop works perfect with led blinking, i tested it.

Hard to say with only this much code, but if I were to guess it would be that waitUntil is declared as an int and not a long.

We don't post this stuff for nothing.

Hard to say with only this much code, but if I were to guess it would be that waitUntil is declared as an int and not a long.

Make that unsigned long not long.

Mark

TanHadron:
Hard to say with only this much code, but if I were to guess it would be that waitUntil is declared as an int and not a long.

thanks, this was an issue, waitUntil was int type and now it works fine when i changed to long :slight_smile: