Shop OBEX P1 Docs P2 Docs Learn Events
Switch and one Led — Parallax Forums

Switch and one Led

Jimm2Jimm2 Posts: 16
edited 2010-01-18 20:21 in Propeller 1
What I would like to know is, Is there A way to use the Propeller Chip, so that when you hit the switch (on and off) at A slow pace. It would make the Led, stay on. But when you hit the switch, hold it down it would make the led stay off. When you didn't hit the switch, the led would be off.



PUB

dira[noparse][[/noparse]6] := 1
dira[noparse][[/noparse]21] := 0
repeat
outa[noparse][[/noparse]6] := ina[noparse][[/noparse]21] ((in this line could I put something that would make it read like this)) The ina [noparse][[/noparse]21] == 10101





Switch (101010101010101010)
Led (111111111111111111111)


Switch (000000000000000000)
Led (000000000000000000000)


0 = off
1 = on

Comments

  • CassLanCassLan Posts: 586
    edited 2010-01-15 00:45
    Welcome Jimm2!

    I think what your asking about is totally doable, to know what the end goal/purpose was might lead to a different solution, but from what you've said here is how I would approach it:

    What your saying is that if the status of the switch changes in any way over a certain period of time that would trigger the LED. And that any status of the switch which would occur beyond your pre-defined time period would then leave the LED off.

    So I would make a loop that detects a change in the switches state, and when I see that its changed, I would grab a copy of the cnt (The system counter), the next time my loop caught another change in the state of the switch, it would compare the new cnt value with the one I previously recorded, and if they are close enough (whatever your needs are) , that would trigger the LED.
    That would cover lighting the LED, to see if it should be off, you need to compare the current cnt with each iteration of your loop to the most recent switch-state-change captured cnt value...and again depending on your timing needs, turn off the LED.

    Welcome to Prop world

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • rjo_rjo_ Posts: 1,825
    edited 2010-01-15 01:38
    Just to extend Rick's answer a smidge:

    There is an example that is very close to this in one of the learning labs. The example lights up an LED for as long as you push a button. If you haven't looked through the labs... they are the shortest path, constructed by true professionals with all kinds of feedback from the forum... a gold mine of facts and concepts that you wouldn't otherwise come across until much later.

    If you want to wait a half a second... that count (cnt) interval is represented by clkfreq/2, a tenth of a second is clkfreq/10, etc. So, you set up your interval with a variable(called interval), whose value is assigned to clkfreq/something... then you subtract your cnt values and compare the result to "interval."
  • CassLanCassLan Posts: 586
    edited 2010-01-18 20:21
    Jimm2,

    Here are 4 examples, they build on each other, the last one is what your looking for I think:
    LED Based on Switch:
    CON
         _clkmode = xtal1 + pll16x
         _xinfreq = 5_000_000
    
    LEDPin = 0
    SwitchPin = 3
    Var
    long    LastSwitchChange
    long    RateThreshold
    byte    SwitchStatus
    
    Pub start
    Dira[noparse][[/noparse]LEDPin]~~
    Dira[noparse][[/noparse]SwitchPin]~
    repeat
      SwitchStatus := ina[noparse][[/noparse]SwitchPin]
      if SwitchStatus == 1
        outa[noparse][[/noparse]LEDPin]~~
      else
        outa[noparse][[/noparse]LEDPin]~
    

    LED Based on Timing:
    CON
         _clkmode = xtal1 + pll16x
         _xinfreq = 5_000_000
    
    LEDPin = 0
    SwitchPin = 3
    Var
    long    LastSwitchChange
    long    RateThreshold
    long    LastCNTValue
    byte    SwitchStatus
    
    Pub start
    Dira[noparse][[/noparse]LEDPin]~~
    Dira[noparse][[/noparse]SwitchPin]~
    RateThreshold := 100_000_000
    LastCNTValue := cnt
    repeat
      
      if cnt > LastCNTValue + RateThreshold
        !outa[noparse][[/noparse]LEDPin]
        LastCNTValue := cnt
    

    LED Based on Switch Timing (how fast you press the switch repeatedly):
    CON
         _clkmode = xtal1 + pll16x
         _xinfreq = 5_000_000
    
    LEDPin = 0
    SwitchPin = 3
    Var
    long    LastSwitchChange
    long    RateThreshold
    long    LastCNTValue
    long    ResultCNT
    byte    SwitchStatus
    
    Pub start
    Dira[noparse][[/noparse]LEDPin]~~
    Dira[noparse][[/noparse]SwitchPin]~
    RateThreshold := 100_000_000
    LastCNTValue := cnt
    repeat
      ResultCNT := cnt - LastCNTValue  
      if Ina[noparse][[/noparse]SwitchPin] <> SwitchStatus       'is the state of the switch different from the last time it changed?
        LastCNTValue := cnt                   'lets record this time
        SwitchStatus := Ina[noparse][[/noparse]SwitchPin]        'store this as the current switch status
        
      if ResultCNT > RateThreshold
        outa[noparse][[/noparse]LEDPin]~
      else
        outa[noparse][[/noparse]LEDPin]~~
    
    

    2 LEDs Based on Switch Timing with Different Actions:
    CON
         _clkmode = xtal1 + pll16x
         _xinfreq = 5_000_000
    
    LEDPin1 = 0
    LEDPin2 = 1
    SwitchPin = 3
    Var
    long    LastSwitchChange
    long    RateThreshold
    long    LastCNTValue
    long    ResultCNT
    byte    SwitchStatus
    
    Pub start
    Dira[noparse][[/noparse]LEDPin1]~~
    Dira[noparse][[/noparse]LEDPin2]~~
    Dira[noparse][[/noparse]SwitchPin]~
    RateThreshold := 100_000_000
    LastCNTValue := cnt
    repeat
      ResultCNT := cnt - LastCNTValue  
      if Ina[noparse][[/noparse]SwitchPin] <> SwitchStatus       'is the state of the switch different from the last time it changed?
        LastCNTValue := cnt                   'lets record this time
        SwitchStatus := Ina[noparse][[/noparse]SwitchPin]        'store this as the current switch status
        
      if ResultCNT > RateThreshold            'we have gone over our threshold with no change in the SwitchStatus
                                              'this means either the switch was held down or not pressed
        outa[noparse][[/noparse]LEDPin1]~
        If SwitchStatus == 1        'Switch was held
          outa[noparse][[/noparse]LEDPin2]~~
        else                        'switch was not pressed
          outa[noparse][[/noparse]LEDPin2]~
      else                                    'the switch is changing faster than the threshold rate
        outa[noparse][[/noparse]LEDPin1]~~
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)
Sign In or Register to comment.