Shop OBEX P1 Docs P2 Docs Learn Events
Howdy and noob question from a new propeller user — Parallax Forums

Howdy and noob question from a new propeller user

kennabaukennabau Posts: 2
edited 2008-12-16 16:44 in Propeller 1
I picked up my 1st prop a few weeks ago to try and introduce myself to microcontroller programming...so far I've had good luck doing many of the tutorials i've found.

So, I got brave and started trying to build my own circuits and spin code to help run the christmas village train set...

I got the crossing signal to work - it starts when a train crosses a point in the tunnel, then stays with the two alternating leds until the last car clears the crossing.· So far so good.

Then I attempted to build some logic to throw the switches - I need about· a 1/4 second pulse to a relay to feed the 16vAC through·a relay (with a 5v coil) and depending on which way I want the switch to throw, I feed the AC from the 1st relay through either the NO or NC branch of a second relay.· It works good with a quick press of my test button.·

But here's my problem:· I want a 1/4 second·output on pin 3 when ever pin 24·is pulled down (·I have a pull up resistor on the input pin, and I do have the coil·diodes on my relays·)· My problem is that pin 3 stays high as long as pin 24 is pulled down.· To see this more clearly, I changed my output to 5 seconds, and here's what I got:
1 second press, 5 second output
6 second press, 6 second output

I'm guessing I'm missing some fundamental part of this, but I'm pulling my hair out trying to figure it out...· Any pointers would be greatly appreciated!

Here is my current spincode (I tried several variations, but this is what I currently have...)


Pub LoopTunnelSwitch
· dira[noparse][[/noparse]Loop_Switch_Pin] ~~····························· 'set the pin to output
· dira[noparse][[/noparse]3] ~~·················································· '3 is the source voltage relay control pin
· repeat
··· waitpeq (|<24,|<24,0)································ 'wait until pin 24 goes low
···· outa[noparse][[/noparse]Loop_Switch_Pin]~~···························· 'pull coil on source ac relay
····· outa[noparse][[/noparse]3]~~················································'pull coil on switch relay
····· waitcnt (clkfreq / 1000 *DelayMS + cnt)··········· ' hold coil for DelayMS
····· outa[noparse][[/noparse]3]~
····· outa[noparse][[/noparse]Loop_Switch_Pin]~····························· 'release coil

Kennabau.

Comments

  • T ChapT Chap Posts: 4,223
    edited 2008-12-11 06:48
    Maybe try a solution that incorporates a trap to lock up the repeat loop until the condition returns to normal.

    An example:

    
    pub main
       dira[noparse][[/noparse]24] := 0
       dira[noparse][[/noparse]Loop_Switch_Pin] := 1
       repeat  
          if ina[noparse][[/noparse]24] == 0      'if input goes low
            outa[noparse][[/noparse]Loop_Switch_Pin] := 1   'set output to high
            waitcnt(80_000_000/4 + cnt)   'wait .25 sec
            outa[noparse][[/noparse]Loop_Switch_Pin] := 0    'set output low
          repeat while ina[noparse][[/noparse]24] == 0  'trap the low condition if input is still low
    
    



    Also, it isn't clear that you have set p24 as input.

    Post Edited (TChapman) : 12/11/2008 6:57:50 AM GMT
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-12-11 18:30
    hello kennabau,

    you need to detect the low-high-TRANSITION of the switch
    and store this event in a variable

    
    VAR
      long low_high_detected
    '....
    
        if ina[noparse][[/noparse]24] == 1 ' if switch is pressed
          if not low_high_detected     'only if NO low-high-transition is detected 
            'set low-high-transition-flag immediatly true that if-condition above becomes false
            'and won't be re-entered until the clearing of low-high-transition-flag  
            low_high_detected := true   
            outa[noparse][[/noparse]Loop_Switch_Pin] := 1
            waitcnt(ClkFreq / 4 + cnt)
            outa[noparse][[/noparse]Loop_Switch_Pin] := 0
    
        'as long as switch is still pressed the low-high-transition-flag stays true
        if ina[noparse][[/noparse]24] == 0                 'wait until switch is opened again                  
          if low_high_detected          'then if a low-high-transition was detected 
            low_high_detected := false  'then clear low-high-transition-flag    
    
    
    



    best regards

    Stefan
  • kennabaukennabau Posts: 2
    edited 2008-12-12 03:38
    Thanks for the quick reply!

    I tried both solutions (I actually had Stefan's, I was just missing checking that the bit was still set in the last if statement.· )

    They both almost do what I want, but sometimes I get a second activation of the relay when I release the button, and sometimes I don't.· I'm using a 10k pull-up resistor to keep pin 24 high until the switch press pulls it to ground...

    I'm gonna keep playing with it - especially since I'm going to try to bounce an IR led off the bottom of the train for a sensor and I haven't even started that part yet...· now that I have an idea about which way I should go with the code, I'm gonna tackle the IR and come back to the switch.

    So, I changed the last if (the re-arm-if) to this:
          waitcnt (clkfreq * 10 + cnt)                       ' 10 second rearm timer
          low_high_detected := false
    

    (I took out the two if statements at the end completely)
    Since the train won't be in a position to trip the switch in the tunnel for well more than the 10 seconds, it works.· And it's much more stable - no flipping on the release of the switch.


    Thanks again!

    Kennabau.

    Post Edited (kennabau) : 12/12/2008 4:17:52 AM GMT
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-12-12 04:14
    Depending on what the switch is that you are using, you may have to debounce it. When you release a switch or make a switch there is contact bounce that may cause the retrigger. Certainly adding the 30 second timer you mentioned would be a big debounce.

    It is great to hear that you are making progress and having fun. You are making me miss my train layout.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • soshimososhimo Posts: 215
    edited 2008-12-16 16:44
    Hey Ken -
    I too am a neophyte when it comes to the Propeller, though I've had more than a few years (and no, I'm not sharing that number) under my belt with uControllers and before that lots of linear stuff in the Navy. As Timothy replied above you need switch debouncing. You can go about this in one of two ways. You can use a software method for switch debouncing which requires you poll the switch at a specific interval and save the last switch state - not too complex but not something you want to have to sprinkle around either. Another alternative is to use a debouncing circuit. It just takes an IC *7414 or equivalent*, a resistor and a cap. You basically make an RC filter with a schmitt trigger (if you are going pure analog you can lose the schmitt trigger unless you need a nice crisp transition).

    Here is a circuit diagram to get you started:

    dbounce4.gif

    From one noob to another, HTH.
Sign In or Register to comment.