Shop OBEX P1 Docs P2 Docs Learn Events
How do I take one program, Add to the other program.... — Parallax Forums

How do I take one program, Add to the other program....

Jimm2Jimm2 Posts: 16
edited 2010-01-29 02:10 in Propeller 1
This is the Program. What I need help with is. If I hold down switch (23). It will make A bank of Led's, stay on for the time the switch is held down. If that same switch, is hit off and on and off and on. (Turn) It will make A bank of Led's switch on and off. So what I don't know how to do is. Make two program into one...........







CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


LEDPin1 = 0
LEDPin2 = 1
SwitchPin = 23

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]~~







My program to add to the other one up here................................................................................

PUB (((((Down)))))))

dira[noparse][[/noparse]4..9] := %111111




outa[noparse][[/noparse]4..9] := %111111
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %000000


PUB (((((Turn)))))))

dira[noparse][[/noparse]4..9] := %111111



outa[noparse][[/noparse]4..9] := %111111
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %011111
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %001111
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %000111
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %000011
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %000001
waitcnt(clkfreq/4 + cnt)
outa[noparse][[/noparse]4..9] := %000000





Thank you much.

Comments

  • VIRANDVIRAND Posts: 656
    edited 2010-01-28 01:26
    I'm not sure which way you want to do it...
    Either open both programs and use cut and paste to combine them,
    OR ALSO start a new Cog.

    Someone else would have to remind us what the command to do that is,
    because although it is easy, I don't remember how to start a new cog with for a SPIN program,
    because I usually run PASM programs in the other Cogs.

    Post Edited (VIRAND) : 1/28/2010 1:31:52 AM GMT
  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-01-28 03:32
    Welcome to the forum,

    First, when you post your code, you need to copy it from your editor and then paste it into your message to the forum by starting it with [noparse][[/noparse]∑CODE∑ ] and ending it with [noparse][[/noparse]∑/CODE∑] but remove the ∑'s so the browser doesn't freak out.

    That way your code will be
       properly formatted
         like this
    
    
    



    As for solving your specific problem, you can totally re-write the program into one, of course. Orrrr....

    I suppose you could make a Method out of your second program and have the first program (Main) call up that Method. Look up, for example, page 67 of this:

    www.parallax.com/Portals/0/Downloads/docs/prod/prop/PELabsFunBook-v1.1.pdf

    And of course there is the manual, in which you can investigate all the glories of COGNEW, etc, if you must:

    www.parallax.com/Portals/0/Downloads/docs/prod/prop/WebPM-v1.1.pdf


    hope that helps,
    smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Watching the world pass me by, one photon at a time.
  • Jimm2Jimm2 Posts: 16
    edited 2010-01-28 03:47
    CON
         _clkmode = xtal1 + pll16x
         _xinfreq = 5_000_000
    
    
    LEDPin1 = 0
    LEDPin2 = 1
    SwitchPin = 23
    
    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]~~
    
    
    
    
    
    '---------------------------------------------------------------------------------------------------------------------------------
    'My program to add to the other one up here................................................................................
    '-------------------------------------------------------------------------------------------------------------------------------------------------
    
    PUB (((((Down)))))))
    
    dira[noparse][[/noparse]4..9] := %111111
    
    
    
    
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000
    
    
    PUB (((((Turn)))))))
    
    dira[noparse][[/noparse]4..9] := %111111
    
    
    
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %011111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %001111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000011
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000001
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000
    
    
    
  • eiplannereiplanner Posts: 112
    edited 2010-01-28 05:19
    This should do it for you, it worked for me.

    PUB Turn
    
    dira[noparse][[/noparse]4..9] := %111111
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %011111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %001111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000011
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000001
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000
    
    
    PUB Down
    
    dira[noparse][[/noparse]4..9] := %111111
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000
    
    
    {In your Propeller Tool, click on 'File' then click on 'New'.  Copy and paste this into your new page
    by giving it a name such as LEDBlink.  Save this file in the Parallax folder.
    
    Make an Object block above your VAR block by typing this:   OBJ   led: "LEDBlink"
    
    Then, every time you want to use one of these blinking methods, just put in a call to
    them by typing:   led.Down          or   led.Turn}
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    It's not the size of the dog in the fight, but the size of the fight in the dog.
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-01-28 15:21
    I'm not sure if I understood every detail right.

    If the button is pressed down LEDs on
    If button is not pressed down LEDs off

    so this could be reduced to the following code

    CON
       _clkmode = xtal1 + pll16x
       _xinfreq = 5_000_000
    
      LEDPin1 = 0
      SwitchPin = 23
    
    PUB LED_Button_On_Off
    
      dira[noparse][[/noparse]LEDPin1] := 1
      repeat
        outa[noparse][[/noparse]LEDPin1] := ina[noparse][[/noparse]SwitchPin] 'set LEDPin1-pin on the same value as input SwitchPin 
    
    




    what I understand what you want to do in the second program is

    if the SwitchPin changes its state for 3 times On-Off-On-Off-On-Off within 1 second the outputs 4..9 should light up ONE time with the pattern

    outa[noparse][[/noparse]4..9] := %011111
    outa[noparse][[/noparse]4..9] := %001111
    outa[noparse][[/noparse]4..9] := %000111
    outa[noparse][[/noparse]4..9] := %000011
    outa[noparse][[/noparse]4..9] := %000001
    outa[noparse][[/noparse]4..9] := %000000
    
    



    is this correct?

    If this is correct answer yes. If it is not correct you have to specify how the thing should work

    best regards

    Stefan
  • Jimm2Jimm2 Posts: 16
    edited 2010-01-28 23:33
    [noparse][[/noparse]∑CODE∑ ]
    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000


    LEDPin1 = 0
    LEDPin2 = 1
    SwitchPin = 23

    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]~~
    [noparse][[/noparse]∑/CODE∑]


    This Program works like this.......


    State of Switch(11111111111111111)
    (ON=1) and (OFF=0) power going to switch and led's
    (Ledpin1)(00000000000000000)
    (Ledpin2)(11111111111111111)

    State of Switch(10010010010010010)
    (Ledpin1)(11111111111111111)
    (Ledpin2)(11111111111111111)

    State of Switch(00000000000000000)
    (Ledpin1)(00000000000000000)
    (Ledpin2)(00000000000000000)

    What I need help with is how can I make (ledpin1) and (ledpin2), not just trun on two Led's, but turn on A set of led's.

    Like this..........................


    PUB ((((((((((((((((((((((((((((((Ledpin2)))))))))))))))))))))))))))))))))))))))))))))))))))))

    dira[noparse][[/noparse]4..9] := %111111
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %011111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %001111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000011
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000001
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000


    PUB ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((ledpin1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

    dira[noparse][[/noparse]4..9] := %111111
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000


    What I am trying to do, that I don't know how to do it. What switch is held down..[noparse][[/noparse]4..9] := %111111 led's are turned on. When switch is held
    on and held off and on and off and on and off.

    It will make it do this..........



    dira[noparse][[/noparse]4..9] := %111111
    outa[noparse][[/noparse]4..9] := %111111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %011111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %001111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000111
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000011
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000001
    waitcnt(clkfreq/4 + cnt)
    outa[noparse][[/noparse]4..9] := %000000
  • Jimm2Jimm2 Posts: 16
    edited 2010-01-29 01:46
    Then, every time you want to use one of these blinking methods, just put in a call to
    them by typing: led.Down or led.Turn}


    Where do I put this in the program to make it work......................................................................................................
  • Jimm2Jimm2 Posts: 16
    edited 2010-01-29 02:10
    {In your Propeller Tool, click on 'File' then click on 'New'. Copy and paste this into your new page
    by giving it a name such as LEDBlink. Save this file in the Parallax folder.

    Make an Object block above your VAR block by typing this: OBJ led: "LEDBlink"

    Then, every time you want to use one of these blinking methods, just put in a call to
    them by typing: led.Down or led.Turn}


    Where in the program do I put the (led.down or led.turn) to make it work.............I don't know..................................
Sign In or Register to comment.