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

Could someone look at this code?
http://www.robotc.net/forums/viewtopic.php?f=52&t=10684
Page 1 of 1

Author:  Cyks [ Sat Mar 14, 2015 9:47 pm ]
Post subject:  Could someone look at this code?

We want our robot to check the sensor reading move forward and check again. If the second sensor value is less than the first, go forward, else go back. The robot moves forward twice and then the program stops so I think the problem may be in the if statement. Do you guys see problems in this code?

USSideDist[USSideDistIndex] = SensorValue(SonarSensor);//sets the current value of the USSensor to the first value in this array
USSideDistIndex = USSideDistIndex + 1;

motor[leftmotor] = 40;
motor[rightmotor] = 40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;

wait1Msec(400);

USSideDist[USSideDistIndex] = SensorValue(SonarSensor);//sets the current value of the USSensor to the first value in this array
USSideDistIndex = USSideDistIndex + 1;

if (USSideDist[USSideDistIndex] < USSideDist[USSideDistIndex - 1])//if the latest sensor reading is a less value than the one before it
{
//move forward
motor[leftmotor] = 40;
motor[rightmotor] = 40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;
}
else
{
//move backward
motor[leftmotor] = -40;
motor[rightmotor] = -40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;
}

Author:  MHTS [ Wed Mar 18, 2015 3:21 pm ]
Post subject:  Re: Could someone look at this code?

It's very hard to understand what your code is trying to do. So I am trying to figure out what you want to do from your description. If I understand correctly, are you trying to use a sonar sensor to detect whether you are aligned with the center goal? It looks like you are mounting the sonar sensor to the side so you would continue to go forward until the sonar sensor reads a value indicating the center goal is directly to your (left or right) side. You continue to go forward until the sonar sensor passes the center goal and reads a much bigger value, then you go back. If that's the case, you don't need an array. You can do something like the following.
Code:
//
// Go forward until you see the center goal to your side.
//
while (SensorValue[SonarSensor] > SOME_DISTANCE_THRESHOLD)
{
   motor[leftmotor] = 40;
   motor[rightmotor] = 40;
   wait1Msec(400);
}
//
// Continue to go forward until you pass the center goal
//
while (SensorValue[SonarSensor] <= SOME_DISTANCE_THRESHOLD)
{
   motor[leftmotor] = 40;
   motor[rightmotor] = 40;
   wait1Msec(400);
}
//
// Move backward for a set period of time and stop.
//
motor[leftmotor] = -40;
motor[rightmotor] = -40;
wait1Msec(400);
motor[leftmotor] = 0;
motor[rightmotor] = 0;

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