Hi, I’m trying to write a program that has a task constantly running, but when I press a button on the controller, it stops the task. Inside the task, I have a decent amount of loops. I’m having trouble getting the task to stop. I don’t know if I’m using the wrong things for the task (I’m using ‘int’, I’m not really educated on the differences), if I’m stopping it wrong, or if I stop it correctly but the loops in it keep running. If I need to post the code, I can, but I feel like it’s not the greatest. Any help, if you can, would be appreciated.
Edit: Here’s the code, because that’s probably necessary. Again, I’m not the greatest programmer and theres probably much easier ways to do a lot of this stuff.
#include "vex.h"
using namespace vex;
//Sets the "Readout" task, which puts the distances on the screen
int Readout()
{
while(true)
{
Brain.Screen.clearScreen();
Brain.Screen.setCursor(10, 1);
Brain.Screen.print("%2.1f", LeftO.distance(inches));
Brain.Screen.setCursor(7, 1);
Brain.Screen.print("%2.1f", LeftI.distance(inches));
Brain.Screen.setCursor(4, 1);
Brain.Screen.print("%2.1f", RightI.distance(inches));
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("%2.1f", RightO.distance(inches));
wait(0.05, seconds);
}
//"ints" always need a "return". This command will never get here, because of the while command, but it is required to state this anyways.
return 0;
}
//Main Roomba code
int Roomba()
{
Drivetrain.setDriveVelocity(30, percent);
Drivetrain.setTurnVelocity(30, percent);
if(true) { //While command 1 start
for(int i = 0; i < 15000; i++){ //Runs Main and Failsfe programs for 15000 iterations.
if(RightO.distance(inches) > 1 && RightI.distance(inches) > 1 && LeftI.distance(inches) > 1 && LeftO.distance(inches) > 1)
{
//If both outside sensors are greater than 4, runs main program. Added as failsafe.
if (RightO.distance(inches) > 4 || LeftO.distance(inches) > 4) {
//Drive forward
if ((RightO.distance(inches) > 4) && (LeftO.distance(inches) > 4) &&
(RightI.distance(inches) > 7) && (LeftI.distance(inches) > 7)) {
Drivetrain.drive(forward);
wait(2, msec);
}
//Detects object to its right closer than its left, turn left
else if ((RightO.distance(inches) + RightI.distance(inches)) < (LeftO.distance(inches) + LeftI.distance(inches))) {
Drivetrain.turn(left);
wait(2, msec);
}
//Doesnt detect object to its right closer than its left, turns right
else {
Drivetrain.turn(right);
wait(2, msec);
}
//Waits to prevent waisted resources
wait(2, msec);
}
//Runs if both outside motors are less than 4 inches
else if(LeftO.distance(inches) < 4 && RightO.distance(inches) < 4){
Drivetrain.drive(reverse);
wait(1, sec);
Drivetrain.turn(left);
wait(1, sec);
}
wait(2, msec);
} //End of Main If
else {
Drivetrain.drive(reverse);
wait(1, seconds);
Drivetrain.turn(left);
wait(1, seconds);
}
wait(2, msec);
} //End of for command
//runs this program after 50000 iterations of for command
if(RightO.distance(inches) < 12 || LeftO.distance(inches) < 12 || LeftI.distance(inches) < 12 || RightI.distance(inches) < 12)
{
Drivetrain.drive(reverse);
wait(1, seconds);
Drivetrain.turn(left);
wait(1, seconds);
}
wait(2, msec);
} //End of while comand 1
return(0);
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
//calls the "Readout" task, which always runs
task myTask = task(Readout);
task roomba = task(Roomba);
while(true)
{
if(Controller1.ButtonR2.pressing() == true && Controller1.ButtonL2.pressing() == true)
{
vex::task::suspend(roomba);
}
else
{
vex::task::resume(roomba);
}
wait(0.02, seconds);
}
}