Compilation Error

Doing my best to start small and it seems I'm even failing at that!

All I wanna do is use a pushbutton to count to 5 and display the results to 3 LED's in a binary form.

int buttonState = 0;
int count = 0;

void setup() { Serial.begin(9600); pinMode(2,INPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); }

void loop() { digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW);

buttonState = digitalRead(2);

if(buttonState == LOW) {

} else { count++; delay(500); Serial.print(count);

if((count == 1)) { digitalWrite(3, HIGH); } }

if((count == 2)) { digitalWrite(4, HIGH); } }

if((count == 3)) { digitalWrite(3,4, HIGH); } }

if((count == 4)) { digitalWrite(5, HIGH); } }

if((count == 5)) { digitalWrite(5,3, HIGH); } delay(500); } }

Here's the error message:

Arduino: 1.8.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

sketch_nov25a:18: error: expected unqualified-id before 'if'

if((count == 3)) { digitalWrite(3,4, HIGH); } }

^

sketch_nov25a:18: error: expected declaration before '}' token

if((count == 3)) { digitalWrite(3,4, HIGH); } }

^

exit status 1
expected unqualified-id before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Apparently it's not as simple as I thought. But ya gotta start somewhere. :slight_smile:

Too many closing braces...

if((count == 1)) { digitalWrite(3, HIGH); } }
// Should be
if (count == 1) { digitalWrite(3, HIGH); }

You should format more carefully; the following snippet is legal, but the "else" clause includes the next 'if" statement, with the extra closing brace finally terminating the "else." I can't tell whether you want the string of "if"s inside the else or not...

if(buttonState == LOW) {
} else { count++; delay(500); Serial.print(count);
if((count == 1)) { digitalWrite(3, HIGH); } }

With formatting:

if(buttonState == LOW) {
 // do nothing
} else {
  count++;
  delay(500);
  Serial.print(count);
  if((count == 1)) {
     digitalWrite(3, HIGH);
  }
}

Put every statement on a different line. Mashing everything together like that just makes your code hard to read and leads to errors like this. Do you notice how much easier it is to understand the structure of the second code westfw posted at a glance?

Hi,

Putting everything on one line does not make the compilation or execution of your code any faster.

By structuring the lines of code and executing a CTRL-T, to auto fromat, you would have been able to see the bracket errors.

Tom.... :slight_smile:

I never used the Auto-Format before. I like that a LOT better! :slight_smile:

I dumped the extra closing braces, split the digitalWrites, and then let the compiler lead me to some other errors .... and we are good to go!

Thanx guys. Karma added. One small step at a time. :slight_smile:

Hi,
Good to hear.
You can refine that code more by giving the pins names, instead of using pin numbers;

int buttonState = 0;
int count = 0;
int ButtonPin = 2;
int ledOnePin = 3;
int ledTwoPin = 4;
int ledFourPin = 5;
void setup() {
  Serial.begin(9600);
  pinMode(ButtonPin, INPUT);
  pinMode(ledOnePin, OUTPUT);
  pinMode(ledTwoPin, OUTPUT);
  pinMode(ledFourPin, OUTPUT);
}

void loop() {
  digitalWrite(ledOnePin, LOW);
  digitalWrite(ledTwoPin, LOW);
  digitalWrite(ledFourPin, LOW);

  buttonState = digitalRead(ButtonPin);

I haven't done all your code but you should be able to see what i have done.
Naming the pins makes reading the code easier, also if you need to move your hardware to different pins, you only need to edit the variable declarations.

Tom... :slight_smile:

I wired it up and it's counting LED's to 5 in Binary, but only one time. I thought it would loop and start over again?

The Serial Monitor OTOH keeps counting 6,7,8, etc.

If I hit the reset we start all over again. That makes sense.

My little UNO Buddy is trying to cooperate. :confused:

So I need to add a reset in there somewhere? I tried a "if (count == 0)" at the end and it did compile, but the LED result went bonkers.

Any suggestions?

Forgot the code.

int buttonState = 0;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop()


{ digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW);

  buttonState = digitalRead(2);

  if (buttonState == LOW) {

  } else {
    count++; delay(500); Serial.print(count);

    if (count == 1) {
      digitalWrite(3, HIGH);
    }


    if (count == 2) {
      digitalWrite(4, HIGH);
    }


    if (count == 3) {
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
    }


    if (count == 4) {
      digitalWrite(5, HIGH);

    }

    if (count == 5) {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);


    }  delay(500);
  }
}

The old grey matter ain't what it used to be but I just realized it is looping. I know this because when I push the button the LED only lights momentarily. The delay 500 keeps it on for a half a second. Then it loops and sits and waits for me to push the button again.

But why does it stop after only one sequence?

You guys are gonna think I'm a nut, :slight_smile: , but the fog is beginning to disperse. The reason it only runs the sequence once is because I haven't reset the counter to zero. My LED's don't know what to do with a "6".

I'll go with that and see what compiles.

Now you are getting the hang of this remove the turged method of codeing you are using and reduce it to a very small amount by using arrays.
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Then your code hasn’t got to know with how to cope with six, it will use the three bits to the maximum extent.