Not declared in this scope.

Why doesn't my code work?

int ledPin = 3;
int buttonPin = 8;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

I get the error:

sketch_oct28a.ino: In function ‘void loop()’:
sketch_oct28a:10: error: ‘buttonState’ was not declared in this scope

Just lick it says it has not been declared.

Mark

Try this:

int ledPin = 3;
int buttonPin = 8;
int buttonState = 0;   //Declaration added as without it this sketch will not compile.
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

I'm not sure why holmes4 wants it licked, but the declaration I've added should make it work.

Thanks :slight_smile: That worked.

but the declaration I've added should make it work.

Why is a variable used only in loop() global?

PaulS:
Why is a variable used only in loop() global?

Because I was lazy... It needn't be, it could be declared just before it's used, but that does mean writing it as zero every time the loop goes around. That said in a sketch of this size it makes no difference, and I did double check it against the official example in the IDE.

but that does mean writing it as zero every time the loop goes around.

What?

  byte buttonState = digitalRead(buttonPin);

The variable is local, and assigned a value on every pass through loop().

That is cool... and it is assigned the correct value on every pass... I still stand by my other line of defence that I just copied the declaration from the official example! :wink:

PaulS:
What?

  byte buttonState = digitalRead(buttonPin);

The variable is local, and assigned a value on every pass through loop().

It should be a bool.

DigitalRead will only ever return HIGH or LOW. No need for a whole byte.

bool buttonState = digitalRead(buttonPin);

While you are correct if one reads the code, the documentation only guarantees that digitalRead(...) will return a byte containing LOW or HIGH.

If we have to read the code, why bother with the documentation?

it could be declared just before it’s used, but that does mean writing it as zero every time the loop goes around.

See static in the ref.

Mark

KenF:
It should be a bool.

DigitalRead will only ever return HIGH or LOW. No need for a whole byte.

bool buttonState = digitalRead(buttonPin);

Strictly speaking it should be an int to match the prototype:

int digitalRead(uint8_t);
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println (sizeof (int));
  Serial.println (sizeof (byte));
  Serial.println (sizeof (bool));
  }  // end of setup

void loop () { }

Output:

2
1
1

Changing from byte to bool doesn't save anything.

Why was the prototype defined as an int? Even a whole port only needs a byte.

I am just a pawn in the game of Life. I know not of what you speak.

KenF:
It should be a bool.

DigitalRead will only ever return HIGH or LOW. No need for a whole byte.

bool buttonState = digitalRead(buttonPin);

But a boolean variable takes up a byte anyway. Furthermore, strictly speaking HIGH and LOW are not equivalent to true and false either because it depends on how they are defined. As it happens they are equivalent and that is not likely to change.