default values of variables in functions

Hi,

I’m trying to write a script i can use to blink an led (see code below), however, i am having trouble trying to get default variables in variables addressed in the function declaration (ie. void function(int number=0) ).

What i have found (regarding my code below), is that if i declare the minimum two, three or four variables when calling the function, i get a “not declared in this scope” error, but if i remove the default values (ie. just have ‘int flashtime’ instead of ‘int flashtime=200’, and call the function with all four variables, i get no errors (as would be expected).

Am i representing default values properly, or is there another problem, like a bug?

Cheers,
Rob

#define ledRED 4 //Define LEDs (RGB)
#define ledGRN 5
#define ledBLU 6

void setup() {
 pinMode(ledRED, OUTPUT); //Configure LEDs
 pinMode(ledGRN, OUTPUT);
 pinMode(ledBLU, OUTPUT); 
}

void loop() {
 blinkLED(ledRED,10); //only needed variables declared (ie. no variables which have default values) - results in 'not declared in this scope' error
 blinkLED(ledRED,10,200,0); //all variables declared (this works if you remove the default values from the function declaration)
}


void blinkLED(int pin, int times, int flashtime=200, int remainonafter = 0) { //blinks an LED then returns to BLUE on
 if (flashtime==0) {flashtime = 200;} //SET DEFAULT FLASH TIME
 digitalWrite(ledRED,0); digitalWrite(ledGRN,0); digitalWrite(ledBLU,0); //all off
 if (times == 0) { //one continous flash
  digitalWrite(pin,HIGH);
  delay(flashtime);
 }
 else { //if flashing
  for (int i=0; i<times; i++) {
   digitalWrite(pin, HIGH);
   delay((flashtime/2));
   digitalWrite(pin, LOW);
   delay((flashtime/2));
  }
 }
 if (remainonafter == 1) { //if the light being blinked is supposed to stay on afterwards
  digitalWrite(pin,HIGH);
 }
 else { //if the light being blinked isnt supposed to stay on after it has been used, turn it off and go to blue
  digitalWrite(pin,LOW); //double check that the pin is low
  digitalWrite(ledBLU,1); //blue on
 }
}

I'm away from my PC, but this sounds like the IDE's inability to generate protoypes for all functions.

this sounds like the IDE's inability to generate protoypes for all functions

should this be moved to bugs then?

should this be moved to bugs then?

Not until you prove it is a bug by manually defining the function prototype, and validating that the code works.

Am i representing default values properly, or is there another problem, like a bug?

Think it has to do that the C++ of the arduino is not "complete" C++ but a subset. So its behaviour by design. (although not 100% sure)

you need to do something like:

void blinkLED(int pin, int times) 
{
  blinkLED(pin, times, 200,0);
}

void blinkLED(int pin, int times, int flashtime) 
{
  blinkLED(pin, times, flashtime, 0);
}

void blinkLED(int pin, int times, int flashtime, int remainonafter)
{
... as is

Put this towards the top of your Sketch...

void blinkLED(int pin, int times, int flashtime=200, int remainonafter = 0);

Change the fucntion definition to this...

void blinkLED(int pin, int times, int flashtime, int remainonafter ) { //blinks an LED then returns to BLUE on
 if (flashtime==0) {flashtime = 200;} //SET DEFAULT FLASH TIME
 digitalWrite(ledRED,0); digitalWrite(ledGRN,0); digitalWrite(ledBLU,0); //all off
...

OR, just move all of blinkLED above its first use (above loop and setup).

"Problem" solved.