loading

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

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

Jayefuu5 years ago

It should be:

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


Myidentityisasecret (author)  Jayefuu5 years ago
Thanks it worked
verence5 years ago
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.
kelseymh5 years ago
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.