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

Could someone look over my code?
http://www.robotc.net/forums/viewtopic.php?f=11&t=1209
Page 1 of 1

Author:  fizxguy [ Tue Feb 10, 2009 10:13 pm ]
Post subject:  Could someone look over my code?

I am following the VEX lessons on the website and so far have had no problems. Then in the Autonomy section where we have to a boolean variable , i feel like we followed the directions but yet the robot is not responding correctly. Basically we want an have operator assist by running a moving straight function using the lower Ch5 button. We want to end an Idle loop by using the upper Ch5 button. We want to operate an arm using the buttons on ch6. Everything compiles correctly with no errors, but yet we should be able to disengage the idle loop, have RC when we want it, be able to use the move straight function if we need it and be able able to operate the arm. Hope that is clear. See the code below.

Thanks - Ken



const tSensors downLimit = (tSensors) in1; //sensorTouch //*!!!!*//
const tSensors rightEncoder = (tSensors) in2; //sensorRotation //*!!!!*//
const tSensors leftEncoder = (tSensors) in3; //sensorRotation //*!!!!*//
const tSensors upLimit = (tSensors) in4; //sensorTouch //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//

void moveStraight()
{if(SensorValue[leftEncoder] > SensorValue[rightEncoder])
{
motor[port3] = 50;
motor[port2] = 63;
}
if(SensorValue[leftEncoder] < SensorValue[rightEncoder])
{
motor[port3] = 63;
motor[port2] = 50;
}
if(SensorValue[leftEncoder] == SensorValue[rightEncoder])
{
motor[port3] = 63;
motor[port2] = 63;
}}

task main()
{
int armDirection;
bool isMovingStraight;
bIfiAutonomousMode = false;
while(vexRT[Ch5] == 0)
{
}
bMotorReflected[port2] = 1;

armDirection = 0;
isMovingStraight = false;
ClearTimer(T1);

while(time10[T1] < 12000)

{

if(armDirection == 1)
{
if(SensorValue(upLimit) == 0)
{
motor[port6] = 31;
}
else
{
motor[port6] = 0;
}

if(armDirection == -1)
{
if(SensorValue(downLimit) == 0)
{
motor[port6] = -31;
}
else
{
motor[port6] = 0;
}
if(armDirection == 0)
{
motor[port6] = 0;
}

if(vexRT[Ch6] == -127)

{
armDirection = 1;
}


if(vexRT[Ch6] == 127)

{
armDirection = -1;
}

if(vexRT[Ch5] == -127)
{
if(isMovingStraight == false)
{
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
isMovingStraight = true;
}
else
{
moveStraight();
}
}
else
{
isMovingStraight = false;
motor[port3] = vexRT[Ch3];
motor[port2] = vexRT[Ch2];
}

}
}
}
motor[port3] = 0;
motor[port2] = 0;
}

Author:  jbflot [ Mon Feb 16, 2009 2:00 pm ]
Post subject:  Re: Could someone look over my code?

Hi Ken -

I think I've found the source of your problems. In your program, you have two if statements that don't get closed until almost the very end of the program. If I copy your code and paste it directly into ROBOTC, the if statements I'm referring to begin on lines 38 and 49. There should be closing curly braces on lines 48, and after 58 to close each, I think (and two less toward the end of the program). By not closing the if statements in the correct places, all the following code gets put in one giant if statement... which would explain the weird, undesirable behavior.

Let me know if that helps.

Author:  fizxguy [ Wed Feb 25, 2009 4:45 pm ]
Post subject:  Re: Could someone look over my code?

THANKS! WORKS like a charm now! I think I will stress using the TAB key to make sure all brackets line up with the control structure they belong to.

Author:  fryfrog [ Fri Mar 20, 2009 12:33 am ]
Post subject:  Re: Could someone look over my code?

Code formatting goes a huge distance in making things readable. On forums, you can use [ code ] and [ /code ] to enclose your nicely formatted code and make it easier to read.

RobotC also has some formatting options, so if your program isn't already nicely aligned you can easily apply some standard alignment rules which will make things like this totally obvious.

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