bug involving declaration of ints?

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.

methanol_driver.txt (6.53 KB)

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)

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.

Compiles fine for me.

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

Ditto.

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

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).

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 :slight_smile:

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

I took out that \ and it compiled okay after the longs, not sure why.

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

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

int injon = 0;      // Injector on time
int injstate = LOW; // Used to control digitalWrite on injPin

The compiler was seeing this...

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.

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

Groove:
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.

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

Absolutely!

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.

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