Shop OBEX P1 Docs P2 Docs Learn Events
Stuttering led — Parallax Forums

Stuttering led

YianieYianie Posts: 49
edited 2014-10-15 09:19 in General Discussion
I wrote a simple program to learn about the power of the RCTIME command. The problem is that the led I have connected stutters (HIGH 3). I was told from a friend familiar with STAMP2 that the program is running in a loop, but I don't get it. Why is the led stuttering when its on?

' {$STAMP BS2}
' {$PBASIC 2.5}


result VAR Word


HIGH 9
PAUSE 1
RCTIME 9, 1, result
DEBUG DEC ? result
IF result>1100 THEN
HIGH 3
ENDIF

Comments

  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2014-10-15 07:33
    It's been years since I've used the stamp, but from what I remember, the RCTIME command doesn't cause any significant pause in the program. This means it pauses 1ms, then a couple more milliseconds (depending on the capacitor size), than flashes the LED as high, and loops. If you want a more stable LED, I'd suggest you add a 250-500ms pause after the HIGH 3 command.
  • YianieYianie Posts: 49
    edited 2014-10-15 07:46
    It's been years since I've used the stamp, but from what I remember, the RCTIME command doesn't cause any significant pause in the program. This means it pauses 1ms, then a couple more milliseconds (depending on the capacitor size), than flashes the LED as high, and loops. If you want a more stable LED, I'd suggest you add a 250-500ms pause after the HIGH 3 command.

    Why does it loop?
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2014-10-15 07:54
    I'm guessing because you left your program open-ended (as I said, it's been a while since I've used this...), but if you put an END command after the ENDIF the BS2 will go into low power mode and stop running instructions.
    E.g.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    result VAR Word
    
    
    HIGH 9
    PAUSE 1
    RCTIME 9, 1, result
    DEBUG DEC ? result
    IF result>1100 THEN
    HIGH 3
    ENDIF
    END
    
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-10-15 08:33
    "Quick and Dirty" programs are NEVER QUICK and ALWAYS DIRTY. I'd encourage you to write your test programs as you would a professional application that you were creating for a client.

    Start by using named constants for pins and writing code in modules to the degree you can; this takes very little extra time and helps prevent errors.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    PotPin    CON  9
    LedPin    CON  3
    
    result    VAR   Word
    
    
    Main:
      GOSUB Read_Pot
      IF (result > 1100) THEN
        HIGH LedPin
      ELSE
        LOW LedPin
      ENDIF
    
      GOTO Main
    
    
    Read_Pot:
      HIGH PotPin
      PAUSE 1
      RCTIME PotPin, 1, result
      RETURN
    


    Why is the led stuttering when its on?


    Because you terminated the program with END (you don't even need to use the keyword for that to happen). When the BS2 hits END the internal watchdog timer stops getting reset; when it times out this will cause an outputs to be disabled for a moment. You'll note this is happening at a specific rate -- you're watching the watchdog repeatedly time out.

    This is from END in the online manual:

    During execution of power conserving commands (END, NAP, POLLWAIT, and SLEEP), current will occasionally be interrupted on I/O pins for about 18 ms durations (60 µs on the BS2pe). The reason is that the watchdog-timer reset that awakens the BASIC Stamp during these commands also causes all of the pins to switch to input mode for approximately 18 ms (60 µs on the BS2pe). When the interpreter firmware regains control of the processor, it restores the I/O direction dictated by your program.
  • ercoerco Posts: 20,256
    edited 2014-10-15 08:34
    It shouldn't be looping. Depending on your battery and external circuit, your Stamp may be constantly resetting.

    Also, after a program ends, it's normal that your LED winks once every 2.3 seconds.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-10-15 09:19
    Use STOP after the ENDIF, and that will keep the Stamp alive and the LED won't stutter. STOP is like END, but does not put the Stamp in the low-power sleep state.

    Another way to reduce the stutter electrically is to put a capacitor in parallel with the R+LED circuit at the Stamp output pin.
Sign In or Register to comment.