do..while

I am reading an RTC that controls a stepper driven clock and I want to pause the clock at a given time.
As a test run, I have the program set to loop out of the 'move hands' routine when the RTC reads 20 seconds. This works fine.
I want the hands to start moving again at 30 seconds.
I have tried the following but it just sticks in an endless loop with the seconds advancing beyond 30

do { //read RTC}
  Serial.print(second);
  while (second <= 30);
}

I have tried the following but it just sticks in an endless loop with the seconds advancing beyond 30

That's because your while clause is in the wrong place.

A do/while statement is fairly rare. A while statement is far more common. Unless you understand the difference, and have a good reason for using do/while, use while.

I have tried just while with the same outcome

Sorry, correction

The serial.print in the above code reads 20 and hangs

do { //read RTC}
  Serial.print(second);
  while (second <= 30);
}

Inside of that loop you do not actually read the RTC.
Unless an interrupt sets the value of "second", second will not change.

You want the do-while loop here. It lets you read the RTC before making the while() compare. If you use a while loop, you need to make a read before the while loop and a read inside of it as well.

do 
{ 
  static unsigned long lastSecond;

  second = some.kind.of.read RTC;
  if ( second > lastSecond ) // keep from printing the same seconds over and over, keep from overflowing the serial output buffer  
  {
    lastSecond = second;
    Serial.print(second);
  }
}  while (second <= 30);

PaulS:
That's because your while clause is in the wrong place.

A do/while statement is fairly rare. A while statement is far more common. Unless you understand the difference, and have a good reason for using do/while, use while.

You got it to be kidding, right?
Yet another nonsense , offensive to recipient " Unless you understand the difference" , and unsubstantiated , "advise".

Perfect example for "do {process} while" is to guarantee at lest ONE pass thru the process.

GoForSmoke:

do { //read RTC}

Serial.print(second);
 while (second <= 30);
}




Inside of that loop you do not actually read the RTC.

I am confused why I am not reading the RTC. The serial.print advances the seconds from 20 but carries on past 30

Post the actual code you are using, not some useless snippet.

The while loop goes after the do loop. Try googling your issue first though

B1Gtone:
I am confused why I am not reading the RTC. The serial.print advances the seconds from 20 but carries on past 30

You don't actually read the RTC because the do while loop only runs once. When you had the 20 seconds there, it must have lined up properly to "work"

void PAUSE_CLOCK_HANDS() {
 int second;
 while (second <= 30) {
   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":"); 
Serial.println(second);

 }

}

This reads the RTC and outputs the time to serial.print

The time reads and advances correctly but the seconds advance past 30 without exiting the while loop

"second" is declared twice, that ain't gonna work.

Perfect example for "do {process} while" is to guarantee at lest ONE pass thru the process.

And the one pass may be one too many. There is nothing that a do/while can do that a while can't do, and the while will be sure to NOT iterate at all, if doing so is inappropriate.

Danois90:
"second" is declared twice, that ain't gonna work.

Please explain?

If I dont put "int second;" then the compiler says "second was not declared in this scope"

The do while loop was fine, but just had a syntax error. Idk what the first kid was talking about. Using the while loop, it will only loop to 29 anyway.

B1Gtone:
Please explain?

If I dont put "int second;" then the compiler says "second was not declared in this scope"

Because you are trying to read second before you declare it, simply move the byte.... line outside the while loop.

 int second;
 while (second <= 30)

How do you know what the initial value of second is?

I have tried that but the clock sticks at 20 seconds

Try rewriting the same do loop you had at the top, moving the while loop to the end (like the stuff I have) and moving the byte line above the do while loop.

You need to move "second" out of the while loop:

void PAUSE_CLOCK_HANDS() {
 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
 do {
   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
   Serial.print(hour);
   Serial.print(":");
   Serial.print(minute);
   Serial.print(":"); 
   Serial.println(second);
 } while (second <= 30);
}

B1Gtone:

void PAUSE_CLOCK_HANDS() {

int second;
while (second <= 30) {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);

}

}




This reads the RTC and outputs the time to serial.print

The time reads and advances correctly but the seconds advance past 30 without exiting the while loop

You declare second as an int and then as a byte.
Which one is the RTC read stuffing data into?
Which one is being compared to 30?

Try moving this line

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

above the while and getting rid of

int second;

You can't be ambiguous with the compiler, it will give unexpected results.

Thank you Danois90 and GoForSmoke

That is where the problem was. I had not realised that byte declared 'second'

All working now :slight_smile: