Pages: [1]   Go Down
Author Topic: why don't 'while' work?  (Read 778 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

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
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 344
Just dip it in mercury and power it up
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24423
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3113
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#define EVER   (;;)

for EVER {
...
}


Logged

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).

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley

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.
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SOLVED!

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
SOLVED!
Always got to know why/how...
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  }   

  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.
Logged

Pages: [1]   Go Up
Jump to: