Using Global variable in a function return a error but work on other functions.

Hi,

I'm sure it's something really simple but I cannot find it after a few days to try.

PS: Any trick or advice are welcome; been years I didn't code so I'm a bit rusty.

Here the orignial code and error message. I'll post what I think should have been the fix (based on the compiler errors) and the error returned.

Original code:

const int TLR = 4; //Trigger Red LED Output
const int TLG = 7; //Trigger Green LED Output
const int TLB = 8; //Trigger Blue LED Output
const int BMode = 3; //mode Switching button
int TrOUT = 0; //Red Led ON or OFF
int TgOUT = 0; //Green Led ON or OFF
int TbOUT = 0; //Blue Led ON or OFF

//volatile for the values used in the interrupt

volatile int mode = 0; //Mode for the gun (0=SAFE 1=BIO 2=RAD 3=ENER)
volatile int fmode = 0;
//pinMode (3, INPUT_PULLUP);
void setup() {
  // put your setup code here, to run once:
pinMode(2 , INPUT); //Trigger input (interrupt)
pinMode(3 , INPUT); //Button switching mode input (interrupt?)
pinMode(4 , OUTPUT); //Trigger output LED R
pinMode(7 , OUTPUT); //Trigger output LED G
pinMode(8 , OUTPUT); //Trigger output LED B
attachInterrupt (digitalPinToInterrupt(2), FIRE, CHANGE);
attachInterrupt (digitalPinToInterrupt(3), ChangeMode, RISING);
Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop() {
  // put your main code here, to run repeatedly:

  /*Serial.print("start");
    Serial.println();
    Serial.print(TrOUT);
    Serial.println();
    Serial.print(TgOUT);
    Serial.println();
    Serial.print(TbOUT);
    Serial.println();
  */
}

int ChangeMode() {
  mode = mode + 1;

  if (mode > 3) {
    mode = 0;
  }
  if (mode == 0) {
    SAFETY();
  }

  if (mode == 1) {
    BIOmode();
  }
  if (mode == 2) {
    RADmode();
  }

  if (mode == 3) {
    ENERmode();
  }
}

int FIRE () {
  //do something for when the trigger is pull
  // digitalwrite the leds depending of the modes pass the value
  // FIRE(TrOUT,TgOUT,TbOUT);
  fmode = fmode + 1;
  
  if (fmode > 1)
  {
    fmode = 0;
  }

  if (fmode == 1)
  {
    FIREact(); //where the compiler got issue with the code
  }

  if (fmode == 0)
  {
    FIRErst();
  }
}

void FIREact(int TrOUT, int TgOUT, int TbOUT)
{
  digitalWrite(TLR, TrOUT );
  digitalWrite(TLG, TgOUT );
  digitalWrite(TLB, TbOUT );
  Serial.print("FIRE ACT");
  Serial.println();
}

int FIRErst (int TrOUT, int TgOUT, int TbOUT) {
  digitalWrite(TLR, LOW );
  digitalWrite(TLG, LOW );
  digitalWrite(TLB, LOW );
  Serial.print("FIRE RST");
  Serial.println();
}

int SAFETY() {
  // all led off

  /*  digitalWrite(TLR, LOW );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, LOW );*/
  TrOUT = 0;
  TgOUT = 0;
  TbOUT = 0;
}

int BIOmode() {

  /*  digitalWrite(TLR, LOW );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, HIGH );*/
  TrOUT = 0;
  TgOUT = 0;
  TbOUT = 1;
}

int RADmode() {

  digitalWrite(TLR, LOW );
  digitalWrite(TLG, HIGH );
  digitalWrite(TLB, LOW );
  TrOUT = 0;
  TgOUT = 1;
  TbOUT = 0;
}

int ENERmode() {

  /*  digitalWrite(TLR, HIGH );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, LOW );*/
  TrOUT = 1;
  TgOUT = 0;
  TbOUT = 0;
}

Error message from the compiler:
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
[.. removed the loading portion]
C:\Users\Sebastien\Documents\Arduino\Waste_gun\Waste_gun.ino: In function 'int FIRE()':

Waste_gun:75:13: error: too few arguments to function 'void FIREact(int, int, int)'

FIREact();

^

C:\Users\Sebastien\Documents\Arduino\Waste_gun\Waste_gun.ino:84:6: note: declared here

void FIREact(int TrOUT, int TgOUT, int TbOUT)

^~~~~~~

Waste_gun:80:13: error: too few arguments to function 'int FIRErst(int, int, int)'

FIRErst();

^

C:\Users\Sebastien\Documents\Arduino\Waste_gun\Waste_gun.ino:93:5: note: declared here

int FIRErst (int TrOUT, int TgOUT, int TbOUT) {

^~~~~~~

exit status 1

too few arguments to function 'void FIREact(int, int, int)'


Corrected code

const int TLR = 4; //Trigger Red LED Output
const int TLG = 7; //Trigger Green LED Output
const int TLB = 8; //Trigger Blue LED Output
const int BMode = 3; //mode Switching button
int TrOUT = 0; //Red Led ON or OFF
int TgOUT = 0; //Green Led ON or OFF
int TbOUT = 0; //Blue Led ON or OFF

//volatile for the values used in the interrupt

volatile int mode = 0; //Mode for the gun (0=SAFE 1=BIO 2=RAD 3=ENER)
volatile int fmode = 0;
//pinMode (3, INPUT_PULLUP);
void setup() {
  // put your setup code here, to run once:
pinMode(2 , INPUT); //Trigger input (interrupt)
pinMode(3 , INPUT); //Button switching mode input (interrupt?)
pinMode(4 , OUTPUT); //Trigger output LED R
pinMode(7 , OUTPUT); //Trigger output LED G
pinMode(8 , OUTPUT); //Trigger output LED B
attachInterrupt (digitalPinToInterrupt(2), FIRE, CHANGE);
attachInterrupt (digitalPinToInterrupt(3), ChangeMode, RISING);
Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop() {
  // put your main code here, to run repeatedly:

  /*Serial.print("start");
    Serial.println();
    Serial.print(TrOUT);
    Serial.println();
    Serial.print(TgOUT);
    Serial.println();
    Serial.print(TbOUT);
    Serial.println();
  */
}

int ChangeMode() {
  mode = mode + 1;

  if (mode > 3) {
    mode = 0;
  }
  if (mode == 0) {
    SAFETY();
  }

  if (mode == 1) {
    BIOmode();
  }
  if (mode == 2) {
    RADmode();
  }

  if (mode == 3) {
    ENERmode();
  }
}

int FIRE () {
  //do something for when the trigger is pull
  // digitalwrite the leds depending of the modes pass the value
  // FIRE(TrOUT,TgOUT,TbOUT);
  fmode = fmode + 1;
  
  if (fmode > 1)
  {
    fmode = 0;
  }

  if (fmode == 1)
  {
    FIREact(int TrOUT, int TgOUT, int TbOUT); //where the compiler got issue with the code
  }

  if (fmode == 0)
  {
    FIRErst(int TrOUT, int TgOUT, int TbOUT);
  }
}

void FIREact(int TrOUT, int TgOUT, int TbOUT)
{
  digitalWrite(TLR, TrOUT );
  digitalWrite(TLG, TgOUT );
  digitalWrite(TLB, TbOUT );
  Serial.print("FIRE ACT");
  Serial.println();
}

int FIRErst (int TrOUT, int TgOUT, int TbOUT) {
  digitalWrite(TLR, LOW );
  digitalWrite(TLG, LOW );
  digitalWrite(TLB, LOW );
  Serial.print("FIRE RST");
  Serial.println();
}

int SAFETY() {
  // all led off

  /*  digitalWrite(TLR, LOW );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, LOW );*/
  TrOUT = 0;
  TgOUT = 0;
  TbOUT = 0;
}

int BIOmode() {

  /*  digitalWrite(TLR, LOW );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, HIGH );*/
  TrOUT = 0;
  TgOUT = 0;
  TbOUT = 1;
}

int RADmode() {

  digitalWrite(TLR, LOW );
  digitalWrite(TLG, HIGH );
  digitalWrite(TLB, LOW );
  TrOUT = 0;
  TgOUT = 1;
  TbOUT = 0;
}

int ENERmode() {

  /*  digitalWrite(TLR, HIGH );
    digitalWrite(TLG, LOW );
    digitalWrite(TLB, LOW );*/
  TrOUT = 1;
  TgOUT = 0;
  TbOUT = 0;
}

Compiler error code:

C:\Users\Sebastien\Documents\Arduino\Waste_gun\Waste_gun.ino: In function 'int FIRE()':
Waste_gun:75:13: error: expected primary-expression before 'int'
FIREact(int TrOUT, int TgOUT, int TbOUT);
^~~
Waste_gun:75:24: error: expected primary-expression before 'int'
FIREact(int TrOUT, int TgOUT, int TbOUT);
^~~
Waste_gun:75:35: error: expected primary-expression before 'int'
FIREact(int TrOUT, int TgOUT, int TbOUT);
^~~
Waste_gun:80:13: error: expected primary-expression before 'int'
FIRErst(int TrOUT, int TgOUT, int TbOUT);
^~~
Waste_gun:80:24: error: expected primary-expression before 'int'
FIRErst(int TrOUT, int TgOUT, int TbOUT);
^~~
Waste_gun:80:35: error: expected primary-expression before 'int'
FIRErst(int TrOUT, int TgOUT, int TbOUT);
^~~
exit status 1
expected primary-expression before 'int'

If a variable (or const) is global, there's no reason to pass it to a function.

you first call it like this :

FIREact(); //where the compiler got issue with the code

But it is declared as

void FIREact(int TrOUT, int TgOUT, int TbOUT)

that means you are expected to pass 3 integers as arguments, but not declare them again.

FIREact(int TrOUT, int TgOUT, int TbOUT); //where the compiler got issue with the code

so like this :

FIREact(TrOUT, TgOUT,  TbOUT);

of course if the variab;es are anyway declared globally you may as well not have arguments to the function

Hi,

TheMemberFormerlyKnownAsAWOL:
If a variable (or const) is global, there's no reason to pass it to a function.

Yes that was pretty much what I remembered and was totally shock that on 1 function it worked and not on the other.

With a good night rest and some more peaceful time I created a new sketch and copy part of the codes.

I ended up removing the varriable "pass" from the functions and hold and behold it worked!

Do the same change in my original code... SAME ISSUE.

Copy and paste the working code in the original Sketch... it WORK!

Now I'm really confused on why this occured like this... only thing I could see is they were something I couldn't see on screen creating a issue. IF someone have a idea or encountered this I would be really interested to know more on the why.

And thanks a lot to @Deva_Rishi for the refresher about the variable passing, I feel it will be helpfull in the near future.
Sorry for, what I feel in the end, a waste of time for some I should have been able to do...