Asked — Edited
Resolved Resolved by Rich!

Reading Analog Port

Need help with what steps are missing when making a decision during an "IF" leg

Two IR units, Front_IR and Rear_IR will be monitored for

possible collision from objects in front or behind a

a moving robot

Once detected then drive motor control will be adjusted

to avoid collision

$Front_IR = GetADC(ADC0) $Rear_IR = GetADC(ADC1)

Normal values for an ADC port are from 0 to 255

Collision value should be 100 or less

$Collision = 100

Now we start up the robot at a low speed - 0 is stop

and 255 is Max

Forward(70)

Read the ADC port value

:Read_ADC $Read_Front = $Front_IR print(GetADC(adc0)) sleep(2000)

IF ($Read_Front <= $Collision) Stop() Sleep(500) Goto(Avoid)

EndIf


No matter what the ADC0 port value is, the GOTO is not performed when the port value changes from 196 to 3.


ARC Pro

Upgrade to ARC Pro

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.

United Kingdom
#1  

Try changing the READ_ADC part to;


$Read_Front = GetADC(ADC0)

I suspect, although am not entirely sure that the $Front_IR variable is only actually checked at the very start of the code and the value stored, then by using $Read_Front = $Front_IR all you are doing is saving the same value that was once got.

In other words;

$Read_Front checks the ADC port and returns a value of 120. $Read_Front is now 120 When you use the code $Front_IR = $Read_Front you are saving this 120 to $Front_IR too. So the ADC value is only really gotten once and then the variable remains the same throughout the script.

If you wanted the values of the IR sensors read multiple times in the script and wanted to save coding you could do a small sub routine;


:Read_Sensors
$Front_IR = GetADC(ADC0)
$Rear_IR = GetADC(ADC1)
Return()

Then where you need to check the values simply


Goto(Read_Sensors)

The script will jump to the label, perform the GetADCs then return to where it was.

#2  

Your suspicion was correct Rich, The statements were getting a reading and storing it and the same value was used for decision making and since once the ADC port value was saved the script did not revisit the ADC port and update the values.

That explains why the IF statement worked if I held my hand up to the IR sensor and then started the script. Thanks for the helpful clues.