In function 'void loop()' - cannot be used as a function

I'm working on modifying an Xbox one controller using an ATTiny85 Microchip.
I'm trying to create a rapid fire mod - for testing purposes only. It was a project I had started earlier in the year meant to build a portfolio of completed projects. And finally decided to jump back on it recently.

Anyways, in my code I've ran into an issue inside the void loop(); section.
I wrote an if statement that would detect if both joysticks are pressed at the same time, then the conditions would be met and call a function called fire_mode() just outside the void loop(); with an argument being passed into the parameter.

after verifying the code, I received this error in question - "In function 'void loop()':
rapid_fire_attiny85_xbox_one_s:37:23: error: 'fire_mode' cannot be used as a function"

Here's the entire code:

int left_stick = 2;
int right_stick = 3;
int trigger = 4;
bool active = false;

void setup()
{
  pinMode(left_stick, INPUT);
  pinMode(trigger, INPUT);
  pinMode(right_stick, INPUT);
}
bool fire_mode(active_mode)
{
  if (active_mode == true)
  {
    if (digitalRead(trigger) >= 5)
    {
      pinMode(trigger, OUTPUT);
      digitalWrite(trigger, 1000);
      delay(22);
      pinMode(trigger, INPUT);
      delay(100);
    }
  }
  if (analogRead(left_stick) < 50 && analogRead(right_stick) < 50)
  {
    return bool active_mode = false;
  }
}
void loop()
{
  if (analogRead(left_stick) < 50 && analogRead(right_stick) < 50)
  {
    bool active = true;
    if (active == true)
    {
      fire_mode(active);
    }
  }
}

Any ideas on what I'm doing wrong here?

Cheers!

What's all this then?

Missing argument type. Do you mean
void fire_mode(bool active_mode)

it's supposed to be the function declaration following the block of code.

I'm not sure if the syntax is correct, I'm actually coming from Python so it's been a processing transitioning.

Okay, I corrected the argument type, and it cleared the void loop error.

So I made the following changes..

fire_mode(active); to bool fire_mode(active);

&

bool fire_mode(active_mode) to bool fire_mode(bool active_mode)

after verifying it, it cleared all the errors!

C++ has been getting me with all these syntax errors! LOL

fire_mode(true);
is the way to make the function call

a bool / boolean is 'true' or 'false'

This:

is functionally identical to this:

   fire_mode(active);

since "active" will ALWAYS be true when the if statement is encountered.

1 Like

Okay, I see what you're getting at.

So if I am understanding you correctly. You're saying it's not necessary to declare the variable type when calling a function?

so I should just be able to call the function name, pass in my argument, and close it off with a semi-colon.

function(arg);

I see.
So you're saying it's redundant to include the if-statement following active = true;

it would make more sense to just call the function right after the boolean variable.

I realized I've been following a process of creating if statements to ward off any undesirable affects in my code, not realizing at the same time I was creating unnecessary obstacles.

On reflection, Livingston is most correct.
If I were passing a numeric variable then it would be bad form to use the number itself. So, likewise, 'best practice', you probably should send that variable alias instead of a 'raw' true/false.
(But in the case of a bool, it seems like a lot more typing to go to.)

1 Like

I agree, I had created the boolean variable with the intention of mutating it throughout the code. That way, under certain conditions certain blocks of code (i.e. the fire_mode function) wouldn't turn on unintentionally.

It still is incorrect. If you also enable warnings then you'll find that your function does not return a value. If you declare a bool fn() then it should return a bool value. For no return value the correct function type is void fn().

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.