Pages: [1]   Go Down
Author Topic: Problem with DO...WHILE command - Expected Primary Expression Query  (Read 1270 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,
Having a bit of a problem getting my program to compile. It is a simple Ped Xing program but I have tried to use the do...while command to reduce the amount (number of lines) of code needed for a simple operation.

I am getting the fault - expected primary expression before ')' token - when trying to compile the script but cannot find how to assign a value or reference to the "while" command (if that makes sense).

Would really appreciate any help on this as it has both my tutor and myself stuck. The program will be run on breadboard when completed.
The notes within the code are just for myself as I have gone along


Code:
/* This program is for a simple traffic light sequence
with a push button and signal for pedestrian crossing */

int ledRed = 12;
int ledYellow = 11;
int ledGreen = 10;
int pedRed = 9;
int pedGreen = 8;
int pushBut = 2;
//These lines set the address on the Arduino board

/*This code states if items on breadboard are input or output*/
void setup(){
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(pushBut, INPUT);
}
void loop(){
  digitalWrite(pedRed, HIGH);
  digitalWrite(ledGreen, HIGH);

  if (digitalRead(pushBut) == HIGH)//Ped button pushed, LED's "DO" sequence "WHILE" lights do another sequence
 do {
  
//This is where I am up to, need to define parameters for DO and While commands
 delay(3000);
 digitalWrite(pedRed, LOW);
    digitalWrite(pedGreen, HIGH); //Could put another delay in after this line to help with timing
delay(1000);
    pulseIn(pedGreen, HIGH, 3000);
 digitalWrite(pedGreen, LOW);
 digitalWrite(pedRed, HIGH);
                              /*Leaving a space here could help make different parts of code clearer */
 }

 while();
delay(1000);
 digitalWrite(ledGreen, LOW);
 digitalWrite(ledYellow, HIGH);
delay(1000);
 digitalWrite(ledYellow, LOW);
 digitalWrite(ledRed, HIGH);
delay(1000);
 digitalWrite(ledRed,LOW);
    pulseIn(ledYellow, HIGH, 3000);
 digitalWrite(ledYellow, LOW);
 digitalWrite(ledGreen, HIGH);


return;
}
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 76
Posts: 2247
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The while part requires a statement that evaluates to true or false, to continue or exit the loop respectively.

for a never ending loop for instance:

Code:
do{

}while(true);

Maybe you want to remove the if above the do and use it as the continuation.

Code:
do{

}while(digitalRead(pushBut) == HIGH);

Although that will always run once before it checks the button, a simple while checks before starting.

Code:
while(digitalRead(pushBut) == HIGH){

}
« Last Edit: November 12, 2012, 07:34:34 pm by pYro_65 » Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while();

I am getting the fault - expected primary expression before ')' token - when trying to compile the script but cannot find how to assign a value or reference to the "while" command (if that makes sense).

Would really appreciate any help on this as it has both my tutor and myself stuck.

Get a different tutor. If your tutor can't figure out what's wrong with that line of code, they are not competent to tutor you on a coding project.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would this help?

http://arduino.cc/en/Reference/While

Lefty
Logged

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

You know what?
I think this is a perfectly valid question.

Consider:
Code:
while () {
  //do something
}
and
Code:
for (;;) () {
  //do something
}

Both should be equivalent, if what we're told about the equivalency of "for" and "while" loops.
One compiles, the other doesn't, but only by some odd quirk of convention.
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.

Offline Offline
Newbie
*
Karma: 1
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the advice, it confirms what I was thinking. So my next question is then, if I make my "while (true)" statement to be whilst pedGreen = HIGH;

Would this then follow for both the "digitalWrite" and "pulseIn" parts of the following code?



Please can you explain further how use of "for" and "while" commands may help the program to run when "do and while" commands do not?

TIA
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50923
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Both should be equivalent
I don't see that.

while ()
{
  //do something
}
and
for (;;)
{
  //do something
}
are equivalent, but the added () in your example is wrong, as far as I can see.
« Last Edit: November 14, 2012, 05:33:36 am by PaulS » Logged

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

Code:
while {
}
is still syntactically incorrect, but
Code:
for(;;)
is not.
In one example, an empty expression is assumed to be true always, but in the other, it is not.

OP
Quote
Please can you explain further how use of "for" and "while" commands may help the program to run when "do and while" commands do not?
A "for" or a "while" loop may never execute the body of the loop, but a "do...while" will always execute the body of the loop at least once.
« Last Edit: November 14, 2012, 05:38:24 am by AWOL » 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.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In one example, an empty expression is assumed to be true always, but in the other, it is not.

My guess is that due to the flexibility of the for loop, the syntax was chosen to allow each of the elements to be optional. This is unusual, and not something that is allowed for boolean expression in general. For example, this doesn't compile:
Code:
if()
{
}

Missing out expressions from the for loop is slightly unusual but an experienced programmer should not be surprised to see it.

Leaving out expressions in while, if statements and so on is obviously and glaringly wrong. An experienced programmer seeing that there is an error on that line should immediately realise what is missing, even without the compiler error message pointing to the parenthesis and saying "Hey, you left something out HERE".
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
the syntax was chosen to allow each of the elements to be optional.
I've got no problem that you can miss out an expression, I was just commenting that a missing expression in one particular case should evaluate to "true" is quirky.
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.

Offline Offline
Newbie
*
Karma: 1
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I have updated my code and it now compiles... The problem now is that it seems to be ignoring my first two digitalWrite commands and is then getting stuck in a indefinite loop between the 2 Ped Led's flashing as per the sequence I stated. None of the other LEDs are coming on and there is no interrupt when I press the push button...

Any clue as to how I can correct my code?

Thanks

Code:
 
  digitalWrite(pedRed, LOW);
  digitalWrite(ledGreen, HIGH);
 
}

void loop(){
  if (digitalRead pushBut == HIGH) > 5000)//Ped button pushed, LED's "DO" sequence "WHILE" lights do another sequence
 
void digitalRead
 do {
   
//This is where I am up to, need to define parameters for DO and While commands
 delay(3000);
 digitalWrite(pedRed, LOW);
    digitalWrite(pedGreen, HIGH); //Could put another delay in after this line to help with timing
delay(1000);
    pulseIn(pedGreen, HIGH, 3000);
 digitalWrite(pedGreen, LOW);
 digitalWrite(pedRed, HIGH);
                              /*Leaving a space here could help make different parts of code clearer */
 }

 while(digitalRead(pushBut == HIGH));
delay(1000);
 digitalWrite(ledGreen, LOW);
 digitalWrite(ledYellow, HIGH);
delay(1000);
 digitalWrite(ledYellow, LOW);
 digitalWrite(ledRed, HIGH);
delay(1000);
 digitalWrite(ledRed,LOW);
    pulseIn(ledYellow, HIGH, 3000);
 digitalWrite(ledYellow, LOW);
 digitalWrite(ledGreen, HIGH);


return;
}
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see this compiling:
Code:
  if (digitalRead pushBut == HIGH) > 5000)//Ped button pushed, LED's "DO" sequence "WHILE" lights do another sequence
 
void digitalRead

Could you post the whole sketch (that does compile).
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50923
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are your switches wired? snippets-r-us.com is down the internet a ways. Here, we get to see ALL of your code, after you make sure it compiles.
Logged

Pages: [1]   Go Up
Jump to: