Need help with creating a task

So I can’t figure out what this error means:
Error:Task ‘scan’ is not defined at global scope level
Error:Tasks must be defined at main scope level
Could ya’ll help me out?

I’m using RobotC. Here’s the code:

#pragma config(Sensor, port12, sonar,          sensorVexIQ_Distance)
#pragma config(Motor,  motor1,          scaner,        tmotorVexIQ, PIDControl, encoder)
#pragma config(Motor,  motor6,          Ldrive,        tmotorVexIQ, PIDControl, reversed, driveLeft, encoder)
#pragma config(Motor,  motor7,          Rdrive,        tmotorVexIQ, PIDControl, driveRight, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
task scan() {
   while (true) {
  setMotorTarget(scaner, 40, 10);    
   setMotorTarget(scaner, -40, 10);  
  }
}

repeat(forever)
{

startTask(scan);
 



  if (getDistanceValue(sonar) <= 300)
  {
   stopAllMotors();
  	if (rand() % 2){
    turnRight(random(350), degrees, 50);
    }
    else {
    turnLeft(random(350), degrees, 50);
    }
      waitUntilMotorStop(Ldrive);
			waitUntilMotorStop(Rdrive);
      setMultipleMotors(100, Rdrive, Ldrive);
      }
   
  }
  
}

Your problem is that you are putting the task ‘scan’ in your ‘main’ task.
You have to define tasks and functions separately from your main task, like this:

task scan() {
    while (true) {
        setMotorTarget(scaner, 40, 10);    
        setMotorTarget(scaner, -40, 10);  
    }
}

task main() {

    startTask(scan);
    repeat(forever) {

        if (getDistanceValue(sonar) <= 300) {  

            stopAllMotors();
            if (rand() % 2) {

                turnRight(random(350), degrees, 50);

            } else {

                turnLeft(random(350), degrees, 50);

            }
            
            waitUntilMotorStop(Ldrive);
            waitUntilMotorStop(Rdrive);
            setMultipleMotors(100, Rdrive, Ldrive);
        }
    
    }

}
2 Likes

put this outside of the repeat loop

2 Likes

My bad, I edited it to make it correct.
@Uriah If the code didn’t work right before, it should now

1 Like