0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« on: December 15, 2009, 11:42:05 am » |
This is the program (not finished as you can see): // CONSTANTS const char STATE_DEFAULT = 'A';
// ENVIRONMENT VARIABLES long currentTime = 0; char state = STATE_DEFAULT; int redInterval = 0; int blueInterval = 0; int greenInterval = 0; long redStartTime = 0; long blueStartTime = 0; long greenStartTime = 0;
void setup() { Serial.begin(9600); setColorInterval(redStartTime, redInterval); setColorInterval(blueStartTime, blueInterval); ssetColorInterval(greenStartTime, greenInterval); }
void loop() { }
void setColorInterval(long &startTime, int &interval, int minInterval, int maxInterval) { if((startTime == 0) || (currentTime - startTime >= interval)) { interval = random(minInterval, maxInterval); startTime = currentTime; } }
void setColorInterval(long &startTime, int &interval) { setColorInterval(startTime, interval, 0, 10000); }
When I compile this code I get a "In function 'void setup()': error: 'setColorInterval' was not declared in this scope". Even when I move the function call from setup to loop() it still gives me the error for loop. Why am I getting this error?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: December 15, 2009, 11:51:13 am » |
setColorInterval is defined AFTER the call to it, in either setup or loop.
Move the function up, so that it is before setup and/or loop, and the problem will go away.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #2 on: December 15, 2009, 12:02:36 pm » |
This works indead, but it should work without. I have written programs in arduino before in the same way and it worked perfect. This is because the arduino build environment automatically creates prototypes for functions in the main tab of a sketch so there is no need to put a function declaration before it is called. So my question remaines.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: December 15, 2009, 12:05:33 pm » |
Good question. It would be interesting to see the .cpp file that the IDE produced (in the applet directory).
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #4 on: December 15, 2009, 12:16:22 pm » |
cpp file: // CONSTANTS #include "WProgram.h" void setup(); void loop();
const char STATE_DEFAULT = 'A';
// ENVIRONMENT VARIABLES long currentTime = 0; char state = STATE_DEFAULT; int redInterval = 0; int blueInterval = 0; int greenInterval = 0; long redStartTime = 0; long blueStartTime = 0; long greenStartTime = 0;
void setup() { Serial.begin(9600); setColorInterval(redStartTime, redInterval); setColorInterval(blueStartTime, blueInterval); ssetColorInterval(greenStartTime, greenInterval); }
void loop() { }
void setColorInterval(long &startTime, int &interval, int minInterval, int maxInterval) { if((startTime == 0) || (currentTime - startTime >= interval)) { interval = random(minInterval, maxInterval); startTime = currentTime; } }
void setColorInterval(long &startTime, int &interval) { setColorInterval(startTime, interval, 0, 10000); }
int main(void) { init();
setup(); for (;;) loop(); return 0; }
It seems that something goes wrong since no prototype is created for the functions
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: December 15, 2009, 12:39:28 pm » |
setColorInterval(blueStartTime, blueInterval); ssetColorInterval(greenStartTime, greenInterval); }
set and sset? Does the compiler complain about both, or just the sset version?
|
|
|
|
|
Logged
|
|
|
|
|
Huntsville, Alabama, USA
Offline
Sr. Member
Karma: 0
Posts: 327
Arduino rocks
|
 |
« Reply #6 on: December 15, 2009, 07:17:37 pm » |
CW,
I notice that you've defined setColorInterval() twice, with different arguments. I didn't think you could do this with 'C'-style functions. I thought it could only be done inside a C++ class.
It's possible that this double definition is confusing the preprocessor, so it doesn't output a declaration for that function. You might try changing it to one function to see if it fixes the problem.
Regards,
-Mike
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #7 on: December 16, 2009, 04:16:18 am » |
I thought of that as well, but when i remove the second function (it can be done without of course) the problem remaines.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #8 on: December 16, 2009, 04:35:46 am » |
@seeDoubleYou If you are still having problems, post your code, again. After all the modifications, it's hard to tell what the problem is, since we can't see over your shoulder.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #9 on: December 16, 2009, 04:53:08 am » |
Ok, I've broke it even more down and still getting the error. Here's the code as it is now: int redInterval = 0; long redStartTime = 0;
void setup() { Serial.begin(9600); setColorInterval(redStartTime, redInterval); }
void loop() { }
void setColorInterval(long &startTime, int &interval) { }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #10 on: December 16, 2009, 04:59:35 am » |
It seems to go wrong with the references. When I remove them (which I don't want), compiling works.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #11 on: December 16, 2009, 05:00:55 am » |
@mem: I need the reference because I want to have a general function to set global variables.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #12 on: December 16, 2009, 05:03:45 am » |
I was in the process of updating my post, I don't think arduino creates prototypes for references You could try adding an explicit prototype a the beginning of the sketch int redInterval = 0; long redStartTime = 0;
void setColorInterval(long &startTime, int &interval);
void setup() { Serial.begin(9600); setColorInterval(redStartTime, redInterval); }
void loop() { }
void setColorInterval(long &startTime, int &interval) { }
|
|
|
|
« Last Edit: December 16, 2009, 05:05:15 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 21
Arduino rocks
|
 |
« Reply #13 on: December 16, 2009, 05:13:03 am » |
This seems to work indeed. Why isn't arduino creating prototypes for references?
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #14 on: December 16, 2009, 05:20:31 am » |
I don't know, but FYI it doesn't create prototypes for user defined types either.
I guess that when the auto-prototype code was written, it was assumed that this functionality would not be needed.
|
|
|
|
« Last Edit: December 16, 2009, 05:22:20 am by mem »
|
Logged
|
|
|
|
|
|