Question

I am trying to count the number of times a button is pressed at input pin C.4 on a picaxe 14M2. I would then like to have a 'mode' that sets b.4 high for 5 seconds then low. This 'mode' needs to repeat the number of times you press the button before hand.

If this makes any sense, how would I do this?

Here is what I have so far...

init: 
    let b0 = 0 
main: 
    low B.1
    low B.2
    low B.3
    low B.4
    low B.5
    if pinC.4 = 1
    let b0 = b0 +1
    goto mode
    Endif
    goto main 
mode: 
    high B.4
    wait 5
    low B.4
    goto main
Was it helpful?

Solution

If I understand your question you want to first count a number of button presses, then output that number of 5 second pulses. But how will your program decide that you've finished your series of button presses, and want it to carry on and generate the sequence of pulses?

Here's a possible solution, but you'll have to decide if it's suitable and adapt it if not:

b0 = 0                                  ' initialise counting variable
w1 = 0                                  ' initialise timing variable (a 2-byte word)

countpresses:
    pause 10                            ' wait for 10 ms
    w1 = w1 + 1                         ' increment the timing variable
    if pinC.4 = 0 then countpresses     ' loop until button pressed 
wait_release:
    pause 10
    w1 = w1 + 1                         ' increment the timing variable
    if pinC.4 = 1 then wait_release     ' loop until button released
    b0 = b0 + 1                         ' increment the counter

if w1 < 200 then countpresses           ' keep counting until 4 seconds have elapsed

if b0 > 0 then
    for b1 = 1 to b0
         high B.4
         pause 5000                     ' take B.4 high for 5 seconds
         low B.4
         pause 1000                     ' and low for 1 second between pulses
    next b1
endif

This will count how many times you press the button in a 4 second period (200 x 20 ms), then output that number of pulses. The pause statements make sure that you don't count 'bounces' of the switch contacts that might occur in the few milliseconds after the button is pressed or released, and the second loop makes sure that you only count once for each press rather than incrementing as fast as the PICAXE can go for as long as you hold the button down! You didn't say how long B.4 should go low for in between the 5 second high pulses - in the code above I've made that 1 second.

If that's not exactly what you want then it shouldn't be hard to figure out how to modify it, for example to wait for a number of seconds after the last time you release the button.

I've used a word variable for the timing counter so that the maximum time to wait isn't limited to 255 counts - you could change the 200 in the code to any value up to 65535 if you wanted (but you should have a think about what might happen if it got near that value). If you're a PICAXE beginner then do read the section of the manual about how byte and word variables relate to each other, which might not be obvious.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top