Go Down

Topic: why don't 'while' work? (Read 1 time) previous topic - next topic

jmnijsse

Hi,

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

Code: [Select]

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

westfw

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)

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

ArduinoM

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.

AWOL

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


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

Code: [Select]


#define EVER   (;;)

for EVER {
...
}


Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

jmnijsse

:)

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.


PaulS

Quote
SOLVED!

Always got to know why/how...

jmnijsse

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

Code: [Select]

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

PaulS

Code: [Select]
  }   

  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.

Go Up