Shop OBEX P1 Docs P2 Docs Learn Events
Brightness controlled servo — Parallax Forums

Brightness controlled servo

vipervickvipervick Posts: 6
edited 2012-12-19 15:30 in BASIC Stamp
Hi all,

I am trying to use my BASIC STAMP kit to control window blinds. Sensing light with a photo-transistor, I want to control a servo to open or close window blinds.

I've done the separate activities to move the servo and sense light. But, putting them together is kicking my butt.

Any tips or suggestions would be highly appreciated, thanks!

Comments

  • davejamesdavejames Posts: 4,047
    edited 2012-12-11 21:58
    Hello vipervick - welcome to the Forum.

    Anyone is going to ask you for more information. Could give more detail as to exactly what is doing the butt-kicking? Possibly making your code available for review? Stuff like that.

    Regards
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-12-11 23:45
    Something along the lines of " IF photoresistor >= xxx (or <= xxx)THEN Pulseout Pin, xxx.
  • vipervickvipervick Posts: 6
    edited 2012-12-12 18:51
    Thanks, this is the catalyst I needed.

    I have my Homework Board set up now with the photo-transistors Anode connected to P2 through a 0.1uF capacitor and 220 ohm resistor. And I have the servo control line connected to P13.

    I'm trying to wrap my head around connecting the output of the PT to the servo control line. Will post code when I get home.
  • vipervickvipervick Posts: 6
    edited 2012-12-12 19:39
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    rcPin     PIN 2
    servoPin  PIN 13
    
    time      VAR Word
    
    PAUSE 1000
    
    DO
    
    HIGH rcPin
    PAUSE 100
    RCTIME rcPin, 1, time
    
    IF time > 500 THEN
    PULSOUT servoPin, 1100
    PAUSE 100
    DO
    LOOP UNTIL time = 50
    
    ELSEIF time < 50 THEN
    PULSOUT servoPin, 300
    PAUSE 100
    DO
    LOOP UNTIL time = 500
    ELSE
    PAUSE 100
    ENDIF
    
    LOOP
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-12-12 20:03
    I am not 100% sure but I believe the problem might be within the nested loop. Try commenting out both of your "DO UNTIL" Loops and see what happens. I am also not sure about the PAUSE before the first DO LOOP. However, I am no expert and I am pretty sure someone else will chime in to give you better advise. I am just stating where I would start my trobleshooting from.
  • vipervickvipervick Posts: 6
    edited 2012-12-13 00:39
    Thanks. Hopefully someone can see what I'm doing wrong.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-12-13 08:55
    VERY odd. I just received an email stating @davejames replied to this thread and it shows his reply and solution but the post is not showing up.
  • ercoerco Posts: 20,256
    edited 2012-12-13 09:45
    You may be overthinking it. BTW, this simple job could also be done with a BS1.

    A few questions. Where is your phototransistor, inside the room (behind and affected by the blinds) or just looking out of your window, unaffected by the blinds (I'm assuming this)? Did you want them to open slowly and proportionally to try to maintain a constant light level in the room, or just snap open and shut at a certain outdoor light level (I'm assuming this)? At any rate, you want to avoid oscillation and freakish performance. Google hysteresis and damping.

    Let's look at your code. Your PAUSES are too long, since servos need a constant stream of pulses at ~50 hz. Looks like you're aware that DEBUG commands take too long, although they are useful in testing to determine what your range is for variable "time" (which I changed to "light" below). You just want a small, fast loop to read your photocell (use a small capacitor to minimize read time), do whatever math is necessary to calibrate the value returned to send one calibrated servo pulsout, something like below. Note that it is format checked, but untested.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' 2 positions of blinds, set by value "threshold"
    
    rcPin     PIN 2
    servoPin  PIN 13
    light VAR Word  ' light level rctimed from photocell  lower= brighter   higher=darker
    pulse VAR Word  'servo pulsout value , either 300 or 1100
    threshold CON 500 '  value to change blinds at (adjust as needed)
    pulse=300' initialize
    PAUSE 1000
    
    DO
    HIGH rcPin:PAUSE 1:RCTIME rcPin, 1, light  ' read ptx
    
    IF light>(threshold+100) THEN pulse=300   'adding 100 as a deadband
    IF light<(threshold-100) THEN pulse=1100  'subtracting 100 as a deadband
    ' you may need to swap the 300 and 1100 depending on your servo
    
    PULSOUT servoPin, pulse
    PAUSE 10  '   might need to adjust this to get ~50 hz output pulses, based on your particular RCtime hardware
    LOOP
    
  • davejamesdavejames Posts: 4,047
    edited 2012-12-13 12:13
    NWCCTV wrote: »
    VERY odd. I just received an email stating @davejames replied to this thread and it shows his reply and solution but the post is not showing up.

    That's probably because I slipped in and deleted the post after you received the notifcation copy.

    I sitll haven't had enough coffee to think it through....and the most excellent Erco has responded in great detail.
  • vipervickvipervick Posts: 6
    edited 2012-12-13 20:45
    Thanks all. I appreciate the inputs. I have fixed it myself, even if I'm still a noob!!! It may not be pretty, but it works.
    [COLOR=#008000]' {$STAMP BS2}[/COLOR]
    [COLOR=#008000]' {$PBASIC 2.5}[/COLOR]
    [COLOR=#008000]
    rcPin PIN 2[/COLOR] ' Replaces instances of Pin 2 with rcPin
    [COLOR=#008000]servoPin PIN 13[/COLOR] ' Replaces instances of Pin 13 with servoPin
    
    [COLOR=#008000]time VAR Word[/COLOR] ' Sets the time variable to a max of 65,535
    
    [COLOR=#008000]PAUSE 1000[/COLOR] ' Pause for 1 second
    
    [COLOR=#008000]DO[/COLOR] ' Starts main program
    
    [COLOR=#008000] HIGH rcPin[/COLOR] ' Sets a high at pin 2 to forward bias the
    [COLOR=#008000] PAUSE 100[/COLOR] ' phototranistor. The RC time is calculated
    [COLOR=#008000] RCTIME rcPin,[/COLOR] 1, time ' then displayed in the debug window for a
    [COLOR=#008000] DEBUG HOME, "time = ", DEC5 time[/COLOR] ' visual confirmation
    
    [COLOR=#008000] GOSUB Light_Close[/COLOR] ' Starts the first subroutine
    [COLOR=#008000] GOSUB Dark_Open[/COLOR] ' Starts the second subroutine
    [COLOR=#008000]LOOP[/COLOR] ' Returns to the first line of code
    
    [COLOR=#008000]Light_Close:[/COLOR] ' If the phototranistor is conducting due to
    [COLOR=#008000] IF time < 1000 THEN[/COLOR] ' bright light, then it commands the servo to
    [COLOR=#008000]   PULSOUT servoPin, 1100[/COLOR] ' rotate CCW to 180 degrees
    [COLOR=#008000]   PAUSE 100
      ELSE
        PAUSE 100
      ENDIF
    RETURN[/COLOR] ' Returns to the first line after the first
    ' subroutine
    
    [COLOR=#008000]Dark_Open:[/COLOR] ' If the phototransistor is conducting less
    [COLOR=#008000] IF time > 5000 THEN[/COLOR] ' due to darkness, then it commands the servo
    [COLOR=#008000]   PULSOUT servoPin, 300[/COLOR] ' to rotate CW to 0 degrees
    [COLOR=#008000]   PAUSE 100
      ELSE
        PAUSE 100
      ENDIF
    RETURN[/COLOR] ' Returns to the first line after the second
    ' subroutine. Infinite loop
    
  • davejamesdavejames Posts: 4,047
    edited 2012-12-14 08:39
    Viper - thank you for disclosing the resolve. It's always nice to know what was donet to "make it work".
  • vipervickvipervick Posts: 6
    edited 2012-12-19 13:40
    No problem, I just don't like the formatting trying to copy the code here. Looks aweful. Hopefully this can help someone else down the road.
  • davejamesdavejames Posts: 4,047
    edited 2012-12-19 15:30
    vipervick wrote: »
    No problem, I just don't like the formatting trying to copy the code here. Looks aweful. Hopefully this can help someone else down the road.

    viper - I took the liberty of modifying your previous post with the "code /code" tags.

    Edit the message and you'll see how they are used. It definitely helps maintain the format.
Sign In or Register to comment.