Help with basic coding...Please

Hi al, I am new to Robotics and coding. To put it simply, I am trying to autonomously control my robot for 2:30 minutes then remote control my robot for the following minute. I have a code file the I am needing some help with. I put comments into it to help understand the file and also the issues I am having. Basically, I am trying to limit the arm with a potentiometer after some other basic commands. The potentiometer code seems to be the issue. I took the timer out because I thought that was part of the problem. Can you give a look and see if there is something obvious that I am doing wrong? We are using the Vex 2.0 Cortex and using Robot C to code. Thanks

#pragma config(Sensor, in1,    armPot,         sensorPotentiometer)
#pragma config(Sensor, in2,    lineFollowerRIGHT, sensorLineFollower)
#pragma config(Sensor, in3,    lineFollowerCENTER, sensorLineFollower)
#pragma config(Sensor, in4,    lineFollowerLEFT, sensorLineFollower)
#pragma config(Sensor, in5,    lightSensor,    sensorReflection)
#pragma config(Sensor, dgtl1,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl5,  frontLimit,     sensorTouch)
#pragma config(Sensor, dgtl6,  sonarSensor,          sensorSONAR_cm)
#pragma config(Motor,  port1,           rightMotor,    tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port8,           clawMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           armMotor,      tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port10,          leftMotor,     tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void moveStraight(int encoderCount)
{
	SensorValue[rightEncoder] = 0;
	SensorValue[leftEncoder] = 0;

  while(SensorValue[leftEncoder] < encoderCount)
  {
    if(SensorValue[leftEncoder] > SensorValue[rightEncoder])
    {
    motor[rightMotor] = 63;
    motor[leftMotor] = 50;
  	}
   if(SensorValue[rightEncoder] > SensorValue[leftEncoder])
    {
    motor[rightMotor] = 50;
    motor[leftMotor] = 63;
  	}
   if(SensorValue[leftEncoder] == SensorValue[rightEncoder])
    {
    motor[rightMotor] = 63;
    motor[leftMotor] = 63;
  	}
  }
}

void turnLeft (int encodercount)
{
	SensorValue[rightEncoder] = 0;	          //Set the encoder so that it starts counting at 0
	SensorValue[leftEncoder] = 0;

  while(SensorValue(rightEncoder) < encodercount)   //While rightEncoder has counted less than parameter, turn left
	{
		//Turn Left
		motor[rightMotor] = 127;					      //Motor on port2 is run at full (127) power forward
		motor[leftMotor]  = -127;				        //Motor on port3 is run at full (-127) power reverse
	}
}


void turnRight (int encodercount)
{
	SensorValue[rightEncoder] = 0;
	SensorValue[leftEncoder] = 0;	            //Set the encoder so that it starts counting at 0

  while(SensorValue(leftEncoder) < encodercount)    //While leftEncoder has counted less than 1800 counts
	{
		//Turn Right
		motor[rightMotor] = -127;				        //Motor on port2 is run at full (-127) power reverse
		motor[leftMotor]  = 127;				      	//Motor on port3 is run at full (127) power forward
	}
}



task main()
{
//This curly Brace opens the task main. This is where autonomous starts.

	while(vexRT[Btn7D] == 0)  //Remote Start Button
	{
		//Do Nothing, unless button is pressed. Once pressed, run next behavior.
	}

	//Had timer here, doesn't seem to be working

	while(SensorValue(lightSensor) > 346)   // While the ambient lightSensor reads a value greater than 346
	{
		// Do nothing
	}

	moveStraight(1800);  //calling on function from the Void above

	motor[rightMotor] = 0;  //Brake for 1 second
	motor[leftMotor]  = 0;
	wait1Msec(1000);

	turnLeft(360);

	motor[rightMotor] = 0;  //Brake for 1 second
	motor[leftMotor]  = 0;
	wait1Msec(1000);

	turnRight(360);

	motor[rightMotor] = 0;  //Brake for 1 second
	motor[leftMotor]  = 0;
	wait1Msec(1000);

	moveStraight(1800);  //calling on function from the Void above

	motor[rightMotor] = 0;  //Brake for 1 second
	motor[leftMotor]  = 0;
	wait1Msec(1000);



	//Potentiometer to limit the arm
	while (1==1)
	{
	if(SensorValue(armPot) < 510)   // While the armPot reads a value less than 510, raise the arm at a power of 63.
	{

	motor[armMotor] = 63;
	motor[leftMotor] = 0;
	motor[rightMotor] = 0;
	}
	else
	{
	motor[armMotor] = 10;
	motor[leftMotor] = 0;
	motor[rightMotor] = 0;
	}
	}  //If this bracket isn't here, arm keep raising. If it is here, arm stops, but nothing after this executes.

	//This is where the problem is. The arm raises and nothing else executes.



//Line Track until sonar senses an obstacle
 
 int threshold = 1352;

  while(SensorValue(sonarSensor) > 20 || SensorValue(sonarSensor) == -1)//Track the line until the sonar detects an obstacle
 	{

    // RIGHT sensor sees dark:
    if(SensorValue(lineFollowerRIGHT) > threshold)
    {
      // counter-steer right:
      motor[leftMotor]  = 63;
      motor[rightMotor] = 0;
    }
    // CENTER sensor sees dark:
    if(SensorValue(lineFollowerCENTER) > threshold)
    {
      // go straight
      motor[leftMotor]  = 63;
      motor[rightMotor] = 63;
    }
    // LEFT sensor sees dark:
    if(SensorValue(lineFollowerLEFT) > threshold)
    {
      // counter-steer left:
      motor[leftMotor]  = 0;
      motor[rightMotor] = 63;
    }
 	}//Closes line track while loop

motor[leftMotor]  = 0;
motor[rightMotor]  = 0;
//Wants to keep tracking line. Figure out why.

//Closes the Timer while loop

//This is where the Remote Control Section starts

while(vexRT[Btn8D] == 0)//Remote Start Button
	{
		//Do Nothing. If pressed, move onto the next behavior
	}

	clearTimer(T2);
  while(time10[T2] < 6000)//Timer Runs for 60 seconds and runs code within the while loop
	{//This curly brace opens the while loop

  	motor[leftMotor] = vexRT[Ch3] / 2;
 		motor[rightMotor] = vexRT[Ch2] / 2;//Remote Control Joysticks

		if(vexRT[Btn6U] ==1 && SensorValue[armPot] < 546)
		{
		motor[armMotor] = 40;
		}
		else if(vexRT[Btn6D] ==1 && SensorValue[frontLimit] == 1)
		{
		motor[armMotor] = -40;
		}
		else
		{
		motor[armMotor] =0;
		}
		//If Button 6U is pushed, raise the arm up at a power of 40, if Button 6D is pushed, lower the arm at a power of 40


	if(vexRT[Btn5U] ==1)
		{
		motor[clawMotor] = 40;
		}
	else if(vexRT[Btn5D] ==1)
		{
		motor[clawMotor] = -40;
		}
	else
		{
		motor[clawMotor] =0;
		}	//If Button 5U is pushed, raise the claw up at a power of 40, if Button 5D is pushed, lower the arm at a power of 40
	//This bracket closes the timer while loop
}
}

edit: code tags added by mods, please remember to use them.

Well, if the problem is that the arm goes to where it is and that it gets to where it SHOULD be, then it’s jsut that the loop doesn’t break out. Make the while loops 1==1 to whatever is in the if statement.

Never underestimate the power of printf (or std::cout if you’re ever using c++). It can tell you a lot about what’s going on

Note that when you use a while loop there is the chance that the code will get stuck in that inner loop and never exit. It helps to either use if statements or provide some method for the program to break out of a while loop. A few options include using counters or timers. Below are some counter examples.

int escapeCount = 100;
while(escapeCount > 0) {
  // your code
  escapeCount--;
  wait1Msec(20);
}

int escapeCount = 100;
while(true) {
  // your code
  escapeCount--;
  wait1Msec(20);
  if(escapeCount < 0) 
    break;
}

The other option for complex code in Cortex programming is to use tasks . They allow different sections of code to run at the same time. You could put your arm control in one task and the chassis control in another. Each can have a while loop

//Notice that the two tasks have different responsibilities
// and control different motors so they don't interfere with 
// each other.

//Global variable can be accessed (get or set) by any task;
int LiftTarget;
	
task LiftControl(){
	//This allows the driver to raise or lower the lift with
	//buttons.  Otherwise the lift will hold position based on
	//the LiftTarget.  If the LiftTarget is changed then the
	//lift will move to that position.
	float PSpeed;
	float kP = 2;
	int error;
	while(true){
		if(vexRT[Btn[6U]){
			//lift up
			motor[liftRight] = 60;
			motor[liftLeft] = 60;
			LiftTarget = SensorValue[liftPot];
		}
		else if(vexRT[Btn[6D]){
			//lift down
			motor[liftRight] = -60;
			motor[liftLeft] = -60;
			LiftTarget = SensorValue[liftPot];
		}
		else{
			//lift hold or autonomous move
			error = LiftTarget - SensorValue[liftPot];
			PSpeed = kP*error;
			motor[liftRight] = PSpeed;
			motor[liftLeft] = PSpeed;
		}
	wait1msec(20); //CPU rest
	}
}

task DriverControl(){
    while(true) {
	  //Left Motors 
	  motor[leftMotor1]  = (vexRT[Ch2] + vexRT[Ch1])/2;  // (y + x)/2
	  motor[leftMotor2]  = (vexRT[Ch2] + vexRT[Ch1])/2;  // (y + x)/2

	  //Right Motors 
	  motor[rightMotor1] = (vexRT[Ch2] - vexRT[Ch1])/2;  // (y - x)/2
	  motor[rightMotor2] = (vexRT[Ch2] - vexRT[Ch1])/2;  // (y - x)/2
	
      //Lift height presets 
      if(vexRT[Btn7U]) LiftTarget = 100;
      if(vexRT[Btn7D]) LiftTarget = 0;
	
      wait1msec(20); //CPU rest
    }
}

task main()
{
  startTask(LiftControl);
  startTask(DriverControl);
  
  while(true){
	//Keep task alive;
	wait1msec(20); //CPU rest
  }
}

More information on RobotC tasks can be found here: RobotC Task Search or start with this thread

3 Likes