Pages: [1]   Go Down
Author Topic: Global variable "not declared in this scope"  (Read 1800 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm only used to programming things in PHP, so I understand there are differences.  I don't understand why, no matter where I declare this variable, I keep getting the same errors.

My entire program so far is as follows:

Quote
int greenPin = 11;
int redPin = 10;
int switchPin = 9;
int lightState;

void setup()
{
  pinMode(greenPin,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(switchPin,INPUT);
}
//int lightState;
int lightDisplay(lightState){
  
  switch (lightState)
  {
    case 1:
    digitalWrite(greenPin,HIGH);
    digitalWrite(redPin,LOW);
    break;
    case 2:
    digitalWrite(redPin,HIGH);
    digitalWrite(greenPin,LOW);
    break;
    case 3:
    digitalWrite(redPin,LOW);
    digitalWrite(greenPin,LOW);
    break;
  }
}

void loop()
{

  if (digitalRead(switchPin) == HIGH)
  {
    lightDisplay(3);
  }
  else
  {
    lightDisplay(1);
  }
}


All I want to do is be able to call the lightDisplay() function and tell it whether to light a green LED, a red LED, or nothing at all.  Seems like this would be easier than rewriting the digitalWrite() statements over and over.

Anyway, when clicking the Verify button, I get this:

sketch_apr01a:-1: error: 'lightState' was not declared in this scope
sketch_apr01a:12: error: redefinition of 'int lightDisplay'
sketch_apr01a:-1: error: 'int lightDisplay' previously defined here

lightState is declared at the top of the code so I don't understand how this can be happening.  Am I using the wrong datatype or something?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int lightDisplay(lightState){
Does lightState have a type?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can get rid of it at the top of your code.  You just need to 'declare' it in your function definition

Code:
void lightDisplay(int lightState){

And make it a void function because it returns nothing.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you.  I'm not used to having to declare variables that way, so no wonder I couldn't figure it out.  Declaring the variable inside the function worked great.
Logged

Georgina Ontario
Offline Offline
Sr. Member
****
Karma: 5
Posts: 437
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a reference section for this idea of scoping!
http://www.arduino.cc/en/Reference/Scope

It is worth the time to read and understand.

Global variables should generally be avoided.

Local variables are better.

Except for the exceptions -- but that is the advanced class.
Logged

Just another Hacker

Pages: [1]   Go Up
Jump to: