Shop OBEX P1 Docs P2 Docs Learn Events
LDR Programming Help! — Parallax Forums

LDR Programming Help!

jmathews8jmathews8 Posts: 2
edited 2012-04-10 18:07 in Accessories
I've been working on a project that has been going well until I got the programming portion of it. I'm making a set of blinds that open when light strikes the LDR and then closes when a decreased amount hits it. The only problem is that I don't know how to get started with that. The programming knowledge of our class doesn't encompass this much stuff. How do I get the LDR to tell the Servo motor to begin turning the blinds open?

Comments

  • prof_brainoprof_braino Posts: 4,313
    edited 2012-04-06 10:18
    My advice is that you need to address the PLANNING before you think about coding.

    This is two separate problems:
    1) reading the sensor.
    2) controlling the actuator.

    We need to know something about the LDR (such as a link to its datasheet). Then you can start to ask about getting reading from it.
    Then we need to know a little something about the servo, such as is it a stock hobby servo, and which one.

    Once you determine how to accomplish steps 1 & 2, then step three, writing code to that brings these together as your application, is a piece of cake.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2012-04-06 11:22
    Chapter 7, Measuring Light, of What's a Microcontroller http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf may help. It also covers controlling hobby servos.

    John Abshier
  • jmathews8jmathews8 Posts: 2
    edited 2012-04-10 12:44
    Alright, here's my code so far. The only problem is that whenever the LDR stays in the darkness, it keeps turning counter-clockwise.

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    LdrValue VAR Byte
    PIN6 PIN 6 'ldrsensor
    pin12 PIN 12 'servo
    pin15 PIN 15 'led
    Baud CON 396 '9600 Baud
    counter VAR Word
    OUTPUT pin12
    LOW PIN12
    'the lightness has levels from 0 TO 255 AND I choose 150 AND beyond TO turn LED ON
    LdrValue = 0
    DO
    here: SERIN pin6, Baud, [LdrValue]
    'DEBUG CLS, DEC LdrValue, CR
    IF LdrValue >= 250 THEN
    FOR counter =1 TO 100
    PULSOUT pin12, 2125
    PAUSE 20
    NEXT
    FOR counter = 1 TO 100
    PULSOUT pin12, 1875
    HIGH 15
    PAUSE 20
    NEXT
    ENDIF
    IF LdrValue <= 250 THEN
    FOR counter =1 TO 100
    PULSOUT pin12, 1625
    LOW 15
    PAUSE 20
    NEXT
    FOR counter =1 TO 100
    PULSOUT pin12, 1875
    NEXT
    ENDIF
    LOOP

    Any assistance would be greatly appreciated.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-10 16:59
    You need to follow the advice you've been given. First you write a program that uses the DEBUG statement to tell you what the LDR values are and verify that the program (and the LDR) behave as expected. Then you modify that program to use the DEBUG statement instead of a servo to tell you what the servo would do depending on the light level. You write another program that just moves the servo to one position based on a value from a DEBUGIN statement that tells it whether to open or close. Once you've gotten that far, you should be able to combine the two programs for your finished project.

    You might also use the [ code ] and [ /code ] tags along with indenting to show the structure of your program, namely which statements are under control of which IF / THEN and FOR / NEXT statements. Comments also can help describe a program's functioning to another person and often help you in your own understanding of the program and its errors.

    attachment.php?attachmentid=78421&d=1297987572
  • kwinnkwinn Posts: 8,697
    edited 2012-04-10 18:07
    You also need a way to sense when the blinds are fully opened and fully closed so you can stop the motor at that point. This is usually done with one or two microswitches or optical sensors.
Sign In or Register to comment.