Picaxe 20x2 Adc

Sean87

Member
Picaxe 20x2 ADC - 0 to 8 LED's with a POT

Hi all,

I have 20x2 with 8 LED's on my bread board. I also have some potentiometers (10K, 100k and 470K).

First question is which potentiometer to use (Or the value is not important?)

Then I want to do something like potentiometer at it lowest valuse no LED to be ON and then increasing the value, LED's start to turn ON and when potentiometer is at maximum all LED's are on.

Would be nice to hear some instructions :p

Thanks.
 
Last edited:

Sean87

Member
OK I tried some stuff myself, here is the explanation and some questions

I have 8 LED and a 470k pot

I have connected the pot to V+, C.7 and GND

and this is a simple program

Code:
let adcsetup = %0000000000001000

main:
readadc c.7,b1
select b1
case <= 50
low 1,2,3,4,5,6,7
high 0
case >= 100
low 0,2,3,4,5,6,7
high 1
end select
goto main
This seems to work, but how can I set it as I asked in the first post, that is with lowest value there will be no LED on, and with turning the pot handle, as the value increase(or decrese) more LED's get ON untill the pot has reached the max(min) value?

And how can I read the value of "b1"?? or better to say how can I debug it?

Thanks
 

morrismarine

New Member
OK I tried some stuff myself, here is the explanation and some questions

I have 8 LED and a 470k pot

I have connected the pot to V+, C.7 and GND

and this is a simple program

Code:
let adcsetup = %0000000000001000

main:
readadc c.7,b1
select b1
case <= 50
low 1,2,3,4,5,6,7
high 0
case >= 100
low 0,2,3,4,5,6,7
high 1
end select
goto main
This seems to work, but how can I set it as I asked in the first post, that is with lowest value there will be no LED on, and with turning the pot handle, as the value increase(or decrese) more LED's get ON untill the pot has reached the max(min) value?

And how can I read the value of "b1"?? or better to say how can I debug it?

Thanks
you could set the pot to thelowest value then read to b1 and then output that value across all the outputs it should give the binary output you can then vary the pot and see what the leds read in binary and use these as the limits,

label_9: readadc 0,b0
let outpinsB = b0 'Out command
pause 2000 'Wait command
goto label_9
 
Last edited:

Sean87

Member
you could set the pot to thelowest value then read to b1 and then output that value across all the outputs it should give the binary output you can then vary the pot and see what the leds read in binary and use these as the limits,
Can you give more detail please? What I need is some sort of calibration so that I know what will be the value of b.1 (variable which holds ADC read) at the minimum and maximum value of POT.

So you mean I can read a binary value of it using the 8 LED's I have? Can you help with the code also please?

Thanks.
 
Last edited:

MartinM57

Moderator
#terminal 9600
...at the top of the program (or maybe #terminal 4800)

sertxd ("b1 = ". #b1, CR, LF)
...whenever you want to see the value of b1
 

morrismarine

New Member
Can you give more detail please? What I need is some sort of calibration so that I know what will be the value of b.1 (variable which holds ADC read) at the minimum and maximum value of POT.

So you mean I can read a binary value of it using the 8 LED's I have? Can you help with the code also please?

Thanks.
ive edited my post with a bit of code it will require this as a calibration run and then using the found values to make the new program values
 

Sean87

Member
#terminal 9600
...at the top of the program (or maybe #terminal 4800)

sertxd ("b1 = ". #b1, CR, LF)
...whenever you want to see the value of b1
That was a smart advice :p

How stupid I am...ofcourse it would be 0 at min and 254 at max :p hahaha
 

morrismarine

New Member
it should be 0 when the pot is at ground and 254 when its at the top but if your not getting the full travel then its worth checking, you could make a calibration loop that reads the min value, push a button move to max value push a button and then use these two values divided by 8 to give the range for each led
 

Sean87

Member
ive edited my post with a bit of code it will require this as a calibration run and then using the found values to make the new program values
That code didn't work :(

OK can somone help with this? how can I write a SELECT CASE and compare against multiple conditions? The below code does not compile:

Code:
SELECT b1
case 0
low 0,1,2,3,4,5,6,7
case < 32 and > 0
high 0
end select
So how can I write for example(what I used to do in C). I couldn't find an example in the manual (Or the SELECT command is not good for this task?)
Code:
if(VAR>=0 && VAR <=32) 
{
 //DO STUFF
}
 

MartinM57

Moderator
It's all in Manual 2 - worth a read ;)

Syntax:
SELECT VAR
CASE VALUE
{code}
CASE VALUE, VALUE...
{code}
CASE VALUE TO VALUE
{code}
CASE ?? value
{code}
ELSE
{code}
ENDSELECT

...including a worked example at the end
 

eclectic

Moderator
@Sean87

A possibility

Code:
let adcsetup = %0000001000000000
let Dirsb = %11111111

Main:
readadc 9, b0 ; or whichever ADC input

sertxd (#b0," ")

Let b1 = b0/31 

select case b1

case 1
outpinsB = %00000001

case 2
outpinsB = %00000011

case 3
outpinsB = %00000111


case 4
outpinsB = %00001111

case 5
outpinsB = %00011111

case 6
outpinsB = %00111111

case 7
outpinsB = %01111111

case 8

outpinsB = %11111111

endselect

pause 1000
goto main
I've used adc9.
It works in simulation

Apologies for my post #2.
I'm now wearing my reading glasses.

e (And OE)
 

eclectic

Moderator
I think 20X2 does not support "outpinsB", that is my understanding from the manual.
It passes Syntax check.

It simulates.

I have it working on an AXE091 with a 20X2

Modified for case 0
Code:
#picaxe 20X2

let adcsetup = %0000001000000000
let Dirsb = %11111111

Main:
readadc 9, b0 ; or whichever ADC input

sertxd (#b0," ")

Let b1 = b0/31 

select case b1

case 0
outpinsB = %00000000

case 1
outpinsB = %00000001

case 2
outpinsB = %00000011

case 3
outpinsB = %00000111


case 4
outpinsB = %00001111

case 5
outpinsB = %00011111

case 6
outpinsB = %00111111

case 7
outpinsB = %01111111

case 8

outpinsB = %11111111

endselect

pause 1000
goto main
 

Attachments

MartinM57

Moderator
You are correct...but page 20,manual 2.
When used on the left of an assignment ‘pins’ applies to the ‘output’ pins e.g.
let pinsB = %11000000
will switch outputs 7,6 high and the others low.
 

Sean87

Member
It passes Syntax check...
Thanks, it works nice!

I also made up the code below. I wonder if it possible to optimize it (Like I wouldn't need to call "LOW 0,1,2,3,4,5,6,7" every time and use a symbol or function instead of it).

Code:
let adcsetup = %0000000000001000

main:
readadc c.7,b1

SELECT b1

CASE 0
low 0,1,2,3,4,5,6,7

CASE 1 TO 31
high 0

CASE 31 TO 62
low 0,1,2,3,4,5,6,7
HIGH 0,1

CASE 62 TO 93
low 0,1,2,3,4,5,6,7
HIGH 0,1,2

CASE 93 TO 124
low 0,1,2,3,4,5,6,7
HIGH 0,1,2,3

CASE 124 TO 155
low 0,1,2,3,4,5,6,7
HIGH 0,1,2,3,4

CASE 154 TO 185
low 0,1,2,3,4,5,6,7
HIGH 0,1,2,3,4,5

CASE 185 TO 216
low 0,1,2,3,4,5,6,7
HIGH 0,1,2,3,4,5,6

CASE > 216
low 0,1,2,3,4,5,6,7
HIGH 0,1,2,3,4,5,6,7

ENDSELECT

goto main
 

Sean87

Member
You are correct...but page 20,manual 2.
Hmm this also didn't work, no change in any LED's. What can be wrong? this is the final code I used to show binary value on LED's

Code:
let adcsetup = %0000000000001000

label_9: 
 readadc c.7,b1

 let pinsB = b1 'Out command
 pause 2000 'Wait command
 goto label_9
 

eclectic

Moderator
Try this:

Code:
let adcsetup = %0000000000001000

let DirsB=%11111111 ;***********************

label_9: 
 readadc c.7,b1
 
 Sertxd (#b1," ") ; *******************

 let pinsB = b1 'Out command
 pause 2000 'Wait command
 goto label_9

e
 

Sean87

Member
Try this:

Code:
let adcsetup = %0000000000001000

let DirsB=%11111111 ;***********************

label_9: 
 readadc c.7,b1
 
 Sertxd (#b1," ") ; *******************

 let pinsB = b1 'Out command
 pause 2000 'Wait command
 goto label_9

e
Thanks, this one was the best!
 

inglewoodpete

Senior Member
A couple of points.

1. I think eclectic meant Manual 2 Version 7.7

2. Sean77 meantioned way back that he was using a 470k potentiometer. This value is so high that you will probably get non-linearity problems. The maximum recommended impedance for an ADC input is 10k.
 

eclectic

Moderator
A couple of points.

1. I think eclectic meant Manual 2 Version 7.7

2. Sean77 meantioned way back that he was using a 470k potentiometer. This value is so high that you will probably get non-linearity problems. The maximum recommended impedance for an ADC input is 10k.
1. Either actually. I should have said
the manual that comes with PE 5.40

e
 

Sean87

Member
A couple of points.

1. I think eclectic meant Manual 2 Version 7.7

2. Sean77 meantioned way back that he was using a 470k potentiometer. This value is so high that you will probably get non-linearity problems. The maximum recommended impedance for an ADC input is 10k.
Yes I am using a 470k pot, but it seems that it does react linear. But anyway I will change it with a 10k soon.
 
Top