View unanswered posts | View active topics It is currently Sun Jul 24, 2016 6:43 am

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
while loops and timers issue
Author Message
Rookie

Joined: Mon Jul 08, 2013 12:53 pm
Posts: 1
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;    }  }}}}}

Mon Jul 08, 2013 12:57 pm

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

Wed Jul 10, 2013 7:27 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning