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

programming help
http://robotc.net/forums/viewtopic.php?f=11&t=1304
Page 1 of 1

Author:  bradford [ Wed Mar 04, 2009 10:01 am ]
Post subject:  programming help

I am having trouble getting the program to end when the timer expires. I want the robot to stop once the 5 seconds is up no matter where it is in the program. The program below is not working. It seems to be ignoring the timer altogether. Any suggestions?

#pragma config(Sensor, in1, downLimit, sensorTouch)
#pragma config(Sensor, in2, rightEncoder, sensorQuadEncoder, int5)
#pragma config(Sensor, in3, leftEncoder, sensorQuadEncoder, int6)
#pragma config(Sensor, in4, upLimit, sensorTouch)
#pragma config(Sensor, in5, sonarSensor, sensorSONAR, int1)
#pragma config(Sensor, in6, Touch, sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

// power level set at 30
void moveStraight()

{
if(SensorValue[leftEncoder] > SensorValue[rightEncoder])
{
motor[port3] = 25;
motor[port2] = 30;
}
if(SensorValue[rightEncoder] > SensorValue[leftEncoder])
{
motor[port3] = 30;
motor[port2] = 25;
}
if(SensorValue[leftEncoder] == SensorValue[rightEncoder])
{
motor[port3] = 30;
motor[port2] = 30;
}
}


task main()
{
wait1Msec(2000);
bMotorReflected[port2] = 1; //Reflects the direction of the motor on port2
ClearTimer(T4);
int i = 0;

while(time10[T4] < 500 && i < 1)
{
if(time10[T4] < 500)
{
while(SensorValue[downLimit] == 0)
{
motor[port6] = -31; //move arm down to limit switch
}
}

if(time10[T4] < 500)
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
while(SensorValue[sonarSensor] >= 3 || SensorValue[sonarSensor] <= 0)
{
moveStraight();
}
motor[port3] = 0;
motor[port2] = 0;
wait1Msec(250);
}

if(time10[T4] < 500)
{
while(SensorValue[upLimit] == 0)
{
motor[port6] = 31;
}
}
i++;
}
motor[port3] = 0;
motor[port2] = 0;
motor[port6] = 0;
}

Author:  fryfrog [ Fri Mar 20, 2009 12:39 am ]
Post subject:  Re: programming help

I'm not the best at visually decoding and debugging, I usually have to compile and test on a bot.

I think your problem is with the while loop, right outside of it you set i to 0. Your while loop says "while i'm less than 500 seconds and also while i is less than 1"

At the end of your first loop, you increment i by 1 and i is no longer less than 1.

I think :)

Code:
  while(time10[T4] < 500 && i < 1)
  {
    if(time10[T4] < 500)
    {
      while(SensorValue[downLimit] == 0)
      {
        motor[port6] = -31; //move arm down to limit switch
      }
    }

    if(time10[T4] < 500)
    {
      SensorValue[leftEncoder] = 0;
      SensorValue[rightEncoder] = 0;
      while(SensorValue[sonarSensor] >= 3 || SensorValue[sonarSensor] <= 0)
      {
        moveStraight();
      }
      motor[port3] = 0;
      motor[port2] = 0;
      wait1Msec(250);
    }

    if(time10[T4] < 500)
    {
      while(SensorValue[upLimit] == 0)
      {
        motor[port6] = 31;
      }
    }
    i++;
  }

Author:  fryfrog [ Fri Mar 20, 2009 12:41 am ]
Post subject:  Re: programming help

Also, I think the "if" portion of the following is redundant, you should not need that. The while loop will run so fast, that you'll catch 5 seconds very quickly.

Also, you are going to get stuck in those while loops when the value is met (maybe you mean to do this).

Code:
    if(time10[T4] < 500)
    {
      while(SensorValue[downLimit] == 0)
      {
        motor[port6] = -31; //move arm down to limit switch
      }
    }

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