Go Down

Topic: bug involving declaration of ints? (Read 1 time) previous topic - next topic

J-Ri350

I am working on writing a sketch to control a methanol injection system on my supercharged Cavalier.  I added the int "injstate" after declaring 4 longs, which results in the error message "Methanol_driver_pde:82: error: 'injstate' was not declared in this scope" being displayed when testing.  I found that if I moved it anywhere except after the 4 longs, it works fine (including moving it to directly after the third).  It also worked fine if I declared it in "void loop()".  The sketch is nowhere near complete, I include the code only so someone can copy it and see for themselves what happens.  The post exceeded the allowed 9500 characters with the code copied here, so I included it in the attached text file.

CrossRoads

#1
Feb 18, 2011, 08:59 am Last Edit: Feb 18, 2011, 09:03 am by CrossRoads Reason: 1
Does it work okay if you declare it to be 0 instead of LOW, like the rest of your variables?

int injstate = LOW; // Used to control digitalWrite on injPin


On an editorial note, could be a little less confusing with names too, like warnPinYellow & warnPinRed or something:

int warnPin = 6;    // select the output pin for the yellow warning LED (note lower case letters in name)
int WARNPin = 9;    // select the output for the red warning LED (note UPPER CASE letters in name)
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.

J-Ri350

No.  It also does not work if I rename the variable.  It seems that declaring it after the longs is what makes the difference.  The last time i wrote any program was in TI-BASIC back in high school... so this is a bit out of my area of expertise.

EDIT: the same thing happens if I moveed the int "red" to after the longs.

Coding Badly


J-Ri350

I'm using Arduino 0022, if the difference may be there... ?

Coding Badly

Ditto.

Open a fresh instance of the Arduino IDE.  Paste the contents of the text file.  Verify (compile).  Does that work?

CrossRoads

#6
Feb 18, 2011, 09:10 am Last Edit: Feb 18, 2011, 06:58 pm by CrossRoads Reason: 1
Move it down a few lines & try it:

//move from here
long cinjpw = 0;    //  / Used to turn the injector on and off without using "delay", as duty cycle may
long pinjpw = 0;    //  \ approach 85% at higher boost levels
long pumpon = 0;    //  / Used to keep track of how long ago the pump was last needed
long pumpoff = 0;   //  \ << seems to be related to this slash here!
// down to here
int injstate = 0; // Used to control digitalWrite on injPin

I can get the error he is reporting also (IDE -0021).
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.

J-Ri350

I tried opening a new IDE and it compiles...

The weird thing is I was playing around with moving it before the suggestion about the slash... I did the same slash thing to the first two longs and it works after the first two... just seemed to be after 4 in a row.

At any rate, it works, thanks for the help guys.  That's pretty darn fast for 2AM :)

CrossRoads

2? Its 3 here! I should really get to bed ...

I took out that \ and it compiled okay after the longs, not sure why.
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.

Coding Badly

The \ character is "line continuation" for the pre-processor.

My suspicion is that there was a line-ending problem.  The editor was displaying this...

Code: [Select]
int injon = 0;      // Injector on time
int injstate = LOW; // Used to control digitalWrite on injPin


The compiler was seeing this...

Code: [Select]
int injon = 0;      // Injector on time int injstate = LOW; // Used to control digitalWrite on injPin

Why did it work on my system?  Probably a difference in operating system, JVM, or internet browser.  Any one of those could have silently corrected a line-ending discrepancy.

Why did removing a \ fix the problem?  Maybe the editor checked and corrected the line-endings because of a change.

But, that's all just a shot-in-the-not-very-dark-because-it's-a-full-moon-lit-night theory.

Groove

Code: [Select]
long pumpoff = 0;   //  \

It's the backslash on the declaration of "pumpoff" - it is a continuation character, so any declaration on the next line is not seen by the compiler.

Don't put backslashes in your code unless you know what you're doing
Per Arduino ad Astra

Coding Badly

It's the backslash on the declaration of "pumpoff"


Can't be.  I didn't have to make any changes for the Sketch to compile.

Quote
Don't put backslashes in your code unless you know what you're doing


Absolutely!



Groove

Quote
Can't be.  I didn't have to make any changes for the Sketch to compile

The sketch as posted compiles correctly, because there is no declaration on the line after the declaration of "pumpoff", as stated in the original post.
If you put any declaration immediately after that line and later reference the declared item, the compilation will fail - the "declaration" is simply seen as part of the "//" comment and ignored.

It isn't a matter of declaration of "long"s or any other datatype, it is simply a matter of not putting continuation characters where they're not needed.
I think a similar problem came up a few days ago, but then it was a continuation character at the end of a comment immediately before a functions closing brace.

Per Arduino ad Astra

Coding Badly

Yikes!  Sorry about that.  I'm used to folks posting code that doesn't work.  Posting code that does work tripped me up.

Go Up