how do i program to count a number of pulses then give an output

peter howarth

New Member
I would like to program a picaxe to count,say 10 pulses, be it a second long,a second in between pulses, then once say 10 pulses are counted, then give or change a status on an output, then have the picaxe continue on with other stuff,
also is it possible to program a picaxe to have an output flash a led whilst ever power is applied,, as well as the picaxe perform its other programmed (sequential) functions..
 

lbenson

Senior Member
This program accomplishes the first--you can run it in the simulator.
Code:
#picaxe 08M2

symbol bPinVal = bit0
symbol pulseCount = b1
symbol pPulsePin = pinC.3
symbol pLED1 = C.1
do
  if bPinVal <> pPulsePin then
    bPinVal = pPulsePin
    if bPinVal = 1 then
      inc pulseCount
      if pulseCount = 10 then
        toggle pLED1 ' on for 10 pulses, off for 10 pulses
        pulseCount = 0
      endif
    endif
  endif
  ' now do other stuff
loop
The following does the same but also toggles pin C.0 every second (using the "time" variable available on M2 PICAXEs).
Code:
#picaxe 08M2

symbol bPinVal = bit0
symbol pulseCount = b1
symbol wSeconds = w13

symbol pPulsePin = pinC.3
symbol pLED1 = C.1
symbol pLED0 = C.0

wSeconds = 0
time = 0

do
  if wSeconds <> time then
    wSeconds = time
    toggle pLED0 ' toggle every second
  endif
  if bPinVal <> pPulsePin then
    bPinVal = pPulsePin
    if bPinVal = 1 then
      inc pulseCount
      if pulseCount = 10 then
        toggle pLED1 ' on for 10 pulses, off for 10 pulses
        pulseCount = 0
      endif
    endif
  endif
  ' now do other stuff
loop
This also will run in the simulator, but note that because of the slow speed of the simulation, the toggling will not strictly occur every second. It will on a real chip (unless your other code takes so long to execute that some seconds are missed).
 

hippy

Technical Support
Staff member
I would do the pulse counting this way -

Code:
#Picaxe 08M2

Symbol PIN_IN  = pinC.3
Symbol LED_OUT = C.2

Symbol counter = b0

Low LED_OUT
For counter = 1 To 10
  Gosub WaitForPulse
Next
High LED_OUT
End

WaitForPulse:
  Do : Loop While PIN_IN = 1
  Do : Loop Until PIN_IN = 1
  Return
Multi-tasking can be used to have a 'RUN' LED flash at the same time -

Code:
#Picaxe 08M2

Symbol PIN_IN  = pinC.3
Symbol LED_OUT = C.2
Symbol RUN_OUT = C.1

Symbol counter = b0

Start0:
  Low LED_OUT
  For counter = 1 To 10
    Gosub WaitForPulse
  Next
  High LED_OUT
  End

WaitForPulse:
  Do : Loop While PIN_IN = 1
  Do : Loop Until PIN_IN = 1
  Return

Start1:
  Do
    Toggle RUN_OUT
    Pause 1000
  Loop
 
Top