function not declared in scope

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?

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.

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.

Good question. It would be interesting to see the .cpp file that the IDE produced (in the applet directory).

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

  setColorInterval(blueStartTime, blueInterval);

ssetColorInterval(greenStartTime, greenInterval);
}

set and sset? Does the compiler complain about both, or just the sset version?

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

I thought of that as well, but when i remove the second function (it can be done without of course) the problem remaines.

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

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)
{
}

It seems to go wrong with the references. When I remove them (which I don't want), compiling works.

@mem: I need the reference because I want to have a general function to set global variables.

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)
{
}

This seems to work indeed. Why isn't arduino creating prototypes for references?

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.

1 Like

ok, good to know.