Do{}While (); Doesn't work ??

Hi

I want a waiting loop until msCpt>=msTmp. (msCpt is incremented by interrupt)
I tried with an empty Do in a Do...While.

But it only works when i add something in Do. Not only a simple opération like x=x+1 but a more complex one such as Serial.print...

Do you have any idea?
Merci !


do{
Serial.print(ID); // required for functional Do While ???
}
while (msCpt<msTmp);

The compiler will recognize that an empty "do {} while();" accomplishes nothing and it will remove it altogether.
However, it should depend upon the values of msCpt and msTmp. If msCpt<msTmp is true it is equivalent to while(1) and your code should hang there. If msCpt<msTmp is false then the loop won't do anything useful if it is empty.

Pete

I would like to know if there is a way to turn off code optimisation like that. Other than declaring the variable as volatile.

Oh, I missed "msCpt is incremented by interrupt".
Have you declared msCpt to be volatile? If not, the loop will be optimized away. But if is declared volatile, the loop should be compiled in.

Pete

There is no advantage to using "do {} while ();" where "while();" will work.

Try just:    while (msCpt<msTmp) /* DO NOTHING */;

You might have made a mistake somewhere else in your sketch. If you want anyone to find the mistake it is best to include enough of a sketch so that it actually compiles.

Thanks for your reply.

Here an extract of my code :
With "While problem" at lines 75 & 103

/* trying to synchronise with RTC module
 * Questions about While... 
 */

// DECLARATIONS POUR MODULE RTC
// Fonctions utiles : rtc.getTimeStr() rtc.getDateStr()
	#include <DS3231.h>
	DS3231  rtc(SDA, SCL);

// DECLARATION DES MESSAGES TEXTES
	const char ID[]="< Test While >: ";

// TIMER 0 = FONCTIONS MILLISECONDES
	// DECLARATION DES DONNEES REGISTRE POUR TIMER 0
	// Timer utilisé les fonctions de temporisation  
	// En mode CTC à 1ms
	// Clk/64 prescaler, compte jusqu'à 249
	//  
	const byte TCNT0_INIT = 0x00;        
	const byte TCCR0A_INIT = 0b00000010;    // CTC Mode, timer /64
	byte TCCR0B_INIT = 0b00000011;          // CTC Mode, timer /64
	const unsigned int OCR0A_INIT = 249;    // Match A value 
	const unsigned int OCR0B_INIT = 0xFF;   // Match B value (non utilisé)
	const byte TIMSK0_INIT = 0b00000010;    // Interruption activée sur Compare Match A


//DECLARATION DE VARIABLES
	unsigned long msCpt=0, msTest=0, msTmp=0, msTmp2=0;
		
void setup() {
	// Serial com
		Serial.begin(57600); // initialise
		delay(20);
		Serial.println(" ");
		cli();

	// Timer 0 Init
		Serial.print(ID);
		Serial.print("Initialisation du Timer 0 ...");
		TCNT0  = TCNT0_INIT;
		TCCR0A = TCCR0A_INIT;
		TCCR0B = TCCR0B_INIT;
		OCR0A  = OCR0A_INIT;
		OCR0B  = OCR0B_INIT;
		TIFR0  = 0xFF;
		TIMSK0 = TIMSK0_INIT;
		Serial.println("Done");
	
	sei();	

	// RTC module Init
		rtc.begin();

  
	//synchronize msCpt with RTC module	
	uint8_t tmp1,tmp2;
	Time rtcTime;
	rtcTime=rtc.getTime();
	tmp1 = rtcTime.sec;
	do{
		rtcTime=rtc.getTime();
	  tmp2 = rtcTime.sec;
	}
	while (tmp1==tmp2);				// repeat until seconds change
	msCpt=0;
	msTest=0;
}

void loop() {

	for(uint8_t x = 1;x<=100;x++){
		msTmp=100+msTest;		
		do {
			msTmp2=msCpt;
			Serial.print("");			// /!\ Doesn't work without
		}
		while (msTmp2<msTmp);
		msTest=msTmp2;
		Serial.print(ID);
		Serial.print("Synchro=");
		Serial.print(x);
		Serial.print(" - ");
		Serial.print(rtc.getTimeStr());
		msTmp=(msCpt/100)%10;
		Serial.print(".");
		Serial.print(msTmp);
		Serial.print(" - ");
		Serial.print(msTest);
		Serial.print(" - ");
		Serial.println(msCpt);
	}	
 
}

// FUNCTION 0 "millisecondes"
	ISR(TIMER0_COMPA_vect){
		msCpt++;                    // add 1ms
  }

void wait(unsigned long Value){
		unsigned long waitValue = msCpt+Value;
		while (msCpt<waitValue){
			Serial.print("");           // /!\ Doesn't work without
		}
  }

I hope someone could help me, because i'm lost :wink:

You are missing the "volatile" keyword, as mentioned above.

unsigned long msCpt=0, msTest=0, msTmp=0, msTmp2=0;

Why are you overriding the built in Arduino millis() function?

// FUNCTION 0 "millisecondes"
	ISR(TIMER0_COMPA_vect){
		msCpt++;                    // add 1ms
  }

With Volatile, it works! Thank You...
Now I just have to study what "volatile" means ! :wink:

I override millis() because i want to synchronise with my rtc.
Also, in my project I need to use severals timers and it appears to be a good way to learn how to manipulate them!

Thanks a lot