Go Down

Topic: Global variable "not declared in this scope" (Read 7432 times) previous topic - next topic

discofreakboot

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?

AWOL

Code: [Select]
int lightDisplay(lightState){

Does lightState have a type?
"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.

maniacbug

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

Code: [Select]

void lightDisplay(int lightState){


And make it a void function because it returns nothing.

discofreakboot

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.

WillR

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.
Just another Hacker

Kkarthik

guys can you tell me to fix this skecth

#include <AFMotor.h>
#define trigPin 3
#define echoPin 2
#motor1(1, MOTOR12_8KHZ); // create motor #1, 8KHz pwm
#motor3(3, MOTOR34_8KHZ); // create motor #3, 8KHz pwm

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor 1.setSpeed(255); // set the speed to 200/255
  motor 3.setSpeed(255); // set the speed to 200/255
}

int CheckDistance()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
  return distance;
}

void MotorForward(int delaytime)
{
  motor1.run(FORWARD);
  motor3.run(FORWARD);
  delay(delaytime);
}
void MotorBackward(int delaytime)
{
  motor1.run(BACKWARD);
  motor3.run(BACKWARD);
  delay(delaytime);
}
void MotorRelease()
{
  motor1.run(RELEASE);
  motor3.run(RELEASE);
  delay(1000);
}

void MotorLeft()
{
  motor1.run(FORWARD);
  motor3.run(BACKWARD);
  delay(600);
}

void MotorRight()
{
  motor1.run(BACKWARD);
  motor3.run(FORWARD);
  delay(500);
}

void loop()
{
  int testDistance = CheckDistance();
 
   Serial.print(testDistance);
   Serial.println(" test");
   
  if (testDistance >= 30 || testDistance <= 0){
    Serial.println("Out of range");
    //go forward and check range again
    MotorForward(700);
    MotorRelease();
  }
  else
  {
    Serial.print(testDistance);
    Serial.println(" cm");
    //object in path, reverse and turn to avoid
    MotorBackward(600);
    MotorRelease();
    MotorRight();
    MotorRelease();
    MotorForward(600);
    MotorRelease();
    MotorLeft();
    MotorRelease();
   
  }
   
  delay(500);
}

the problem is that it is saying motor 1 was no in the skecth

CrossRoads

Sure, Fix that sketch!

Probably don't want the space between motor and 1, and motor and 3, here:
Code: [Select]

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor 1.setSpeed(255); // set the speed to 200/255
  motor 3.setSpeed(255); // set the speed to 200/255
}


Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulS

Code: [Select]
#motor1(1, MOTOR12_8KHZ); // create motor #1, 8KHz pwm
#motor3(3, MOTOR34_8KHZ); // create motor #3, 8KHz pwm

I don't know what you were smoking when you wrote that, but you need to pass it around.
The art of getting good answers lies in asking good questions.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy