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);
}
}
}
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.)
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().