Ultrasonic Rangefinder Forward till Near - ROBOTC.net
Ultrasonic Rangefinder Forward till Near - ROBOTC.net
Ultrasonic Rangefinder Forward till Near - ROBOTC.net
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Sensing<br />
<strong>Ultrasonic</strong> <strong>Rangefinder</strong> <strong>Forward</strong> <strong>till</strong> <strong>Near</strong> (cont.)<br />
Auto<br />
Auto<br />
Auto<br />
Auto<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
12<br />
13<br />
14<br />
15<br />
16<br />
17<br />
18<br />
19<br />
20<br />
21<br />
22<br />
23<br />
24<br />
25<br />
26<br />
27<br />
28<br />
29<br />
30<br />
31<br />
32<br />
const tSensors rightEncoder = (tSensors) in2;<br />
const tSensors leftEncoder = (tSensors) in3;<br />
const tSensors sonarSensor = (tSensors) in5;<br />
void moveStraight()<br />
{<br />
if(SensorValue[leftEncoder] > SensorValue[rightEncoder])<br />
{<br />
motor[port3] = 50;<br />
motor[port2] = 63;<br />
}<br />
if(SensorValue[leftEncoder] < SensorValue[rightEncoder])<br />
{<br />
motor[port3] = 63;<br />
motor[port2] = 50;<br />
}<br />
if(SensorValue[leftEncoder] == SensorValue[rightEncoder])<br />
{<br />
motor[port3] = 63;<br />
motor[port2] = 63;<br />
}<br />
}<br />
task main()<br />
{<br />
wait1Msec(2000);<br />
bMotorReflected[port2] = 1;<br />
SensorValue[leftEncoder] = 0;<br />
SensorValue[rightEncoder] = 0;<br />
while(SensorValue[sonarSensor] > 3)<br />
{<br />
moveStraight();<br />
}<br />
}<br />
10. Download and Run.<br />
Checkpoint<br />
© Carnegie Mellon Robotics Academy / For use with VEX ® Robotics Systems<br />
10. Go to Compile and Download Program<br />
9n. Add this code<br />
Add these commands to<br />
reset the values of the<br />
encoders to 0.<br />
<strong>ROBOTC</strong><br />
9o. Modify this code<br />
Replace the generic motorforward<br />
commands with a<br />
call to moveStraight();.<br />
Your robot now runs straight until its <strong>Ultrasonic</strong> Sonar <strong>Rangefinder</strong> detects an object within<br />
3 inches, in its forward field of view. You can change the stopping distance by changing<br />
the 3 inch “cutoff” point.<br />
<strong>Ultrasonic</strong> <strong>Rangefinder</strong> • 10