ADC compare

Phil bee

Member
Hi folks, can any one help with a problem I just can't get my head around.

What i am trying to do is read 2 adc pins and compare them, if the voltage on both is within 20% then loop to main prog but if one is more than 20% above the other then switch a motor on
The motor should turn c/w or cc/w dependent upon which adc reading is higher.

Any ideas PLEASE.

Sorry I forgot to say I need some way of avioding rollover or under for the variables so that the motor drive dose not go in the wrong direction.

This is my first atempt at writing a program for any thing but if I can get this working then I have loads of ideas for projects so please help out an "old fart".
 
Last edited:

eclectic

Moderator
@Phil bee
Firstly, this section of the Forum
is for finished projects.
You'll get a better response on the Main / Active Forum.

In the meantime however, until someone posts a better solution,
here's a rough idea:

1. Find the difference.
2. Multiply it by 5

3. If it's bigger than b0 or b1, then do things.
4. If not, goto quit.

Code:
symbol diff = w1
Main:
	readadc 2,b0	
	readadc 1,b1
	
if b1 > b0 then one
if b0 > b1 then two	

goto main

one:
let diff = b1 - b0
let diff = diff *5 ; multiply by 5
if  diff > b1 then ; greater than 20%
goto cw
endif
goto quit

two:
let diff = b0 -b1
let diff = diff *5
if  diff > b0 then
goto ccw
endif
goto quit


cw:
low 1		
high 0		
goto main		

ccw:
low 0
high 1
goto main

quit:  
low 0
low 1
goto main
e
 

hippy

Ex-Staff (retired)
[Moved to Active Forum]

The specification is a little ambiguous because there can be a case where the first is within 20% of the second, but the second is not within 20% of the first. You need to be clear about what "20% of" is "of".

This code doesn't do exactly what you want but should give an idea of how I'd approach it. The byte readings 0..255 are shifted to be offset about $8000 to avoid underflow and overflow issues and allow working with READADC10. The IF-THEN needs to be put on one line ...

Code:
ReadAdc 0, rawX
ReadAdc 1, rawY

percent20X = rawX * 20 / 100
percent20Y = rawY * 20 / 100

X = rawX + $8000
Y = rawY + $8000

Xplus20percent  = X + percent20X
Xminus20percent = X - percent20X

Yplus20percent  = Y + percent20Y
Yminus20percent = Y - percent20Y

If X > Yplus20percent Or
   X < Yminus20percent Or
   Y > Xplus20percent Or
   Y < Xminus20percent
Then
  Gosub OutsideLimits
Else
  Gosub WithinLimts
End If
 
Last edited:

papaof2

Senior Member
$8000 (hex) is 32768. It is added to the ADC value so math won't go negative when subtracting values.
 

Phil bee

Member
adc compare

Thank you for the replies they have given me somthing to try.

The idea is to monitor 2 sensors and use the value to turn a motor one way or the other, but if the reading from the sensors is within about 0.5 volt or less of each other themotor stays off so that it won't hunt back and forth all the time.
 

Phil bee

Member
ADC Compare

I got it at last !! and it works so thanks guys for some gentle nudging.

This is my sollution to comparing two adc inputs.
 
Last edited:
Top