'var' not declared in this scope. (Global Var)

Hi all,

I'm getting a weird error so figured I'd post to see if there's any info i'm missing somewhere. I'm working on defining all the pins i'm using for my relays and sensors. I have my 'MotorRelay' defined as a global variable being that it is defining it as a PIN. When I have it being declared at the end of my declaration of my pins it doesn't register to the sketch that it exists therefor giving me the declared in scope error. If I switch it higher up the list it complies fine. I've checked my spelling (twice) and I can't find any issues. Any ideas?

Thanks,

  • Matt

Hello,

Post your code

guix:
Hello,

Post your code

It's a little depressing that this even needs to be said.

Such a friendly community today...

Here is my code. Although the problem is more of a generic issue rather then a issue with the code itself in my opinion.

#include <EEPROM.h>

//System Load
const int ErrorLED = 12; //Error LED
const int StopButton1 = 3;
const int StopButton2 = 4;
const int ResetButton = 2;
//const int MotorRelay = 41;  // When pin is declared here, sketch compiles.

//Load Sensors
const int HookRailEmpty = 15;
const int HookRailFull = 16;
const int HangerRackFull = 17;
const int HookCycleStart = 18;
const int FeedTrigger = 19;
const int HeadUp = 20;
const int HeadDown = 21;
const int StripOffOut = 22;
const int CrimpCycleStart = 23;
const int CrimpMicro = 24;
const int StartFeedButton = 25;
//Load Solenoids
const int ToolHead = 25;
const int StripOff = 26;
const int HookStopper = 27;
const int CrimpStopper = 28;
const int Crimp = 29;
const int FeedTable = 30;
const int MainAir = 31;
//System Outputs
const int HookShaker = 32; //*** 120 VAC ***\\
const int MotorRelay = 41;  // When pin is declared here it does not compile

//Load in Variables
int Safe = 0;
int Error = 0;
int ResetCheck = 0;

//Setting up the Timers
unsigned long previousTimer1 = 0;
unsigned long previousTimer2 = 0;
unsigned long previousTimer3 = 0;
unsigned long previousTimer4 = 0;
unsigned long previousTimer5 = 0;
const long TimerINV1 = 1000;
const long TimerINV2 = 1000;
const long TimerINV3 = 1000;
const long TimerINV4 = 2300;
const long TimerINV5 = 1000;
const long TimerINV6 = 1000;


int FeedLoop = 0;
int FeedCheck = 0;
int FeedNext = 0;
int HookNext = 0;
int HookLoop = 0;
int HookCheck = 0;
int CrimpLoop = 0;
int CrimpNext = 0;
int RailCheck = 0;
int RailCheckNext = 0;

//*************************** SETUP START ****************************
void setup(){
  Serial.begin(9600); //Set Serial Speed
  Serial.println("Starting Setup...");
  //attachInterrupt(2, StopAll, FALLING);
  for (int y = 0; y < 5; y ++){
    int a = EEPROM.read(y);
    a = a * 100;
    Serial.print("Time Var");
    Serial.print(y);
    Serial.print(":");
    Serial.print(a);
    delay(1000);
  }
  //INPUT
  pinMode(StopButton1, INPUT);
  pinMode(StopButton2, INPUT);
  pinMode(HookRailEmpty, INPUT);
  pinMode(HookRailFull, INPUT);
  pinMode(HangerRackFull, INPUT);
  pinMode(HookCycleStart, INPUT);
  pinMode(FeedTrigger, INPUT);
  pinMode(HeadUp, INPUT);
  pinMode(HeadDown, INPUT);
  pinMode(StripOffOut, INPUT);
  pinMode(CrimpCycleStart, INPUT);
  pinMode(CrimpMicro, INPUT);
  pinMode(StartFeedButton, INPUT);
  pinMode(ResetButton, INPUT);
  //OUTPUT
  pinMode(ErrorLED, OUTPUT);
  pinMode(ToolHead, OUTPUT);
  pinMode(StripOff, OUTPUT);
  pinMode(HookStopper, OUTPUT);
  pinMode(CrimpStopper, OUTPUT);
  pinMode(Crimp, OUTPUT);
  pinMode(FeedTable, OUTPUT);
  pinMode(MainAir, OUTPUT);
  pinMode(HookShaker, OUTPUT);
  pinMode(MotorRelay, OUTPUT);

  //Assign Variables
  digitalWrite(MainAir, HIGH);
  digitalWrite(MotorRelay, HIGH);
}
void loop() {
  //Safety Check
  Safe = (digitalRead(StopButton1) + digitalRead(StopButton2));
  if (Safe >= 1){
    digitalWrite(MainAir, LOW);
    digitalWrite(MotorRelay, LOW);
    digitalWrite(ErrorLED, HIGH);
    digitalWrite(MotorRelay, LOW);
    Serial.println("Stop Button Activated");
    return;
    }
  if (Error >= 1){
    digitalWrite(MainAir, LOW);
    digitalWrite(MotorRelay, LOW);
    digitalWrite(ErrorLED, HIGH);
    return;
    }
}

This is not the full code but the main part in question.  The board in use is a MEGA 2650

If I try to compile it I get

sketch_jan20a.ino: In function 'void setup()':
sketch_jan20a.ino:69:22: error: 'StopAll' was not declared in this scope
'StopAll' was not declared in this scope

Which is completely true, it's not defined anywhere... At least try to give us compilable code if you give us a simplified version plus the whole error message.

Because when I comment out that l line all compiles without a problem...

Your problem is on this line

const int HookShaker = 32; //*** 120 VAC ***\\

Character \ tells compiler to read the next line as part of the current line.

The line, as seen by the compiler:
const int HookShaker = 32; //*** 120 VAC *** const int MotorRelay = 41;  // When pin is declared here it does not compile. Variable MotorRelay is commented, so you get Variable not declared error.

It compiles fine for me if I remove the slashes in the comment from the preceding line and change it to

 const int HookShaker = 32; //*** 120 VAC ***
const int MotorRelay = 41;  // When pin is declared here it does not compile

Dont' understand it, but there you go - something in that comment is confusing the compiler

EDIT: Oops, guix beat me to it.

septillion:
If I try to compile it I get

sketch_jan20a.ino: In function 'void setup()':

sketch_jan20a.ino:69:22: error: 'StopAll' was not declared in this scope
'StopAll' was not declared in this scope




Which is completely true, it's not defined anywhere... At least try to give us compilable code if you give us a simplified version **plus** the whole error message.

Because when I comment out that l line all compiles without a problem...

I didn't notice that was left un-commented until I already posted my code. I commented it out in my edit but I'm thinking you got to it before I could edit it out.

guix:
Your problem is on this line

const int HookShaker = 32; //*** 120 VAC ***\\

Character \ tells compiler to read the next line as part of the current line.

Interesting. I did not realize that would be compiled as code instead of a comment.

Thanks,

  • Matt

Wow, where did that get changed... I would not expect the \ to do anything if it's IN a comment. But apparently on 1.8.1 it indeed also comments the next line. But on 1.6.4 it acts like I would imagine (hence, no error)! Pretty damn annoying if you ask me...

septillion:
Pretty damn annoying if you ask me...

It's only worked that way since the '70s....

A trailing backslash is, and always has been since the first versions of c, the line continuation character.

Regards,
Ray L.

Is the multiline comment use an exception then?
/* start comments
more
end comment */

I guess that's not the same slash direction now that I see in text.

Then I guess somebody made a space-time whole for version 1.6.4.... :smiley:

I always thought everything in a comment block was ignored but apparently a \ at the end of a comment is supposed to be a line continuation as well. But in 1.6.4 it's not :stuck_out_tongue: And that's what I used to test the code...

I now notice Notepad++ parses the code correct and displays the next line as comment indeed. Arduino IDE (also 1.8.1) does not so that's a bug.