Shop OBEX P1 Docs P2 Docs Learn Events
LED Light detector — Parallax Forums

LED Light detector

jgjonolajgjonola Posts: 27
edited 2009-07-02 20:19 in General Discussion
Hi everyone, I am just getting back in to playing with SX/B (its been a few years.) I was playing around with making an LED light detector. So i have an led hooked between RB.1 and RB.2 with a resistor in series. Here is the code:

'PWM.SXB
DEVICE         SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ         4_000_000

led2 PIN RB.2
led1 PIN RB.1
led7 PIN RB.7
light VAR Word
time CON 2000


'Code

PROGRAM Start
Start:

led1 = 1
led2 = 0
DO
  OUTPUT led2
  OUTPUT led1

  RCTIME led1, 1, light

  IF light = 0  THEN 
    led7 = 1 
  ELSE 
    led7 = 0
  ENDIF

PAUSE time
LOOP





I have another LED connected to RB.7 that is supposed to turn on if the value of light is 0. Not seeming to work. I copied this from similar code i found from the basic stamp and it worked fine with my stamp. I just made a few changes for the SX and no go...

Here is the code i used as a base for the Stamp:

'{$PBASIC 2.5}
'file:LED_Emitter_Detecter.bs2
'Clark Radcliffe
'Michigan State University
'July 30, 2007
'Requires an LED in series with a 220 Ohm resister
'between pin 1 and the LED connected to pin 2
'Declarations
Pin2 PIN 2
Pin1 PIN 1
light VAR Word
time CON 2000
'Code
DEBUG "Start test...", CR
DO
DEBUG "LED is a detecter (LED off and charging)", CR
Pin1 = 1
Pin2 = 0
PAUSE 10
RCTIME pin1, 1, light
DEBUG "Light measured = ", DEC light, CR
PAUSE time
LOOP




Thanks!
John G

Comments

  • BenoitBenoit Posts: 47
    edited 2009-07-02 01:17
    John,

    I don't see in your code where RB.7 is set as an output. It could be that the pin is set as an input.

    -Benoit
  • ZootZoot Posts: 2,227
    edited 2009-07-02 01:19
    Your code examples are not exactly the same; in the SX version you are presuming you will get an RCTIME of 0 -- seems unlikely as there will probably always be *some* capacitance.

    Your Stamp code debugs out the value; for the SX, use WATCH and then run the program in DEBUG mode with your Key connected. Then try it out and see what values you get for various light intensities -- once you have a decent threshold number established, then you can have an LED turn on and off based on that threshold.

    Then, shouldn't the loop turn the led back on (and maybe pause) to charge up the capacitance again?

    'PWM.SXB
    DEVICE         SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ         4_000_000
    
    led2 PIN RB.2 OUTPUT
    led1 PIN RB.1 OUTPUT
    led7 PIN RB.7 OUTPUT
    light VAR Word
    time CON 2000
    
    someThreshold CON 100 ' or whatever a good number ends up being
    
    WATCH light
    
    'Code
    
    PROGRAM Start
    Start:
    
    
    DO
      'OUTPUT led2
      'OUTPUT led1 
    
      'led1 = 1
      'led2 = 0
    
      HIGH led1 ' makes high and output
      LOW led2
    
      PAUSE time
    
      RCTIME led1, 1, light  ' also automatically makes led1 an input
    
      IF light < SomeThreshold  THEN 
        led7 = 1 
      ELSE 
        led7 = 0
      ENDIF
    
    LOOP
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 7/2/2009 1:26:56 AM GMT
  • jgjonolajgjonola Posts: 27
    edited 2009-07-02 20:19
    Zoot,
    That was it. Thanks! It works perfectly now.

    John Gjonola
Sign In or Register to comment.