Error Message - Not declared in this scope and button issue.

Hi all,

I hope you don't mind a few questions.

I got the Arduino UNO a week ago and am enjoying it so far but have come up against a couple of problems I cant figure out.

Below is the code I have for the setup of a basic traffic light sequence that I started from a tutorial. I modified it to add the walk/dont walk lights and converted the sequence to match UK traffic lights.

.(Original tutorial Tutorial here)

The sequence works fine and looks good.

The problem is the error shown in the code below when following instructions to add a button:

void setup(){
const int red = 13;
const int yellow = 12;
const int green = 11;
const int walk = 10;
const int nowalk = 9;
const int button = 2; // switch is on pin 2
int buttonValue = 0; // switch defaults to 0 or LOW

pinMode(button,INPUT);
buttonValue = (digitalRead, button);
digitalWrite(green,HIGH);
digitalWrite(nowalk, HIGH);
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
pinMode(walk, OUTPUT);
pinMode(nowalk, OUTPUT);
}
void loop(){
if(buttonValue = HIGH)    //buttonValue was not declared in this scope. <--------
changeLights();
delay(1000);

How do i fix this error/what have i done wrong? (hours of googling and tinkering and I'm stumped.

Next issue:

I have the button wired up as shown in the tutorial posted above but if i connect the trigger wire the sequence triggers and runs indefinitely until I disconnect the wire. Could this be because of the error in the code?

I have confirmed the button is push to make a connection via a continuity test, the connection is made when pressed and broken when released.

When the trigger wire is disconnected, The lights will not run continuously but will seemingly run randomly. I can also trigger the sequence by just touching the wire (as it the board is reacting to my own electrical field?)

Does the IDE interact with the UNO while connected to the Laptop in a way that could trigger the lights?

Any help would be highly appreciated.

Thanks

Dave

first . . .
if(buttonValue = HIGH) //buttonValue was not declared in this scope. <--------
You want == , not =

second . . .
Your global declarations should not be done in setup(), but a level above.
eg
int buttonValue = 0; // switch defaults to 0 or LOW

if(buttonValue = HIGH)
:wink:
if(buttonValue == HIGH)

You need global variables defined outside of loop() and setup()

buttonValue = (digitalRead, button);

This is not the correct syntax

Try

buttonValue = digitalRead(button);

Then your other pb is that all your variables are declared in setup, so are local to that function and invisible in the loop. Try moving them outside setup, at the beginning of your program

const int red = 13;
const int yellow = 12;
const int green = 11;
const int walk = 10;
const int nowalk = 9;
const int button = 2; // switch is on pin 2
int buttonValue = 0; // switch defaults to 0 or LOW

void setup(){
....

Things should be better ( with the == instead of =)

This may help to understand scope rules.

scope

Thank you guys for the quick replies.

J-M-L, I had changed the syntax you mentioned after posting, but moving the variables out of setup solved the scope error.

(it also created another but I found the issue on that one)

groundfungus, Thanks for the link, I will have a read :slight_smile:

Thanks all.