View unanswered posts | View active topics It is currently Sat Oct 25, 2014 3:08 am






Reply to topic  [ 2 posts ] 
while loops and timers issue 
Author Message
Rookie

Joined: Mon Jul 08, 2013 12:53 pm
Posts: 1
Post 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 192 times
Mon Jul 08, 2013 12:57 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post 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

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Wed Jul 10, 2013 7:27 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.