" Cannot Initialize a parameter of type 'void (*)()' with an rvalue of type 'void' " error

I am trying to get a button to toggle a motor on and off. At first, I tried using the Controller1.<Button>.pressing(), but it cycled through the on and off modes too fast when I pressed the button because it thought I was holding down the button. I thought that if I had the motors toggle when I let go of the button would fix things, but I could never get the Controller1.<Button>.released to work because I have no clue how a callback function works. I did find a work around with a do-while loop, but I’d still like to understand how the released callback function works. Here is my attempt at it: (the error says " Cannot Initialize a parameter of type ‘void (*)()’ with an rvalue of type ‘void’ ")

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       C:\Users\hazem                                            */
/*    Created:      Sat Jan 13 2024                                           */
/*    Description:  V5 project                                                */
/*                                                                            */
/*----------------------------------------------------------------------------*/

#include "vex.h"

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Controller1          controller                    
// motor1               motor         1               
// ---- END VEXCODE CONFIGURED DEVICES ----

using namespace vex;

bool runMotor1 = 0;

int runAllMotors() {
  while (true) {
    if (runMotor1 == 1) {
      motor1.spin(forward);
    } else {
      motor1.stop(hold);
    }
    task::sleep(20);
    return 0;
  }
}

void release_callback() { runMotor1 = abs(runMotor1 - 1); }

int main() {

  task runMotors = task(runAllMotors); // runs 'runAllMotors' in the background

  Controller1.ButtonL1.released(release_callback()); // does not work

  while (true) {                           // Working alternative
    if (Controller1.ButtonL1.pressing()) { // checks if button L1 is pressed
      do {
        wait(5, msec);
      } while (
          Controller1.ButtonL1.pressing()); // waits until button L1 is released
      runMotor1 = abs(runMotor1 - 1);
    }
  }
}

make it so that it says Controller1.ButtonL1.released(release_callback); (same thing, without parenthesis)

that seems to have fixed the error.
I have a different question. Does it always check if ButtonL1 is released, or do I need to put it in a while loop?

Adding a function callback creates an event handler that will automatically call the function when it detects the button being released. Putting it in a while loop will cause the event handler to be initialized multiple time which is a very bad idea.

1 Like

ok, thank you so much!

it will always check. Just make sure to put it in main so it would only register once.

1 Like

also, instead of using a number value for the motor state, just flip the bool.

`void release_callback() { runMotor1 = !runMotor1; }

1 Like

Thanks, I didn’t know you can do that.

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