PDA

View Full Version : Comparing values in an array...



Matt White
07-17-2006, 11:36 AM
I'm using an ADC0834 to process analogˇreadings from 4 Sharp GP2D12 sensors.ˇ All that works fine.ˇ I have an array called adc(0,1,2, ...).ˇ Essentially what I want to do is evaluate the four sensor readings stored in the array and find out which one has the largest value.ˇ While this seems like an easy task when I start coding it, the excersize turns into a lots and lots of IF, THEN, ELSE statements... Anyone got a better way to do this?ˇ

Would I be better off just checking each value against a set of threasholds?ˇ The application is colision avoidance -- I'd also like to know "how far" I am from an object, wall, etc.ˇ The idea being the closer I am to an obstacle the more of a course correction I need to make.

Thanks!
Matt
ˇ

Mike Green
07-17-2006, 12:43 PM
Are the values all evaluated the same way? How about a FOR loop?



lastFound = 0
highest = adc(0)
for i = 1 to 7
if adc(i) < highest then skipIt
highest = adc(i)
lastFound = i
skipIt:
next



The testing in the loop can be more complex as long as all items are treated alike.

Matt White
07-17-2006, 09:19 PM
Perfect. Thanks Mike!