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