The ultrasonic sensor proved to be difficult to set up. But with a few hacks with the help of John Legoman, it came through.
The main difficulty of the sensor is that it often fluctuates between -1 and the actual distance reading. This creates problems when our code instructs the robot to stop whenever it sees a value less than 3(stop when it's 3 inches away from a wall).
John fixed this issue by converting the sensor readings with an unsigned byte variable. This warps negatives over to the max values, and thus -1 becomes 255.
unsigned byte sonarValue
sonarValue = SensorValue[sonar]
With this conversion, the code worked beautifully.
void stopdistance(int distance);
{
unsigned byte sonarValue
sonarValue = SensorValue[sonar]
while(sonarValue < distance)
{
motor[motorL] = 0
motor[motorR] = 0
}
}
task main()
while(1==1)
{
motor[motorL] = 80
motor[motorR] = 75
stopdistance(3);
}
No comments:
Post a Comment