Help with setting adc in program editor

GAP

Senior Member
I am trying to set the levels from LDRs using adc in program editor.

I was shown a way on how to set them so that false triggering is eliminated but can't for the life of me remember how it was done.

I have a vague memory of a panel in editor where I could put in the levels but just can't remember what I was shown.

Is there any help manual for editor that might explain the sequence for me?
 

nick12ab

Senior Member
I am trying to set the levels from LDRs using adc in program editor.
Do you mean set the ADC levels in the simulator? In which case eclectic is right - Manual 1 page 68. If you are using readadc10 instead of normal readadc then you must use the Generic field instead to set the value.

I was shown a way on how to set them so that false triggering is eliminated but can't for the life of me remember how it was done.
If you're talking about the ADC calibration wizard, then in Programming Editor, click Analogue Sensor Calibration... on the PICAXE menu.
 

GAP

Senior Member
If you're talking about the ADC calibration wizard, then in Programming Editor, click Analogue Sensor Calibration... on the PICAXE menu.

main: readadc 0,b1 ; read value into b1
if b1 > 50 then flsh ; jump to flsh if b1 > 50
goto main ; else loop back to start

flsh: high B.1 ; switch on output B.1
pause 1000 ; wait 1 second
low B.1 ; switch off output B.1
pause 1000 ; wait 1 second
goto main ; loop back to start

If I use this example how do I set b1 value in the simulator to make it work in simulator?

Do I need to have a measured value to put into b1?

Please understand that I am totally new to programming and really have not much of an idea what I am doing.
 
Last edited by a moderator:

GAP

Senior Member
Thanks for the advice guys got that sorted.

The next question is if I want to use as an input, the output of a voltage divider comprising 2 LDRs, how do I put that into the variable?

Do I just say b1=3 for a high and b0=2 for a low with 2&3 being the voltage reading from the divider?
 

nick12ab

Senior Member
The next question is if I want to use as an input, the output of a voltage divider comprising 2 LDRs, how do I put that into the variable?
The simulator doesn't support component simulation - you will have to just enter an ADC value.

Do I just say b1=3 for a high and b0=2 for a low with 2&3 being the voltage reading from the divider?
??????
 

SAborn

Senior Member
My gut feeling is you will need to use each LDR as a seperate input then read each input and decide in code what action to take.
Without more details of your circuit and what is is to do and control, it is hard to answer your question on how to code the reading from the LDRs.
 

erco

Senior Member
I think in this case it's harder to simulate than it is to build the real hardware. Do you have a PICAXE and two fairly matched LDRs?

If you READADC the junction of 2 LDRs in series across 5V, the byte variable returned will range from 0-255. ADC10 will yield values 0-1024. Values depend only on the relative difference in illumination on the LDRs, not the overall light level. Expect to see around 127 when both LDRs see the same light level. When the one connected to ground is darker, the value will increase and vice-versa.

Per SAborn, if you have one more i/o pin to spare, you can get more useful info by connecting one LDR & fixed resistor to two seperate pins. You'll get overall light level plus relative level between sensors.
 

cravenhaven

Senior Member
Thanks for the advice guys got that sorted.

The next question is if I want to use as an input, the output of a voltage divider comprising 2 LDRs, how do I put that into the variable?

Do I just say b1=3 for a high and b0=2 for a low with 2&3 being the voltage reading from the divider?
There are two ways of achieving what you want.
Either adjust the simulated ADC values as Nick showed in post #5 or
single step the code and immediately after the readadc command you can poke a value into b1. As Erco says in post #9 the value would be in the range of 0-255 and if the two LDR's were in series and equally lit then the value to insert would be approx 127.
 

GAP

Senior Member
There are two ways of achieving what you want.
Either adjust the simulated ADC values as Nick showed in post #5 or
single step the code and immediately after the readadc command you can poke a value into b1. As Erco says in post #9 the value would be in the range of 0-255 and if the two LDR's were in series and equally lit then the value to insert would be approx 127.

What I have is 2 LDRs in series between 0 & 5V rails and I am taking an output from between them ie a voltage divider.

I can get a measurable voltage at the mid point using a multimeter that varies depending on which LDR is darkened, I now want to use that voltage as an input to my picaxe to get it to perform tasks.

My question is how do I convert for example 4V into something that picaxe can read.
I will be using readadc and I want to set limits so that the 4V equals a high but 2V equals a low.

Do I assign the voltage a value between 0 & 255? eg 4V= 200 and 2V= 100

Am I on the right track here or way off?
 

boriz

Senior Member
"What I have is 2 LDRs in series between 0 & 5V rails and I am taking an output from between them ie a voltage divider."

A neat trick, but it has limitations. Firstly, in full daylight, will the combined resistance be low enough to cause out-of-spec currents to flow in the the LDRs? Secondly, in low light, will the parallel resistance be low enough to offer reasonable input impedance to the ADC?

A couple of additional resistors and an op-amp follower would solve both those problems, if indeed they exist.

On the subject of ADC readings - to - voltage, it's been done to death here on the forum and it's all in the manuals anyway. Try a search?
 

boriz

Senior Member
EEP. That was a bit abrupt, sorry.

"Do I assign the voltage a value between 0 & 255? eg 4V= 200 and 2V= 100"

Yep. Pretty much spot on. Assuming you have a stable 5v VCC and are using READADC (20mv per step).

In reality the readings will be slightly different. The steps are not quite 20mv. (5v/256 = 0.01953125)

You'll have three states. Over 200, over 100, under 100. Is that what you want? Maybe you should bring those numbers closer to the middle?
 

GAP

Senior Member
Thanks for the answers i now understand how to convert my voltage readings to adc values.

All I have to do now is stop the flickering when a train crosses the LDR and varying the voltage from affecting my picaxe input.

I was thinking of using a capacitor in parallel with the sensing LDR to smooth out any variations.
 

boriz

Senior Member
Perfectly good experimental solution, but like I said previously, the large range of resistance that an LDR presents could make predicting the RC time constant very tricky. Suck it and see I says.
 

erco

Senior Member
Alternatively, use 2 IO pins. One LDR and series fixed resistor (5k-10k) make a voltage divider to feed each pin. READADC both pins and compare. Avoids extreme situations listed in post #12.

Even faster than READADC (if it matters), you can choose your fixed resistor to yield appropriate voltages to make an input pin read high or low directly. Note that some pins have 2 selectable trigger ranges, TTL and Schmitt.

Re flickering: you don't have to same continuously. Take 3 readings a second apart and average together, or whatever it takes to software filter your train's crossing.
 
Top