Shop OBEX P1 Docs P2 Docs Learn Events
Photoresistor controlled light switch? — Parallax Forums

Photoresistor controlled light switch?

FemiFemi Posts: 3
edited 2012-07-08 22:00 in BASIC Stamp
I'm trying to design a product that will flip on a light switch when the ambient light level drops below a certain threshold. I'm using a BASIC Stamp, but I can't seem to get the code right. Can you guys point me in the right direction? Thanks!

Here's my code so far:

' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Word
time VAR Word
DEBUG "Counterclockwise 10 o'clock", CR
DO
HIGH 2
PAUSE 100
RCTIME 2, 1, time
DEBUG HOME, "time = ", DEC5 time
LOOP
IF time > 50 THEN
FOR counter = 1 TO 75
PULSOUT 14, 1100
PAUSE 20
NEXT
DEBUG "Clockwise 2 o'clock", CR
FOR counter = 1 TO 150
PULSOUT 14, 450
PAUSE 20
NEXT

DEBUG "Center 12 o'clock", CR
FOR counter = 1 TO 150
PULSOUT 14, 2000
PAUSE 20
NEXT
DEBUG "All done."
ENDIF
END

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-01-21 07:35
    Two things, your DO - LOOP will never exit and go on to your other code and if the lights turn on when the light level gets to a low level what happens when the lights go on?
  • ercoerco Posts: 20,256
    edited 2011-01-21 08:33
    Using a servo, huh? Nice effort. Franklin's right, you're probably headed for oscillation trouble that plagues too-simple control circuits.

    Yes, you can make a simple switch that will turn a lamp on when the room gets too dark. But once the lamp is on, the room is bright again, and that same circuit will turn the lamp off in short order. So the lamp will go on & off rapidly. You can buy outdoor lights and night lights with this photocell feature, and they usually oscillate (flash rapidly or dim themselves) at some almost-dark light level, even though they have these luxuries:

    1) low light output that doesn't flood the light with room, and
    2) they can aim the photocell away from the light output to minimize self-triggering effects

    Your options include hysteresis (Google it), which is turning off at a brighter level than you turn on (experimentation required), or once on, set a timer to run the lamp for an hour or so, then switch it off, then check the ambient light level, and switch on if necessary. Loop that!
  • TomatoTomato Posts: 23
    edited 2011-01-21 20:01
    Hello Femi,

    You do not need a microcontroller for this. An extremely simple comparator circuit can be made to close relay contacts. If you wish to have the light switched mechanically, the comparator can activate an h-bridge connection via two series NOT gates. iF you use dedicated ICs (like monolithic inverters, monolithic h-bridge, and monolithic comparator) you can fit all this circuitry on a small board with pretty few external components (pwr supply, some resistors, some decoupling caps, and MAYBE a buffer transistor or two). The rotation of the motor can be controlled by a monostable pulse.

    Hope it helps!
    Tomato
  • bsnutbsnut Posts: 521
    edited 2011-01-21 21:03
    Femi,

    Franklin, Erco and Tomato have very good points on the way to control your lighting project and this is the why I would do it.

    I would use a Basic Stamp 1 to do this with solid state relay or you can use cool lighting control board called FC-4 from EFX-TEK.com. This board is able to control the dimming and turn on/off your lights serialy and this will be the best bet for you.
  • FemiFemi Posts: 3
    edited 2011-01-23 13:24
    Thanks for all the replies! I agree that don't need a microcontroller for this, but this is actually a school project and the teacher is requiring the use on a BASIC Stamp as well as a servo.
    TO Franklin: why won't my do loop exit? Sorry if that's a silly question, but I'm very new to coding.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-23 13:43
    Your DO / LOOP statement won't exit because you haven't told it to exit. If you don't supply an exit condition on either the DO or LOOP part of the statement and you don't have some other kind of statement between the DO and the LOOP that'll do a GOTO out of the loop, it won't exit. Read the description of DO / LOOP again in the Stamp Basic Manual or the Stamp Editor's help files.
  • FemiFemi Posts: 3
    edited 2011-01-24 05:44
    I added an exit condition (LOOP UNTIL) and my code works perfectly now. Thanks guys! Can't believe I made such a simple mistake.
  • unoxunox Posts: 4
    edited 2012-07-08 19:03
    Hi everyone!

    i'm also doing the similar project which is to use photoresistor to detect the room brightness & adjust the brightness of the lamp.
    which is to say something like environmental friendly lighting system that will dim the lamp when the room has sufficient brightness.

    i really need help with that & hope to see more ideas soon!
    thanks alot!
  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-08 22:00
    Welcome to the forums. Do keep in mind that this is not a passive place where you sit around and people feed you answers although sometimes it might feel like that. The best way to use these forums is to provide as much information as you can, particularly your ideas, questions, etc. Show any code or pseudo-code that you might have that might show both what you want to do and what you know, particularly the latter. You'll get the most and best quality help when you ask specific questions with examples.
Sign In or Register to comment.