ROBOTC.net forumshttp://www.robotc.net/forums/ while loops and timers issuehttp://www.robotc.net/forums/viewtopic.php?f=11&t=6194 Page 1 of 1

Author:  jgarrett [ Mon Jul 08, 2013 12:57 pm ]
Post subject:  while loops and timers issue

I am teaching a class on RobotC basics and Vex and am new to robotc. Students and I are trying to write code for a simple program that has use a timer for 10 to 20 seconds to control the robot. While under the timer loop (while statement) the robot needs to go forward until it senses an object turn left or right then go forward turn left or right then go straight. After 10 seconds gives the user control of the robot through joystick controller. So far we can either get it to sense the object or go straight for 10 seconds then give up control but not both. Example code below. Thoughts?
 Code:task main(){int normalSpeed;int slowSpeed;normalSpeed = 103;slowSpeed = 63;  ClearTimer(T1);  while(time1[T1] < 10000)  {   if(SensorValue[sonarSensor] > 3)    {      motor[leftMotor] = 103;      motor[rightMotor]= 63;    }   else // see wall    {      motor[rightMotor]= 0;      motor[leftMotor] = 0;    }   wait1Msec(500);   SensorValue[leftEncoder] = 0;   SensorValue[rightEncoder] = 0;   while(SensorValue[rightEncoder] < 900)   {      motor[leftMotor] = 103;      motor[rightMotor] = -103;   }   SensorValue[leftEncoder] = 0;   SensorValue[rightEncoder] = 0;   while(SensorValue[leftEncoder] < 900)    {    if(SensorValue[leftEncoder] > SensorValue[rightEncoder])    {    motor[rightMotor] = slowSpeed;    motor[leftMotor] = normalSpeed;    }    if(SensorValue[rightEncoder] > SensorValue[leftEncoder])     {     motor[rightMotor] = slowSpeed;    motor[leftMotor] = normalSpeed;    }    if(SensorValue[leftEncoder] == SensorValue[rightEncoder])      {    motor[rightMotor] = normalSpeed;    motor[leftMotor] = normalSpeed;     }    }    SensorValue[leftEncoder] = 0;    SensorValue[rightEncoder] = 0;   while(SensorValue[leftEncoder] < 900)   {      motor[leftMotor] = 103;      motor[rightMotor] = -103;   }   while(SensorValue[leftEncoder] < 1440)   {      if(SensorValue[leftEncoder] > SensorValue[rightEncoder])    {    motor[rightMotor] = slowSpeed;    motor[leftMotor] = normalSpeed;    }    if(SensorValue[rightEncoder] > SensorValue[leftEncoder])     {     motor[rightMotor] = slowSpeed;    motor[leftMotor] = normalSpeed;    }    if(SensorValue[leftEncoder] == SensorValue[rightEncoder])      {    motor[rightMotor] = normalSpeed;    motor[leftMotor] = normalSpeed;  {  }   while(1 == 1)  {    //Driving Motor Control    motor[leftMotor] = vexRT[Ch3] / 2;    motor[rightMotor] = vexRT[Ch2] / 2;    //Arm Control    if(vexRT[Btn6U] == 1)    {      motor[armMotor] = 40;    }    else if(vexRT[Btn6D] == 1)    {      motor[armMotor] = -40;    }    else    {      motor[armMotor] = 0;    }  }}}}}