I need to do this because my driver would prefer it that way and it would be better for match loads, and I just can’t figure it out
I do wish “forumers” would not post code that is obviously untested and incorrect in many ways.
5 Likes
Sorry, I’ll try to do better next time.
1 Like
I know my reply was harsh, but sometimes just posting the concept is better than posting code that is incorrect. OP will copy your code and find it does not work, then we have to explain why. When posting code it’s better to take a few minutes and write a working example.
C++ example to toggle a motor using an event handler
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: 1/10/2024, 2:32:15 PM */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
vex::controller c1;
// define your global instances of motors and other devices here
vex::motor cata(PORT20);
// event handler that toggles state of a motor
// and keeps track of the motor state using a boolean variable.
void
cataToggle() {
static bool bState = false;
if(bState) {
cata.stop();
bState = false;
}
else {
cata.spin(forward, 100, rpm);
bState = true;
}
}
int main() {
// register a button event, do this in main
c1.ButtonA.pressed(cataToggle);
while(1) {
// Allow other tasks to run
this_thread::sleep_for(10);
}
}
Python example to toggle a motor using an event handler
# ---------------------------------------------------------------------------- #
# #
# Module: main.py #
# Author: james #
# Created: 1/10/2024, 2:37:03 PM #
# Description: V5 project #
# #
# ---------------------------------------------------------------------------- #
# Library imports
from vex import *
# Brain should be defined by default
brain=Brain()
c1=Controller()
cata=Motor(Ports.PORT20)
# event handler to toggle state of the cata motor
# when controller button is pressed
cata_state = False
def cata_toggle():
global cata_state
if cata_state:
cata.stop()
cata_state = False
else:
cata.spin(FORWARD, 100, RPM)
cata_state = True
# register the event handler
c1.buttonA.pressed(cata_toggle)
5 Likes
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.