Shop OBEX P1 Docs P2 Docs Learn Events
Need Help With Switch De-Bounce — Parallax Forums

Need Help With Switch De-Bounce

NAGCSNAGCS Posts: 2
edited 2006-05-21 18:53 in General Discussion
Here is a project I started a few days ago for the XBOX 360 The circuit uses RGB LED's Port RA.3 is used for switching patterns or colors. 3 Lines on the RB Ports switch the grounds on the RGB LED's. Port RC Controls the common Postive terminal of the RGB LED. I am having a big problem with switch bounce when I press the button I want to go cleanly to the next pattern not jump around. I will show some pictures then show the code if any one can help It would be great.

3608.JPG

3606.JPG

3607.JPG


3609.JPG

innerringlight360.jpg


360cb1.JPG



360ip.JPG


'
' Device Settings
'
DEVICE········· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000

'
' IO Pins
'
Select··VAR·RA.3···' Pattern Select
TRIS_Sel·VAR·TRIS_A
Blue··VAR·RB.0
Green··VAR·RB.1
Red··VAR·RB.2
Light··VAR·RC
TRIS_Light·VAR·TRIS_C
'
' Constants
'
Delay·· ·CON·100···' 180 ms between steps
Delay1··CON·80···' 220 ms between steps
'
' Variables
'
maxSteps·VAR·Byte···' steps in sequence
idx··VAR·Byte···' step pointer
' =========================================================================
· PROGRAM Start
' =========================================================================
Pattern0:
· DATA· 8
· DATA·%11111110
· DATA·%11111101
· DATA·%11111011
· DATA·%11110111
· DATA·%11101111
· DATA·%11011111
· DATA·%10111111
· DATA·%01111111
Pattern1:
· DATA· 8
· DATA·%01111111
· DATA·%10111111
· DATA·%11011111
· DATA·%11101111
· DATA·%11110111
· DATA·%11111011
· DATA·%11111101
· DATA·%11111110
'
' Program Code
'
Start:
· Light = %00000000
· TRIS_Light = %00000000
Main:
· PAUSE Delay
· HIGH Red
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S2_Go
· idx = 1
S2_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Red
· PAUSE 200
· HIGH Green
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S3_Go
· idx = 1
S3_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Green
· PAUSE 200
· HIGH Blue
· READ Pattern0. maxSteps
IF idx <= maxSteps THEN S4_Go
· idx = 1
S4_Go:
· READ Pattern0 + idx, Light
· idx + idx + 1
· TOGGLE Blue
· PAUSE 200
· IF Select = 0 THEN Main0
· PAUSE 50
· GOTO Main
Main0:
· PAUSE Delay1··
· HIGH Green
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S5_Go
· idx = 1
S5_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main1·········
· GOTO Main0
Main1:
· PAUSE Delay1··
· HIGH Blue
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S6_Go
· idx = 1
S6_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main2········
· GOTO Main1
Main2:
· PAUSE Delay1··
· HIGH Red
· READ Pattern0, maxSteps
· IF idx <= maxSteps THEN S7_Go
· idx = 1
S7_Go:
· READ Pattern0 + idx, Light
· idx = idx + 1
· PAUSE 25
· IF Select = 0 Then Main3··········
· GOTO Main2
Main3:
· PAUSE 200
· If Select = 0 THEN Main
· Light = %11111111
· GOTO Main3
·

Comments

  • ElectronegativityElectronegativity Posts: 311
    edited 2006-05-20 21:32
    Hi NAGCS.

    That project looks awesome!

    The easiest thing to do is wait for a millisecond between checking to see if a key is pressed.
    That is a conservative value and should work fine, but you can play around with reducing the delay to find out what the actual timescale of bouncing is.

    Good Luck.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I wonder if this wire is hot...
  • BeanBean Posts: 8,129
    edited 2006-05-20 22:55
    Now there is someone who knows how to post a question. Nice job, described the exact problem, post ALL the code and even showed the PCB.

    NAGCS,
    I see you have PAUSE 200, but I don't quite follow your select logic ? Is select = 0 when the button is pressed or does it equal 1 when the button is pressed ?

    BTW: If you download the latest version of SX/B you use IF...THEN...ENDIF blocks instead of all the GOTOs and labels. Also has DO...LOOP loops. Would make the code alot easier to follow.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com


    COMING SOON "SD DATA LOGGER" www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
  • NAGCSNAGCS Posts: 2
    edited 2006-05-21 15:03
    If Select = 0·· Basicly means if there is a ground pulse or (0) on RA.3 switch to the next subroutine. I was wondering if PULSEIN would be a better command but I don't know how to use it right. I am just a beginner when it comes to SXBASIC I excell in the hardware aspect but programming is a struggle for me. Basicly when powered up the circuit displays the first routine. When the NO Push button is pressed a ground is sent to RA.3 sending it to the next routine. There is a 10k pull-up resistor on the input to B+. I was wondering also if I have the input circuitry right. I also tried a transistor debounce circuit with no betters results.
  • BeanBean Posts: 8,129
    edited 2006-05-21 18:53
    Okay, the part I don't understand is here:
    S7_Go:
      READ Pattern0 + idx, Light
      idx = idx + 1
      PAUSE 25
      IF Select = 0 Then Main3           
      GOTO Main2 
    Main3:
      PAUSE 200
      If Select = 0 THEN Main
      Light = %11111111
      GOTO Main3
    
    



    After the PAUSE 25, you are saying if the button is pressed then goto Main3.
    So let's assume the button is pressed, now we are at Main3, if the user releases
    the button during the PAUSE 200 you will be stuck in the Main3 loop forever.

    Bean.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com


    COMING SOON "SD DATA LOGGER" www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
Sign In or Register to comment.