was not declared in this scope

Hi. I recently received my arduino kit with different components so I am experimenting with using a button to control an LED. Here is my code below with my error that I have been getting a lot lately; this code and others.. THANKS!

Ethan :slight_smile: :slight_smile: :slight_smile:

const int LED = 13;
const int button = 12;
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(button, INPUT);
}
void loop()
{
  digitalRead(button) = buttonValue;
  if (buttonValue == 0(LOW))
  {
    digitalWrite(LED, LOW);
  }
  else if (buttonValue = HIGH)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    // do nothing
  }
}

Arduino: 1.6.5 (Mac OS X), Board: "Arduino/Genuino Uno"

buttonandled.ino: In function 'void loop()':
buttonandled:10: error: 'buttonValue' was not declared in this scope
buttonandled:11: error: expression cannot be used as a function
'buttonValue' was not declared in this scope

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

You haven't declared the variable buttonValue at all.

You cannot expect this to work:

  digitalRead(button) = buttonValue;

Maybe you want

  buttonValue = digitalRead(button) ;

But if you haven't declared buttonValue anywhere, you need to declare it:

  boolean buttonValue = digitalRead(button) ;

The message is pretty self explaining.... The variable "buttonValue" is never declared. Befor you can use a variable you must declare it. You tell the compiler that it excists and what it is. Like you do with LED and button.

I did some editing. Please read all my comments :wink:

const int Led = 13;
const int ButtonPin = 12;
/*
byte is better
use thisIsAVaraible for normal varaibles and
ThisIsAConstant for const varable
*/

void setup()
{
  pinMode(Led, OUTPUT);
  pinMode(ButtonPin, INPUT);  
  /* 
  I hope you have a pull down connected to the pin. Otherwise
  use pinMode(ButtonPin, INPUT_PULLUP) and connect the ButtonPin between the pin and GND
  */
}
void loop()
{
  bool buttonValue = digitalRead(ButtonPin);
  //if (ButtonPinValue == 0(LOW)) // Does not work, use or LOW or 0
  if(buttonValue == LOW)
  {
    digitalWrite(Led, LOW);
  }
  /* not needed, if not LOW then is MUST be HIGH
  else if (buttonValue = HIGH)
  {
    digitalWrite(Led, HIGH);
  }
  */
  else
  {
    digitalWrite(Led, HIGH);
  }
}

Also, depending on your hardware, you'll probably need to debounce the button press and incorporate a delay.
This won't work as you expect with just a simple pushbutton.

For now it does exactly the same as connection the push button to the LED. (Because then it will also bounce :smiley: )

But yeay, if you want to do more with the button you need some debouncing (Google it :wink: ). Easy way? Use the Bounce2 library. Does all the heavy lifting for you :wink:

septillion:
For now it does exactly the same as connection the push button to the LED. (Because then it will also bounce :smiley: )

But yeay, if you want to do more with the button you need some debouncing (Google it :wink: ). Easy way? Use the Bounce2 library. Does all the heavy lifting for you :wink:

Of course you're right, with the current code. My bad. I was sort of assuming that there would be more done later on than was shown, or that it was simplified for the post. Not good to make assumptions like that. The code shown might be all that's intended.

Ok. Thanks guys! @septillion and @OldSteve and @MarkT . I will google how to debounce the program and also include a delay, finally add boolean where I state what buttonValue is along with flipping it around, then say how it worked Thanks!

Ethan :slight_smile: :slight_smile: :slight_smile:

Please, leave the delay!

And only Google what switch bounce and debounce is. To use it in your program simply use the Bounce2 library. Wayyyyyy easier because all the heavy lifting is fine for you. You even don't need a bool to save the state, just a library object and you are done!

septillion:
Please, leave the delay!

And only Google what switch bounce and debounce is. To use it in your program simply use the Bounce2 library. Wayyyyyy easier because all the heavy lifting is fine for you. You even don't need a bool to save the state, just a library object and you are done!

I used a bad word. When I said delay, I didn't mean using delay(). I meant a delay period.

@EQOxx123, a library is one way of doing the debounce. But doing it yourself is a very good learning exercise and will teach you a lot about using millis()-based timing. Your choice.
Personally, I wouldn't dream of using a library for something like that without first thoroughly learning how to do it myself.

Ok. First, no mater what I will research debouncing and bouncing a program but what is a delay period? THX!!

ETAHN :slight_smile: :slight_smile: :slight_smile: