Shop OBEX P1 Docs P2 Docs Learn Events
need a "kick start" with my spin object — Parallax Forums

need a "kick start" with my spin object

karlikarli Posts: 16
edited 2008-07-07 19:32 in Propeller 1
··· I'm only 2 months into spin code <and> have finished the Propeller·I/O Timing Basics Lab
··· <and> started on Methods and Cogs··<and> managed to run 3 LEDs in three [noparse][[/noparse]new]cogs
··· on my Prop 40 pin chip proto board as well as my Prop Stck USB·· <and> I'm
··· reasonably proud of myself (haha) but this object has got· me stumped <and> ·kept me indoors
··· all through the 4th July weekend.

··· All I want to do is: translate 1 of 5 button presses (on one push button, P21)
····into· a 2 sec window (or gate) and
··· decode the push button activity into turning on 1 of 5 LEDs.
··· A typical push and release can be done in 250msec,so a 2 sec window should be adequate.
··· i.e., if I push once, the first LED comes on (like P4 as used in all the Lab setups {eg: outa[noparse][[/noparse]9..4])
··· if I push 3x then P4 goes off and P6 comes on, etc.
··· If I were to take longer than 2 sec to complete 5 presses, the previously started 2 sec gate
··· would delete the presses that occurred and only decode the presses in the last 2sec gate
·· into the appropriate LED (1 through 5).
·· I studied the example-solutions #4 and 5 on pages 28/29 of the I/O timing basics manual
·· and I think I got them 'nailed'· ---and--- I have feeling the answer is in there;

·· I'm basically a hardware guy and I kept thinking how to translate this function, using a couple
·· of ICs into spin code.· Maybe that's what slowed me down and I need to Init my mind set.
·· Anyone who can 'kick start' me onto the right track with this object will be rewarded
·· with a pizza.· (we can work out the pizza delivery details via 1-on-1 email)
·· thanks!
··· karli

Comments

  • pgbpsupgbpsu Posts: 460
    edited 2008-07-07 18:14
    Hi Karli-

    I haven't actually coded this or tested it, but I hope it will give you a jump start:

    You'll need 2 cogs to do what I propose. One (timer cog) simply rattles off 2 second windows (high/true for 2 seconds then low/false for a few milliseconds. You can either use a global variable which is set high by the timer cog or have it set a pin high.

    The main cog is the one that watches the button to see if anything has been pressed.
    PSEUDO-CODE
    REPEAT
     IF currently_in_2sec_window (most of the time this is true)
       REPEAT
         IF button_currently_pressed
             wait/debounce
             number_of_pressed += 1
       UNTIL currently_in_2sec_window (every 2 seconds this goes false for a very short time)
     CASE 
        presses = 1; light LED 1
        presses = 2; light LED 2
        presses = 3; light LED 3
        presses = 4; light LED 4
        presses = 5; light LED 5
     presses = 0   'reset presses and start again
    
    


    I'm sure there are more clever ways to do this, but maybe this will help you get started.

    pgb
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2008-07-07 18:31
    As with all programming (and hardware) work on small components, check they work and link them together.

    1. You need to be able to detect a switch press and perhaps debounce
    2. You need to be able to increment a counter and translate this in to which led needs to be lit (forget at the moment about the 2 seconds)
    3. You need to get to grips with waitcnt, start simple
    4. Combine these elements into a loop
    5. Eat the pizza that YOU earnt!

    Graham
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-07 19:32
    pgbpsu and Graham covered most things. The only think I will add is if you want a running 2 sec window rather than a repeating 2sec window which pgbpsu describes.
    In that case look at tracking in an array the time of the last 5 button presses + 2sec. Everytime round the loop check if cnt has reached any of these times, if yes remove the buttom from the array. The number of times in the array is the no of button presses you are interested in. The only thing you dont describe is want happens if more than 5 presses in 2 sec? Either ignore in which case replace the oldest time with the new button press time (+2 sec) or an error in which case what do you want?
Sign In or Register to comment.