Readadc10

alhoop

Member
I am using the following code to get a variable delay up to 60 seconds to control a sound board
in model railroad applications. As i know nothing about Readadc10 is the following code efficient.
In max delay I get 63.1 to 63.8 seconds which is accurate enough for my application - seems like
the +/- 1 bit showing up. I go from 08M2 vcc(4.5 volts) through a 3.9k resistor and a 5 kohm pot
to ground with the pot wiper connected to ADC input pin C.1.
I found that any voltage above 2.56 volts gave random readings. Is this normal?
Ps. I found the answer, my step of 100 was going to 102300, way over the limit of 65000+.
thanks
Al
Code:
;Picaxe 08M2 program for Sound board adapter

start:                            
pause 1000                 ;ensure that train detection is captured.
if pinc.3 <> 0  then      ;lcheck to see if controller has detected
                                     ;a train. if so   
high c.2                       ;pass +12 Vdc to sound board to activate
readadc10 C.1,w1
w1 = w1 * 100
debug w1                    ; wait for crossing gates to come down 
pause w1
endif 
                   
cont:                           
low c.2                         ;deactivate sound board
if pinc.3 = 1 then cont ;keep sound board deactivated until
                                     ;gates go up and another train is detected
goto start                     ;go back to check for another train
 
Last edited:

g6ejd

Senior Member
Your potential divider will not provide more than 2.52 volts and at that voltage the Pot R will be maximum, I suspect it is the Pot causing your random readings. Suggest changing 3.9 to 3.3K if you want a more reliable sweep between 0 and 2.5volts.
 

hippy

Ex-Staff (retired)
With the pot connected to 4.5V you can probably drop the 3.9K resistor completely and put the pot across the 4.5V and 0V rail.

That will then return a READADC10 value of 0 to 1023. To get that to 60s (60,000ms) each step would be 58.65ms. A value of 59 would therefore be a good choice as that will not overflow the 16-bit (65,535) maximum allowed ...

ReadAdc C.1, w1
w1 = w1 * 59
Pause w1
 

eclectic

Moderator
@Al
Please see:
http://www.picaxeforum.co.uk/showthread.php?7679-Read-Me-First!

Code:
;Picaxe 08M2 program for Sound board adapter
start:                            ;line 1
pause 1000                ;ensure that train detection is captured.
if pinc.3 <> 0  then      ;lcheck to see if controller has detected
                                     ;a train. if so   
high c.2                       ;pass +12 Vdc to sound board to activate
readadc10 C.1,w1
w1 = w1 * 100
debug w1                    ; wait for crossing gates to come down 
pause w1
endif 
                   
cont:                           
low c.2                         ;deactivate sound board
if pinc.3 = 1 then cont ;keep sound board deactivated until
                                     ; gates go up and another train is detected
goto start                     ;go back to check for another train
Is easier than someone opening a text file

e
 

alhoop

Member
With the pot connected to 4.5V you can probably drop the 3.9K resistor completely and put the pot across the 4.5V and 0V rail.

That will then return a READADC10 value of 0 to 1023. To get that to 60s (60,000ms) each step would be 58.65ms. A value of 59 would therefore be a good choice as that will not overflow the 16-bit (65,535) maximum allowed ...

ReadAdc C.1, w1
w1 = w1 * 59
Pause w1
Thanks Hippy:
That worked perfectly - gave a linear delay from close to zero up to 60 seconds. Where is 'step' explained and why was
the table in readadc dropped in later editions of the manual?
Al
 

hippy

Ex-Staff (retired)
Where is 'step' explained
It's not explained as such; it's what you would need to multiply a 0 to 1023 value by to get a 0 to 60000 result. That is -

1023 * STEP = 60000

STEP = 60000 / 1023 = 58.65

and why was the table in readadc dropped in later editions of the manual?
I am not sure which table that would be. It may have related to details of the PICAXE-08 low-resolution ADC which is fairly redundant these days as the PICAXE-08 has been superseded.
 
Top