Shop OBEX P1 Docs P2 Docs Learn Events
counter and switch programming help — Parallax Forums

counter and switch programming help

w. williamsw. williams Posts: 9
edited 2005-02-04 01:05 in BASIC Stamp
i'm doing a mini scoreboard project and im trying to write the code for a counter that is controlled by a "flingy" switch (sorry don't know correct term).· I just simply want the counter to count up one number when i fling the switch up (I position), and count down one number when i fling the switch down (O position).· i dont want the counter to count unless the switch tells it to.· If anyone can help me i would greatly appreciate it.·

Comments

  • Erik ArendallErik Arendall Posts: 21
    edited 2005-02-03 17:55
    You can get a SPDT switch and monitor the pins for direction. If Pin 1 goes High you can increment a counter , and if pin 2 goes high you can decrement a counter. You will just have to put your switch in the direction you want.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-02-03 18:43
    Erik has it.· Here's a bit of code to get you started.· Note the the constant called SwDelay lets you control how fast the count increments or decrements when you're holding the switch.· If you don't hold it, the delay should be enough to give a single count per "flick."

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    CntUp      PIN    0
    CntDn      PIN    1
     
    CntMax     CON    100
    CntMin     CON    0
     
    Yes        CON    1
    No         CON    0
    SwDelay    CON    500
     
    counter    VAR    Word
     
     
    Main:
      DO
     
        IF (CntUp = Yes) THEN
          IF (counter < CntMax) THEN
            counter = counter + 1
            GOSUB Update_Display
            PAUSE SwDelay
          ENDIF
        ENDIF
     
        IF (CntDn = Yes) THEN
          IF (counter > CntMin) THEN
            counter = counter - 1
            GOSUB Update_Display
            PAUSE SwDelay 
          ENDIF
        ENDIF
     
      LOOP
     
      END
     
     
    Update_Display:
      
      ' your code here...
     
      RETURN
    


    A schematic is attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA


    Post Edited (Jon Williams) : 2/3/2005 6:51:03 PM GMT
    457 x 333 - 8K
  • w. williamsw. williams Posts: 9
    edited 2005-02-04 01:05
    thanks so much for your help!! i really appreciate it!
Sign In or Register to comment.