Help ldrs not working?

useless

Senior Member
first of all is it imperitive that you have a 100k resistor in the ldr circuit for it to work?(<please answer) if this is not needed then
why does this code only beep the same pitch every time it checks even though the light changes,
Ive got this on a breadboard and its weird because ive put a speaker in and it doesnt work on pin 4 which i have wired it to and coded it to but when i wire it to the 3rd pin from the top left with the semicircle on the left it works!

main:
output 4
input 1
input 2
wait 1
goto sensor1
goto sensor2
end
sensor1: readadc 1,b1
if b1<50 then snd1 b1 = 200
if b1<100 then snd2 b1 = 200
if b1<145 then snd3 b1 = 200
if b1<175 then snd4 b1 = 200
end
sensor2: readadc 2,b2
if b2<50 then snd1 b2 = 200
if b2<100 then snd2 b2 = 200
if b2<145 then snd3 b2 = 200
if b2<175 then snd4 b2 = 200
end
snd1:
sound 4,(100,50)
wait 1
goto sensor1
goto sensor2
end
snd2:
wait 1
sound 4,(80,50)
goto sensor1
goto sensor2
end
snd3:
wait 1
sound 4,(60,50)
goto sensor1
goto sensor2
end
snd4:
wait 1
sound 4,(40,50)
goto sensor1
goto sensor2
end
 

Dippy

Moderator
Are you using 08?

What circuit are you using for your LDR input? Remember, with ADC inputs, the resistances are important.

Are you getting confused between actual ic pin number 4 and output pin number 4?
If so, you haven't read the manual properly I reckon.

Also, have a look at the programming manual as your IFs are IFFY.

Why do you have two GOTOs in a row in a number of places? Just have a think about what happens when you do that. Try and 'Run' the programme through your head i.e. pretend you are the PICAXE trying to do what you've told it... then a lot of these things will (hopefully) click into place.

And as SO MANY people have said in the past; build your programme code up. Start small then you won't get in a big mess.
 

Michael 2727

Senior Member
May pay you to read each instruction in the
PDF Manual#2 as suggested and see what each one is supposed to do.

<code><pre><font size=2 face='Courier'>main:
output 4 ' probably don't need this as you don't use as an input beforehand anywhare.
input 1 ' or this
input 2 ' or this
wait 1
goto sensor1 ' to use the next line (sensor2) you should have a gosub HERE with a RETURN at the end of the sensor1 routine to bring it back or it will just keep going after the (sensor1 routine)
goto sensor2 ' same applies here
end ' end, Not sure why this is used read Manual_2.PDF
sensor1: readadc 1,b1 ' as answered in the other thread (b1 ?), b1, b2, b3 etc to b12 are places the picaxe use to store a number either allocated by the user (b1= 20) or manipulated by the code (b1 = 1+ b1, in a loop etc)or by gathering a DAC conversion
number from 0 to 255 from a sensor,LDR, NTC etc. Then assining it to a b? for use at a later stage in the code, e.g. readadc 2,b2.
'
if b1&lt;50 then snd1 b1 = 200 ' The b1 = 200 is never written into b1 if the LDR value is
under 175, because once told to then ????
the program goes to where indicated skipping
the rest of the sentence.
If the LDR value happened to be 175 or more the 200 would be written but the code goes to back to a readadc which will change the value again anyway.
You probably need a place the code can go if none of the desired requirements are met e.g. snd5: followed with a goto main to send back to the program start again.
'
if b1&lt;100 then snd2 b1 = 200
if b1&lt;145 then snd3 b1 = 200
if b1&lt;175 then snd4 b1 = 200
end
sensor2: readadc 2,b2
if b2&lt;50 then snd1 b2 = 200
if b2&lt;100 then snd2 b2 = 200
if b2&lt;145 then snd3 b2 = 200
if b2&lt;175 then snd4 b2 = 200
end
snd1:
sound 4,(100,50)
wait 1
goto sensor1
goto sensor2
end
snd2:
wait 1
sound 4,(80,50)
goto sensor1
goto sensor2
end
snd3:
wait 1
sound 4,(60,50)
goto sensor1
goto sensor2
end
snd4:
wait 1
sound 4,(40,50)
goto sensor1
goto sensor2
end
</font></pre></code>
Take it step by step and you will get there.
Try out each new line addition to see how it
reacts before adding new lines.
 

hippy

Technical Support
Staff member
Yes, you'll need some sort of resistor to 'balance' the LDR and create a voltage divider, otherwise
you just have a simple ( albeit variable ) resistor to +V or 0V. What value resistor to use will depend
on what LDR you have and the brightness levels you are expecting. A good starting value is the
same as the LDR's value in ambient light. The manuals may give better advice than I do.

<i>if b1&lt;50 then snd1 b1 = 200 </i>

Not sure what that's meant to do but you seem to have found an undocumented syntax issue. If
it were my compiler I'd have had it give a 'nothing can follow the label' error. Multiple
things after a THEN are always confusing or ambiguous in any programming language. For the PICAXE,
that code compiles to ...

- IF b1&lt;50 THEN snd1
- b1 = 200

Edited by - hippy on 15/07/2006 15:43:06
 

Michael 2727

Senior Member
Not sure exactly what you want to do here ?
From the code that works it looks like you want
to read sensor1 into b1 then make it sound one of the sounds depending on the ADC value.
But then read sensor2 into b2 and make it sound
one of the same 4 sounds depending on its value also.
The only problem here is there is no way of knowing if sensor1 or sensor2 is causing the
sound to be produced.
Can you explain a little better what result you are looking for ?
 

useless

Senior Member
OK first of all, the reason bor the b1/b2 =
200 was put there because I didnot realise
you needed to return for it to do the lines below... therefor i thought that if b1 =
say 20 then it would go to snd1 then snd2
then snd3 etc the 200 was put so it would be greater than all of them, but now I know
its not needed, thanks.

will get rid of the 'end' statements and
put returns on the gotos. about the pin
lineup occurance i am using a breadboard
and I have a diagramme of what pins are
what (using the adc 1 and adc2) and I have
wired them to the correct pins, im sure
unless theres something crazy going on with
the breadboard.

I am trying to make a targety game with the
ldrs with a laser light &quot;home made gun thing&quot; and the aim is to shoot the ldrs and a sound is made, just to get to know the way ldrs work etc this code was just to
test what the laser lights light reading
was around e.g 0-50, 50-100 etc then I would change the code to if the light = a specific light reading
then make a sound else do nothing.

Edited by - useless on 16/07/2006 00:45:44
 

Michael 2727

Senior Member
An easier way would be to use a debug instruction.
You will need the LDR Picaxe pluged into the
download cable to watch the value as it
changes, then get someone to hit the LDR
with the LASER from the distance it would
normally used/required.

Try - <code><pre><font size=2 face='Courier'>
main:
readadc 1, b1
debug b1
pause 2000
goto main </font></pre></code>

You will need to be a fairly good shot to
hit an LDR face at more than a few feet away.
A dark red filter may help block out some of the ambient light but let the LASER light
pass through.
A small solar cell may be a better target at
greater distances, you will need to set up the picaxe input to read voltage instead and
you can also use the red filter.









 

Rickharris

Senior Member
The LDR and the resistor form what is called a potential dividor. That means that they divide the available voltage between them. If the resistor stays fixed and the LDR changes then the voltage you can measure at the join between the resistor and the LDR will change, (this is a practical application of Ohms law).


For what you want you need to know if the LDR resistance has gone low (that will happen when it is illuminated). I guess you want to have several targets and so use the digital inputs for the picaxe rather than the anlogue
inputs because there are more digital inputs available.

I have has some success with takeing the output from an LDR/resistor to a digital input and using a simple programme e.g.- IF PIN3=1 THEN turn

BUT you have to play around with the resistor value to get it to be reliable and really it isn't the right way to do this.

You should use the output from the LDR/Resistor to switch a transistor - ideally a schmitt trigger (that is a small circuit that will switch from off to on to off very sharply). Th output from that part of the circuit (called signal conditioning) will feed the picaxe digital input.

You can find out about these things on the web in many places. <A href='http://www.diseno-art.com/encyclopedia/strange_vehicles/strange_vehicles.html' Target=_Blank>External Web Link</a>

is usefull to help understand the potntial divider idea.

Your idea can be done. I would suggest when you get the basics sorted out you start to think about how you can make the laser pulse a short burst or people will scan around until they hit the target.

Start simple and build up - it's good advice - progress can be quick.

 

Michael 2727

Senior Member
A voltage divider works the same for any voltage. (Just the ratio changes)
The Output is a direct ratio between the two resistors
and the total supply Voltage used.
<code><pre><font size=2 face='Courier'> Voltage Divider
5V + o Using a 5V Supply
|
-
\ If both resistors are equal you get 2.5V out,
/ Between Neg and Out. (Half the VOLTAGE)
-\-&gt; LDR If the LDR is 5K you get 3.33V out,
/ If the LDR is 1K you get 4.54V out,
| Out If the LDR is 500 R you get 4.76V out,
o------------o
| If the LDR is 20K you get 1.66V out,
.-. If the LDR is 50K you get 0.83V out,
| | If the LDR is 75K you get 0.58V out,
| | 10K If the LDR is 100K you get 0.45V out,
'-'
|
|
Neg o All Voltages taken between Neg and Out.
'
</font></pre></code>
 
Top