Shop OBEX P1 Docs P2 Docs Learn Events
Latching Remote LEDS — Parallax Forums

Latching Remote LEDS

IRobot2IRobot2 Posts: 164
edited 2011-01-05 13:45 in Propeller 1
Here is what I am working with:
I have 16 momentary pushbuttons connected to a prop (pins 1-16). This prop sends serial data to remote prop that has 16 corresponding LEDS connected to it (pins 1-16).

Here is what I want it to do:
Every time a button is pressed it will hold in the corresponding LED for 5 seconds.

Here is the problem I am having:
Since I do not have 16 cogs, I cannot think of the correct *software* way to latch the LED’s on in such a way that pushing buttons 1,2,3,4 will turn on LED’s 1,2,3,4 at the same time. (Using wait statements to keep an LED on of coarse keeps the other LED’s from lighting at once. )

The catch:
If button 1 is hit 3 times within the 5 second holding period, it still needs to go off in 5 seconds. (Not continue to keep adding 5 seconds).

Can anyone point me in the right direction? Can this even be done?
Thanks for all of your help! -Alex

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-05 12:54
    Then don't use a waitcnt - and use one COG.

    This one COG simply needs an endless loop that's doing the following:

    1. Check if we received a new pushbutton message throm the other prop
    2. If so, check whether that button has already been pushed
    3. If not store the value of the counter in an array and switch the corresponding LED on

    4. Loop over all elements of the array
    5. If the current LED is on, check when it has been switched on ( subtract counter vale stored in the array from the actual cnt )
    6. If the difference is 5*clockfreq or bigger then switch off the LED

    Hope that's enough to point you in the right direction. If not continue asking.
  • IRobot2IRobot2 Posts: 164
    edited 2011-01-05 13:15
    That helps me quite a bit. Storing the counter data in an array was the missing link. Thanks for your help! -Alex
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-05 13:17
    (MagIO2 and I seem to be doing a lot of tag-team assistance this week...)

    I agree with Mag and here's a little method that lets you pass the start and end points of your LED group (they must be contiguous), as well as the address of an array that holds the timer for each LED.
    pub ledcontrol(msb, lsb, pntr) | t, ch                          ' launch with cognew
    
    '' control LED outputs 
    
      outa[msb..lsb] := IS_OFF                                      ' all off
      dira[msb..lsb] := IS_ON                                       ' all outputs
    
      t := cnt
      repeat
        repeat ch from 0 to (msb - lsb)                             ' loop through channels
          if (long[pntr][ch] > 0)                                   ' if running
            outa[lsb+ch] := IS_ON                                   ' LED is on
            long[pntr][ch]--                                        ' update timer
          else
            outa[lsb+ch] := IS_OFF
        waitcnt(t += MS_001)                                        ' scan every millisecond
    

    The attached program runs on a demo board. When you press a key (I'm using PST) it will light the LED for 5 seconds. Note that if the LED is already lit then its timer will be reset to 5 seconds (this logic is easy to change).
  • IRobot2IRobot2 Posts: 164
    edited 2011-01-05 13:45
    Jon, Thanks for the well documented code! This helps me out tremendously. I also liked how you used the PST for inputs. Though I am sure it is common knowledge to every one else, I had never done that. Its funny how learning one trick can really make your life easier. Thanks again. -Alex
Sign In or Register to comment.