Demonstration of open looping style of programming - AXE092 Schools Experimenter

inglewoodpete

Senior Member
Using large pause statements in a program tends to stop everything, such as checking inputs. Often, a beginner will write a program and then complain that it is not detecting input events eg. button presses

The code below is something I wrote to show how to use program delays without large pauses. Using an open looping structure, it checks the input switch and controls timing of the output LEDs, all at the same time. It is also fun to use!

While the code can be adjusted to run on any PICAXE model, this was written specifically for the AXE092 "Schools Experimenter" board with an 08M. The code also uses the Max command: sometimes difficult for the beginner to understand.

Code:
' Demonstration of open-loop style of programming, allowing multiple 'tasks' to be handled
' Also demonstrates the Max command: sometimes difficult for the beginner to understand.
'
'What it does:      (Exercise: draw a flow diagram - use a large sheet of paper)
' * At startup, emits a musical trill sound
' * While turned on but not in use, emits a 'tick' sound every few seconds.
' * The pushbutton switch, if pressed momentarily, will toggle the Yellow LED on and off.
' * If the switch is held a little longer (>400mS), the green LED flashes quickly: this
'    indicates that an intermediate time has expired, affecting how the Yellow LED behaves.
' *   Then, if the switch is released, the yellow LED flashes.
' *   Pressing the switch again briefly turns the Yellow LED off.
' * If the switch is held for an extended time (>4 seconds), the Red LED is turned on.
'
'Tested on PICAXE 08M firmware 9.1  (AXE-092 Board)
' Revision History
' 30-Nov-2006  80 bytes Written by inglewoodpete
' 06-Apr-2011  80 bytes Revised presentation, renamed variables/constants
' 07-Oct-2011  94 bytes Added timer limit indication (Red and Green LEDs)
' 19-Feb-2012 120 bytes Added startup trill and battery-saving 'reminder' tick
'
'***** Definitions *****
'
#PICAXE 08M
'      Variables - byte variables are prefixed with 'b'; words with 'w'
'
Symbol bSwTime    = b0
Symbol bLEDState  = b1      '0 = off, 1 = on, 2 to 255 = flash
Symbol bReminder  = b2      'Battery saver: reminder that the AXE092 is turned on!
'
'      Hardware  - outputs are prefixed with 'o'; inputs with 'i'
'
Symbol oRedLED    = 0      'Indicator: Timer has maxed out
Symbol oYellowLED = 1      'Indicator: Result
Symbol oGreenLED  = 2      'Indicator: Input Timer
Symbol oBeeper    = 2      '   -pin 2 is shared with piezo 'alarm'
Symbol iSwitch    = Pin3   'Input
'
'       Constants - constant definitions are prefixed with a 'c'
'
'      Logical Conditions
Symbol cActive    = 1      'Can be 0 or 1
'      Counter Values
Symbol cLongPress = 40     'Discrimination between short and log press
Symbol cHalfTime  = 10     'LED Hi-Lo transition
Symbol cFullTime  = 20     'LED Lo-Hi. Must not be > 254
Symbol cNoteTime  = 7      'Duration of startup notes
Symbol cGap       = 4      'Duration of gaps between notes
'
'***** End Definitions *****
'************************************************************************
'*****  Begin Program  *****
'
'***** Initialisation  *****
Init:
   Sound oBeeper, (50,cNoteTime,0,cGap,70,cNoteTime,0,cGap,90,cNoteTime,0,cGap,120,cNoteTime)
   '
'***** Begin Main Loop *****
   Do
      bSwTime = 0
      Low oRedLED
      Low oGreenLed
      'This Do loop will be bypassed if switch not pressed
      Do While iSwitch = cActive   'Determine the time that the switch is pressed
         Pause 10                  'Only ever use small delays
         bSwTime = bSwTime Max 254 + 1   'Max prevents overflow
         If bSwTime = 255 Then     'Indicate that timer has maxed out
            High oRedLED
         ElseIf bSwTime > cLongPress Then
            Toggle oGreenLed
         EndIf
         Gosub HandleFlash         'Maintain flashing if required while button held down
      Loop                         'Remain in loop while switch pressed
      Pause 10                     'Only ever use small delays
      'Process any switch press
      If bSwTime > cLongPress Then
         'Start LED flashing
         bLEDState = 2
         High oYellowLED
      ElseIf bSwTime > 0 Then      'Short press
         'Turn LED On or Off  (Off also stops flashing)
         If bLEDState = 0 Then     'Off: so turn LED on
            bLEDState = 1
            High oYellowLED
         Else                      'Turn off
            bLEDState = 0
            Low oYellowLED
         Endif
      Else   'Ie. bSwTime = 0      for when the button is not pressed
         Inc bReminder             'The 'Reminder' counts from 0 to 255 each loop
         If bReminder = 0 Then     ' then the counter "rolls over" to zero
            PulsOut oBeeper, 10    'When bReminder = 0, output a "tick" sound
         EndIf
      EndIf
      Gosub HandleFlash
   Loop
'
'***** Subroutines *****
'
HandleFlash: If bLEDState > 1 Then
                'Flash control required only for range 2 to 255
                Select Case bLEDState
                Case > cFullTime
                   'Cycle complete: Flash the LED back on again
                   High oYellowLED
                   bLEDState = 2   'Reset counter to minimum
                Case > cHalfTime
                   'Half time of the flash cycle: reset the LED
                   Low oYellowLED
                EndSelect
                Inc bLEDState
             EndIf
             Return
'
'***** End Of Program *****
 
Last edited:

inglewoodpete

Senior Member
The code posted above has been revised to give a "trill" of sound at startup. It also emits a periodical "tick" sound while idle, to remind students that the circuit remains operational - saving the batteries from forgetful students.

Since I don't yet have an 08M2 in my collection, I have not adjusted the code for operation with the updated 8-pin PICAXE. (I come from a poor family: my driver is poor, the cook is poor, the gardener is poor......). :) Actually, there is a chance that this code and several AXE092 kits will be used in a local tertiary education program, so I might get an opportunity to rewrite the code for that.
 

westaust55

Moderator
Good example for the newbies IWP.

One comment:
assuming the revisions are in chronological order
' 19-Feb-2011 120 bytes Added startup trill and battery-saving 'reminder' tick

maybe 2012 ?
 
Top