727Views4Replies

Author Options:

Why do I get the error: expected `(' before 'buttonState' in myprogram? Answered

So my code is
int small = 9;
int big = 8;
int buttonPin = 6;

void setup() {
  pinMode(small, OUTPUT);
  pinMode(big, OUTPUT);
  pinMode(2, OUTPUT);
}

void loop() {
  digitalWrite(2, HIGH);
  int buttonState = digitalRead(buttonPin);
  buttonState = map(buttonState, 0, 1023, 0, 1);
  if buttonState = 1 {
    digitalWrite(small, HIGH);
    digitalWrite(big, HIGH);
  }
  else {
    digitalWrite(small, LOW);
    digitalWrite(big, LOW);
  }
}
but for some reason I get that error.
Please reply asap Thanks in advance

4 Replies

user
Jayefuu (author)2011-08-08


It should be:

if (buttonState == 1) {
digitalWrite(small, HIGH);
digitalWrite(big, HIGH);
}


Select as Best AnswerUndo Best Answer

user
verence (author)2011-08-08

Because the compiler expects a '(' before 'buttonState'.
If the compiler wants it, put it there. No need to argue.

And by the the way, it probably should be: "if (buttonState == 1) ..."
Double "=" instead of one as this is a comparison and no assignment.

Select as Best AnswerUndo Best Answer

user
kelseymh (author)2011-08-08

This is the C programming language. Expressions in if statements must be enclosed in quotes.

If you have never programmed in C before, you probably want to get yourself a copy of K&R.

Select as Best AnswerUndo Best Answer