Congratulations Rich, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Reading Analog Port

Assistance Requested

Help Robot Doc with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

Robot Doc claims to have checked these sources:

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
# Read the ADC port value

$Read_Front = $Front_IR

IF ($Read_Front <= $Collision)


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

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.

Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


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;


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

Then where you need to check the values simply



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


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.