why don't 'while' work?

Hi,

In my sketch the while loop don’t work.
This is my code:

unsigned long time;
unsigned long sampletime;
int delaytime;

void setup() {
  Serial.begin(9600);
  Serial.println("CLEARDATA");
}

void loop() {

  //DIT GETAL WIJZIG JE ALS JE DE MEETFREQUENTIE WIL VERANDEREN

  delaytime = 500;
  sampletime = 3000;



  int sensorValue1 = analogRead(A0);

  do  {
    time = millis();  
    Serial.print("DATA, ,");
    Serial.print (tijdstap);
    Serial.print (",");
    Serial.println (sensorValue1);

    delay(delaytime);
  }   

  while (time < sampletime);
}

This code takes a sensorvalue and put it, whit a variable delaytime, serial to excel (plx-daq).
But I even want to setup the totaltime it does.
So, I want to setup a sampletime. In this case for 3 seconds.
The serial communication only have to work this time.
Thereafter serial.prints must stop.
But in this sketch, the serials go on and go on…

Why?

Gr.
Johan

Without looking too carefully...

Because after your do...while loop finished, the loop() function executes again, resets the variables, and re-starts the do-while. If you want it to stop, you have to give it something else to do, at the end of loop(), like "while (1) ;" (spins tightly forever)

It's a little bit strange command 'while (1)'. But: it works! Fine. Thank you.

But is there no anymore goodly solution for this case? Whitout a 'nothing-doing-command' ?

Gr. Johan

you could move your while loop in to the setup code, as it is intended to run once only... then you can leave the loop empty or do some other useful work

Setup runs only once after reset of the arduino and is the right place for code that runs only once.

D.

ut is there no anymore goodly solution for this case? Whitout a 'nothing-doing-command' ?

No, not really - the processor has to do something, even if that is just sitting in a tight loop with no option of breaking out. The only other option is to put the processor into a low-power mode.

jmnijsse: It's a little bit strange command 'while (1)'. But: it works! Fine. Thank you.

But is there no anymore goodly solution for this case? Whitout a 'nothing-doing-command' ?

Gr. Johan

You could go for

#define EVER   (;;)

for EVER {
...
}

:)

One thing: Since i coded the do-while loop, the sensorvalue remains the same. Only at the first sample it takes the new value.

When i remove the do-while loop, its oke.

What happened?

J.

SOLVED!

SOLVED!

Always got to know why/how...

Sorry. Here’s the solving code.
I have to readout the sensorvalues in the do-loop instead of the beginning of the void loop().

unsigned long time;
unsigned long sampletime;
int delaytime;

void setup() {
  Serial.begin(128000);
  Serial.println("CLEARDATA");
}

void loop() {

 

  delaytime = 500;      //time between two samples
  sampletime = 5000;    //time for the whole measurement




  do  {
    int sensorValue1 = analogRead(A0);
    time = millis();  
    Serial.print("DATA, ,");
    Serial.print (delaytime);
    Serial.print (",");
    Serial.println (sensorValue1);

    delay(delaytime);
  }   

  while (time < sampletime);
  while (1);
}

Gr.
Johan

  }   

  while (time < sampletime);

The while clause generally goes next to the closing brace of the do statement, to distinguish it from a while statement.

If you want to do something for a fixed time every time the Arduino is reset, you should put that code in setup(), and leave loop() empty.