Shop OBEX P1 Docs P2 Docs Learn Events
Red LED continuously blinking — Parallax Forums

Red LED continuously blinking

I've searched the forums and help menu for any coding that may help, but I still have no idea how to execute this exercise! My pseudocode is as follows:
1. Red LED on-off once every second
2. Button 1 flash yellow at 20HZ
3. button 2 flash green at 40HZ
4. Buttons 1 & 2 flash both alternately at 10HZ
5. Red LED is always flashing at once a second

I've made the buttons flash the corresponding LEDs (not too worried about the correct HZ yet), but obviously when they break into the if statement the red LED is off. I've thought of adding the red LED high/low command in the IF statements but it would cause the yellow/green LEDs to pause for half a second! I've also tried making the red LED its own loop, but since the loop has to be continuous, the program will only make the red LED blink and ignore the rest of the coding. So far, here's my code:

' {$STAMP BS2}
' {$PBASIC 2.5}
x VAR Byte
y VAR Byte
z VAR Byte

DO
x = 0
y = 0
z = 0
HIGH 13
PAUSE 500
LOW 13
PAUSE 500

IF (IN0 = 1 AND IN1 = 1) THEN
DO
HIGH 15
LOW 14
PAUSE 50
LOW 15
HIGH 14
PAUSE 50
LOW 14
z = z + 1
LOOP UNTIL (z = 10)
ENDIF
IF (IN0 = 1) THEN
DO
HIGH 14
PAUSE 50
LOW 14
PAUSE 50
x = x + 1
LOOP UNTIL (x = 10)
ENDIF
IF (IN1 = 1) THEN
DO
HIGH 15
PAUSE 20
LOW 15
PAUSE 20
y = y + 1
LOOP UNTIL (y = 10)
ENDIF
LOOP

I've thought of GOTO, RETURN, SELECT/CASE.. nothing would work correctly. The only thing I can think of is embedding the red LED high/low code with the IF/THEN statements and just trying to time it out to correspond correctly. Any help would be appreciated though!

Comments

  • Your on the right path with your last statement. You have to find a way to keep track of the red LED timing while you are doing the other ones. Hint: it involves counting.
  • JonnyMacJonnyMac Posts: 9,157
    edited 2015-10-04 15:22
    Attempting to multi-task in a BASIC Stamp is tricky business. As Sapphire pointed out, it involves counting, specifically loop counting. You need to setup a program loop that run at what's required for your fastest blink rate. Slow rates are divided down by counting.

    Here's something to try. It may be advanced beyond your present programming skills, but will be worth studying to help you improve (FTR, this program compiles, but I don't have time to hook it up for testing).

    Note that I avoid the use of "magic numbers" in the code. You would do yourself a favor by defining your IO points (as you did in your description above) as it makes programs far easier to read and debug. Don't get by the "quick and dirty" gag; it's never quick, it's always dirty.
    ' =========================================================================
    '
    '   File......
    '   Purpose...
    '   Author....
    '   E-mail....
    '   Started...
    '   Updated...
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    
    ' -----[ Program Description ]---------------------------------------------
    
    
    ' -----[ Revision History ]------------------------------------------------
    
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    RedLed          PIN     13                      ' 1 Hz
    YellowLed       PIN     14                      ' 20 Hz w/ Btn1
    GreenLed        PIN     15                      ' 40 Hz w/ Btn2
    
    Btn2            PIN     1
    Btn1            PIN     0
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    IsOn            CON     1                       ' for active-high in/out
    IsOff           CON     0
    
    Yes             CON     1
    No              CON     0
    
    
    ' -----[ Variables ]-------------------------------------------------------
    
    rTimer          VAR     Byte
    ygTimer         VAR     Byte
    
    state           VAR     Byte
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    Reset:
      OUTH = %00000000 : OUTL = %00000000           ' clear all
      DIRH = %11100000 : DIRL = %00000000           ' set outputs
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
      PAUSE 12                                      ' set base timing
    
    
    Process_Red:
      rTimer = (rTimer + 1) // 84                   ' keep 0..83
      IF (rTimer < 42) THEN
        RedLed = IsOn
      ELSE
        RedLed = IsOff
      ENDIF
    
    
      ygTimer = ygTimer + 1
    
      state = INL & %11
      BRANCH state, [No_Leds, Flash_Yellow, Flash_Green, Alternate]
    
    
    No_Leds:
      YellowLed = IsOff
      GreenLed = IsOff
      ygTimer = 0
      GOTO Main
    
    
    Flash_Yellow:
      YellowLed = ygTimer.BIT1
      GreenLed = IsOff
      GOTO Main
    
    
    Flash_Green:
      GreenLed = ygTimer.BIT0
      YellowLed = IsOff
      GOTO Main
    
    
    Alternate:
      ygTimer = ygTimer & %111                      ' keep 0..7
      YellowLed = ygTimer.BIT2
      GreenLed = 1 - YellowLed
      GOTO Main
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-10-04 20:50
    Note that you can associate the state of an output with the current state of a bit in a counter byte. For example,
    out15 = x.bit2 ' current state of x.bit2
    out14 = ~x.bit2 ' complement of x.bit2
    Does that help? The quick and dirty summary.
  • Thanks guys! I understand how it has to execute a little more clearly than last night.. I'm going to try different approaches so if I come across this situation again, i'll have different options. Johnny, your coding reminds me of python coding I did a few year ago, where you make classes/functions and call them in your main. I'm going to try writing programs that return or goto after executing!
  • JohnnyJon, your coding reminds me of python coding I did a few year ago, where you make classes/functions and call them in your main. I'm going to try writing programs that return or goto after executing!

    Regardless of language, breaking a program into discrete pieces that are functional makes good sense. An inexperienced programmer would have a very difficult time with your original listing. It only takes a little extra time to format neatly, and I find it saves hours in debugging time.
Sign In or Register to comment.