@73335T, whenever you program a macro that executes during interactive user control period you need to define at least one state variable outside of the while(1) loop.
Please, see the following example:
int iMacroStateVar = 0; // 0 - macro is not running
uint32_t macroStepEndTime = 0;
while(1)
{
int userVal = 0;
int joyVal = Controller1.Axis1.position(pct);
if( fabs(joyVal) > 10 ) // if using joystick
{
userVal = joyVal;
}
// if using buttons
if( Controller1.ButtonR1.pressing() ) // forward
{
userVal = 50; // percent of velocity
}
else if( Controller1.ButtonR2.pressing() ) // backward
{
userVal = -50; // percent of velocity
}
if( userVal != 0 )
{
motor1.spin(forward, userVal, pct);
iMacroStateVar = 0; // cancel any macro run
}
else if( Controller1.ButtonA.pressing() && iMacroStateVar==0 )
{ // user pressed A to start macro and macro is not running
iMacroStateVar = 1; // run motor in reverse for 1 sec or if run into wall
macroStepEndTime = vex::timer::system() + 1000;
motor1.spin(reverse,20,pct);
}
else if( iMacroStateVar==1 && // check when its time to end step 1
(backBumperSwitch.pressing() ||
vex::timer::system() > macroStepEndTime ) )
{
iMacroStateVar = 2; // run motor forward for 200 units or up to 1.5 sec
macroStepEndTime = vex::timer::system() + 1500;
motor1.spinFor(forward, 200, rotationUnits::deg, 15, velocityUnits::pct, false);
}
else if( iMacroStateVar==0 || vex::timer::system() > macroStepEndTime )
{
motor1.stop();
iMacroStateVar = 0;
}
vex::task::sleep(1);
}
In this example user could control motor1 with either value of joystick on Axis1 or by pressing buttons R1 or R2.
When user presses button A it will start the macro and remember its start time.
At any time while macro is running user can take over control and macro sequence will be canceled.
https://api.vexcode.cloud/v5/html/classvex_1_1motor.html
https://api.vexcode.cloud/v5/html/classvex_1_1timer.html
https://api.vexcode.cloud/v5/html/classvex_1_1bumper.html