ROBOTC.net forums
http://www.robotc.net/forums/

while loops and timers issue
http://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;
    }
  }
}
}
}
}


Attachments:
final test program.c [3.48 KiB]
Downloaded 555 times

Author:  rcahoon [ Wed Jul 10, 2013 7:27 pm ]
Post subject:  Re: while loops and timers issue

Hi jgarrett,
Let me make a couple of suggestions, and if you still can't figure it out, let me know:

1. In ROBOTC, select all your code, then select Edit>>Code Formatting>>Format Selection from the menu. This will properly indent your code so that it's easier to see which bits of code are grouped together within while and if statements. I suspect your code isn't doing what you think it is or want it to.

2. Remember that a while loop will cause only its code to be run until it's condition is false. If you want your program to do two things at once, such as go straight and detect objects, they will both need to be inside the same while loop.

Best,
--Ryan

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/