lcd while loop trouble

Hi i am trying to program my robot to use a LCD but as soon as i put the program in the LCD worked the way i wanted it to but the robot wouldn’t move. can someone fix it so the LCD is displaying the stuff i want but the robot also actually working and moving.
I also tried using 2 while loops but that made the LCD not work but the robot did work.


#pragma config(Sensor, in7,    autonp,         sensorNone)
#pragma config(Sensor, in8,    autonc,         sensorPotentiometer)
#pragma config(Sensor, dgtl2,  ad,             sensorTouch)
#pragma config(Sensor, dgtl4,  autons,         sensorTouch)
#pragma config(Sensor, dgtl10, claw,           sensorDigitalOut)
#pragma config(Sensor, dgtl11, au,             sensorTouch)
#pragma config(Sensor, dgtl12, solenoid,       sensorDigitalOut)
#pragma config(Motor,  port2,           a1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           a2,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           a3,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           d2,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           d1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           a4,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           a5,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           a6,            tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"

void pre_auton() {

  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes.



  bStopTasksBetweenModes = true;
}


task autonomous() {

  if(SensorValue autonc ]<=500 ) {
    //fence stars
    if(SensorValue autonp ] >=500 ) {
      //do the fence stars on the left side
    }
    else {
      //do the fence stars on the right side
    }
  }
  else {
    //field stars
    if(SensorValue autonp ] <=499 ) {
      //do the field stars on the left side
    }
    else {
      //Do the field stars on the right side

    }
  }
}






task usercontrol() {


  while(true)
  {

	clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("auto side");   // Display left or right for auto
  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("which one");   // Display which auto the robot is going to do

sleep(5000);
// somthin funny about vex
clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("How's My Driving?");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("1-800-345-7686");   // Display "ROBOTC :]"

  sleep(3000);
  //disply robo name and stuff
  clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("Alpha Robotics's");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("//////Alpha\\\\\\");   // Display "ROBOTC :]"
sleep(5000);
  
    if(vexRT[Btn8L] == 1)
    {
      SensorValue[solenoid] = 1;
    }

    if(vexRT[Btn8L] == 0)
    {
      SensorValue[solenoid] = 0;
    }

    if(vexRT[Btn6D]==0)
    {
      SensorValue[claw]=1;
    }

    if(vexRT[Btn6D]==1)
    {
      SensorValue[claw]=0;
    }

     // Set drive motors
    setMultipleMotors(vexRT[Ch2], d1);
    setMultipleMotors(vexRT[Ch3], d2);

    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    if(SensorValue(au)) {

      SensorValue[solenoid] = 1;

      // Stop all motors
      setMultipleMotors(0, a1, a2, a3, a4,);
      setMultipleMotors(0, a5, a6,);
      sleep(200);
      setMultipleMotors(-50, a1, a2, a3,);
      setMultipleMotors(-50, a4, a5, a6,);
      sleep(100);

      // If button 6U (upper right shoulder button) is pressed, active solenoid
      //SensorValue[solenoid] = vexRT[Btn8D];

      }
    else {

      // If B5U is pressed, motors gets set to full forward speed
      // If B5D is pressed, motors get set to full backward speed
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a1, a2, a3, a4,);
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a5, a6,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a1, a2, a3, a4,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a5, a6,);
      
    }
  }
  
    sleep(20);
  
}

@Cody

You have massive delays in the while loop of your op control code, which means the cortex is going to be spending most of it’s time waiting 5 or 3 seconds and not updating your drive motors like you want it to.

If I understand what you are trying to do correctly, then you need to use tasks. One for updating LCD and one for teleop control.

There might be some other problems with your code as well, but the calls to sleep() seem to be your fundamental problem.

Throw the LCD code into it’s own task.

There are non-task based ways to do this, but that’s the easiest and may serve your needs well.

ok i think i am understanding this a little better but im not sure what it means when it tells me “Error:Functions must be defined at main scope level”



#pragma config(Sensor, in7,    autonp,         sensorNone)
#pragma config(Sensor, in8,    autonc,         sensorPotentiometer)
#pragma config(Sensor, dgtl2,  ad,             sensorTouch)
#pragma config(Sensor, dgtl4,  autons,         sensorTouch)
#pragma config(Sensor, dgtl10, claw,           sensorDigitalOut)
#pragma config(Sensor, dgtl11, au,             sensorTouch)
#pragma config(Sensor, dgtl12, solenoid,       sensorDigitalOut)
#pragma config(Motor,  port2,           a1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           a2,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           a3,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           d2,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           d1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           a4,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           a5,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           a6,            tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"

void pre_auton() {

  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes.



  bStopTasksBetweenModes = true;
}


task autonomous() {

  if(SensorValue autonc ]<=500 ) {
    //fence stars
    if(SensorValue autonp ] >=500 ) {
      //do the fence stars on the left side
    }
    else {
      //do the fence stars on the right side
    }
  }
  else {
    //field stars
    if(SensorValue autonp ] <=499 ) {
      //do the field stars on the left side
    }
    else {
      //Do the field stars on the right side

    }
  }
}





task usercontrol() {



void StartTask(void lcd, const short nTaskPriority)
{
StartTask(lcd, 10);
}

  while(true)
  {

  
  
  
	clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("auto side");   // Display left or right for auto
  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("which one");   // Display which auto the robot is going to do

wait1Msec(5000);
// somthin funny about vex
clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("How's My Driving?");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("1-800-345-7686");   // Display "ROBOTC :]"

  wait1Msec(5000);
  //disply robo name and stuff
  clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("Alpha Robotics's");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("//////Alpha\\\\\\");   // Display "ROBOTC :]"
wait1Msec(5000);
 


    if(vexRT[Btn8L] == 1)
    {
      SensorValue[solenoid] = 1;
    }

    if(vexRT[Btn8L] == 0)
    {
      SensorValue[solenoid] = 0;
    }

    if(vexRT[Btn6D]==0)
    {
      SensorValue[claw]=1;
    }

    if(vexRT[Btn6D]==1)
    {
      SensorValue[claw]=0;
    }

     // Set drive motors
    setMultipleMotors(vexRT[Ch2], d1);
    setMultipleMotors(vexRT[Ch3], d2);

    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    if(SensorValue(au)) {

      SensorValue[solenoid] = 1;

      // Stop all motors
      setMultipleMotors(0, a1, a2, a3, a4,);
      setMultipleMotors(0, a5, a6,);
      sleep(200);
      setMultipleMotors(-50, a1, a2, a3,);
      setMultipleMotors(-50, a4, a5, a6,);
      sleep(100);

      // If button 6U (upper right shoulder button) is pressed, active solenoid
      //SensorValue[solenoid] = vexRT[Btn8D];

      }
    else {

      // If B5U is pressed, motors gets set to full forward speed
      // If B5D is pressed, motors get set to full backward speed
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a1, a2, a3, a4,);
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a5, a6,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a1, a2, a3, a4,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a5, a6,);
      
    }
  } 
    sleep(20);
 }


You cannot nest a function inside another function. Here is an example I gave someone last week showing how to start a task, perhaps this will help.

A task will run until stopped or it exits, for example, this task will perform the three lines of code and then stop.

task testTask() {
  motor port1 ] = 100;
  wait1Msec(1000);
  motor port1] = 0;
}

the task is started by startTask( testTask ); The task runs in parallel with the code after the startTask command, an example.

void foo() {
  startTask( testTask );
  // immediately this code will run
  motor port2 ] = 100;
  wait1Msec( 2000 );
  motor port2 ] = 0;
}

This will start the test task, the motor on port1 will run for 1 second, the motor on port2 will run for two seconds, they will both be running together.


In your case you will have a while loop inside the task that updates the lcd.


task lcdTask() {
  while(1) {
  // update the LCD
  }
}

The code works now mostly but when the robot does not move the LCD stops updating. this is something i can live with because the robot will be moving most of the time during the competition. but i will post the code again anyways.



#pragma config(Sensor, in7,    autonp,         sensorNone)
#pragma config(Sensor, in8,    autonc,         sensorPotentiometer)
#pragma config(Sensor, dgtl2,  ad,             sensorTouch)
#pragma config(Sensor, dgtl4,  autons,         sensorTouch)
#pragma config(Sensor, dgtl10, claw,           sensorDigitalOut)
#pragma config(Sensor, dgtl11, au,             sensorTouch)
#pragma config(Sensor, dgtl12, solenoid,       sensorDigitalOut)
#pragma config(Motor,  port2,           a1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           a2,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           a3,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port5,           d2,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           d1,            tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           a4,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           a5,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           a6,            tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"

void pre_auton() {

  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes.



  bStopTasksBetweenModes = true;
}


task autonomous() {

  if(SensorValue autonc ]<=500 ) {
    //fence stars
    if(SensorValue autonp ] >=500 ) {
      //do the fence stars on the left side
    }
    else {
      //do the fence stars on the right side
    }
  }
  else {
    //field stars
    if(SensorValue autonp ] <=499 ) {
      //do the field stars on the left side
    }
    else {
      //Do the field stars on the right side

    }
  }
}



task lcdTask(){
  while(1) {
  // update the LCD
  clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("auto side");   // Display left or right for auto
  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("which one");   // Display which auto the robot is going to do

wait1Msec(5000);
// somthin funny about vex
clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("How's My Driving?");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("1-800-345-7686");   // Display "ROBOTC :]"

  wait1Msec(5000);
  //disply robo name and stuff
  clearLCDLine(0);                      // Clear line 1 (0) of the LCD
  clearLCDLine(1);                      // Clear line 2 (1) of the LCD

  bLCDBacklight = true;                 // Turn on LCD Backlight

  displayLCDPos(0,0);                   // Set the cursor to line 0, position 0 (top line, far left)

  displayNextLCDString("Alpha Robotics's");   // Display "Hello from"

  displayLCDPos(1,0);                   // Set the cursor to line 1, position 0 (bottom line, far left)

  displayNextLCDString("     Alpha");   // Display "ROBOTC :]"
wait1Msec(5000);
  }
}


task usercontrol() {

startTask( lcdTask );
{

  while(true)
  {


    if(vexRT[Btn8L] == 1)
    {
      SensorValue[solenoid] = 1;
    }

    if(vexRT[Btn8L] == 0)
    {
      SensorValue[solenoid] = 0;
    }

    if(vexRT[Btn6D]==0)
    {
      SensorValue[claw]=1;
    }

    if(vexRT[Btn6D]==1)
    {
      SensorValue[claw]=0;
    }

     // Set drive motors
    setMultipleMotors(vexRT[Ch2], d1);
    setMultipleMotors(vexRT[Ch3], d2);

    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    if(SensorValue(au)) {

      SensorValue[solenoid] = 1;

      // Stop all motors
      setMultipleMotors(0, a1, a2, a3, a4,);
      setMultipleMotors(0, a5, a6,);
      sleep(200);
      setMultipleMotors(-50, a1, a2, a3,);
      setMultipleMotors(-50, a4, a5, a6,);
      sleep(100);

      // If button 6U (upper right shoulder button) is pressed, active solenoid
      //SensorValue[solenoid] = vexRT[Btn8D];

      }
    else {

      // If B5U is pressed, motors gets set to full forward speed
      // If B5D is pressed, motors get set to full backward speed
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a1, a2, a3, a4,);
      setMultipleMotors((vexRT[Btn5U] - vexRT[Btn5D]) * 100, a5, a6,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a1, a2, a3, a4,);
      //setMultipleMotors((vexRT[Btn7L] - vexRT[Btn7R]) * 127, a5, a6,);

    }
  }
    sleep(20);
 }
}


Quick question with while loops and tasks. When I have an infinite while loop in a task that isn’t delayed and I put stoptask(whatevername); it wouldn’t stop it. how come?

Also how do i make it not flicker? I would put time delays of 10 ms and only clear it right before i need to update it and it still happens…