Pages: [1]   Go Down
Author Topic: bug involving declaration of ints?  (Read 1297 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 28 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
« Last Edit: February 18, 2011, 03:03:27 am by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13037
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Compiles fine for me.
Logged

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

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13037
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ditto.

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
« Last Edit: February 18, 2011, 12:58:20 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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 smiley
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13037
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int injon = 0;      // Injector on time
int injstate = LOW; // Used to control digitalWrite on injPin

The compiler was seeing this...

Code:
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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Per Arduino ad Astra

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13037
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Per Arduino ad Astra

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 210
Posts: 13037
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: