120ThingsIn20Years
Senior Member
I couldn't find this and needed it, so I thought I'd share it now that it's done.
It's my first public post of finished code so please excuse anything odd.
It allows a user to hold down a momentary switch until an LED indicates the switch has been recognised, then release the switch to change to the next mode. If the limit of modes is reached, it returns to the default mode, and runs the code in "Main:"
Any feedback (of any kind) would be welcome.
View attachment Mode Button .bas
It's my first public post of finished code so please excuse anything odd.
It allows a user to hold down a momentary switch until an LED indicates the switch has been recognised, then release the switch to change to the next mode. If the limit of modes is reached, it returns to the default mode, and runs the code in "Main:"
Any feedback (of any kind) would be welcome.
Code:
'
'Mode Button - To simulate, set to Picaxe 08M - 2011-10-31-1615
'-------------------------------------------------------------------------------------------
'
'this code is designed to be used wherever you need a mode button to select
'different bits of code to run.
'I used it to change from normal operating mode to a "get new settings" mode on a fish feeder.
'mode 2 populated a variable NumberOfFeedsPerDay from a pot, and then reported the number by
' flashing a LED.
'Mode 3 did the same thing, but set the feed size.
'
'To use, hold the mode button down until all the LED's come on
'In simulator, click and unclick button 3
'
'There are two sections you need to copy and edit to add a new mode. They are commented below.
'--------------------------------------------------------------------------------------------
'Code by 120thingsin20years or BullwinkleII
'
'120thingsin20years.blogspot.com
'
'uses 115 bytes
'
'NO RIGHTS RESERVED
'
'Variables - I count down byte variables, and count up word variables--
symbol CurrentMode = b13 ' used by the program
symbol MaxModes = b12 ' set this value in Init:
'
'
Main:
Gosub Init ' populate some variables
Continue:
gosub TightLoop 'this loop is kept tight so it doesnt miss any button presses
' but it doesnt really need to be as you can hold down the mode button
'
'Add the rest of your Main: code here
'
goto Continue
End
'-----------------------------------------------------------------------------------------
'
Init: ' populate some variables
let MaxModes = 2 'set this to the number of modes you are using
let CurrentMode = 1 ' this just sets the default to run the code under Continue: in Main:
high b.1 ' this is so you can see what's going on in the simulator
low b.2 ' this is so you can see what's going on in the simulator
return
'-----------------------------------------------------------------------------------------
TightLoop: ' calls ChangeMode when button pressed and then released
if pin3 = 1 then
gosub WaitForRelease 'wait until you let go of the button so you dont skip a mode
gosub ChangeMode
end if
'goto TightLoop
return
ChangeMode:'
if CurrentMode = 1 then
high b.1 ' this is so you can see what's going on in the simulator
low b.2
low b.4
return ' bail out because the default mode is to just run Main:
end if
if CurrentMode = 2 then
low b.1 ' this is so you can see what's going on in the simulator
high b.2
low b.4
gosub Mode2 ' go and do whatever you want mode 2 to do
'
end if
'==========================================================================================
'---==== copy this 1st of 2 sections to add a mode, and change the Mode3's ModeXX ========
'----- also change MaxModes value in Init to however many modes you have.
if CurrentMode = 3 then
low b.2 ' this is so you can see what's going on in the simulator
low b.1
high b.4
gosub Mode3 ' go and do whatever you want mode 2 to do
'
end if
'
'=================== end copy section ======================================================
'==========================================================================================
if CurrentMode > MaxModes then 'if you have run out of modes
'
let CurrentMode = 1 'go back to the first mode
high b.1 ' this is so you can see what's going on in the simulator
low b.2
low b.4
'
end if
'
return
'
'--------------------------------------------------------------------------------------------
Mode2:
if pin3 = 1 then ' check to see if someone is holding down the mode button
'
gosub WaitForRelease
return
'
endif
'
'whatever code you want for your mode 2 goes here
'
goto Mode2
'
'==========================================================================================
'=== Also copy this 2nd of 2 sections to add a mode, changing the Mode3's to ModeXX etc ===
Mode3:
if pin3 = 1 then ' check to see if someone is holding down the mode button
'
gosub WaitForRelease
return
'
endif
'
'whatever code you want for your mode 3 goes here
'
goto Mode3
'=================== end copy section ======================================================
'==========================================================================================
'
'----------------------------------------
'
WaitForRelease: ' this traps you here until you let go of the button. It allows you
' to hold down the button until the program flows to here, but stops
'you from skipping onto the next mode
CurrentMode = CurrentMode + 1
ContinueWaiting:
if pinc.3 = 1 then
high b.1 'It's a good idea to indicate that you are in this section by lighting
high b.2 'a LED. I have a few so I light them all until you release the button
high b.4
goto ContinueWaiting 'loop back until the button is released
endif
'
low b.1 'Then I turn them all off to indicate you are out of this section.
low b.2
low b.4
'
return