Mode button, select code section to run with a button - no interrupt

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.

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
View attachment Mode Button .bas
 

hippy

Senior Member
Excellent work and well done.

Good trick to count down byte variables and count up word variables.

There are a few tricks which can be used to remove GOTO commands which can improve the readability of code and which may make the functionality clearer though it's perhaps a matter of personal preference ...

Code:
Mode3:
    if pin3 = 1 then  
        gosub WaitForRelease
        return
    endif
    ' code for mode 3
    goto Mode3
could become ...

Code:
Mode3:
    do until pin3 = 1
      ' code for mode 3
    loop  
    gosub WaitForRelease
    return
And ...
Code:
    ContinueWaiting:
        if pinc.3 = 1 then 
            high b.1
            high b.2
            high b.4   
        goto ContinueWaiting
    end if
could become ...

Code:
   ContinueWaiting:
        do while pinc.3 = 1
            high b.1
            high b.2
            high b.4   
       loop
 

120ThingsIn20Years

Senior Member
Excellent work and well done.

Good trick to count down byte variables and count up word variables.

There are a few tricks which can be used to remove GOTO commands which can improve the readability of code and which may make the functionality clearer though it's perhaps a matter of personal preference ...

Code:
Mode3:
    if pin3 = 1 then  
        gosub WaitForRelease
        return
    endif
    ' code for mode 3
    goto Mode3
could become ...

Code:
Mode3:
    do until pin3 = 1
      ' code for mode 3
    loop  
    gosub WaitForRelease
    return
Are "Do While" and "Do Until" the preferred methods even where there is a large amount of code within that loop?

And if so is it just because they say Loop telling you they go back rather than GoTo Something , and you might not know if that Something is at the top of the section you are in, or somewhere new?

Or is there some other advantage?
 

hippy

Senior Member
The advantage I see of DO-LOOP is that you can easily tell which code is within it if you indent, and yes, you don't have to worry about where GOTO goes to, where that label is and not having it reduces typing and errors through mistyping.

As for large amounts of code in any structure, it's often best to take that out into a separate routine so that makes the control loop clearer and then the routine can be inspected for what it does. For example, this code would be quite understandable even without knowing the routines which may be quite long or complicated ...

Code:
Do
  Gosub ReadTemperature
  Gosub DisplayTemperature
  Gosub SendTemperatureVia433MhzTransmitter
Loop
 

120ThingsIn20Years

Senior Member
The advantage I see of DO-LOOP is that you can easily tell which code is within it if you indent, and yes, you don't have to worry about where GOTO goes to, where that label is and not having it reduces typing and errors through mistyping.

As for large amounts of code in any structure, it's often best to take that out into a separate routine so that makes the control loop clearer and then the routine can be inspected for what it does. For example, this code would be quite understandable even without knowing the routines which may be quite long or complicated ...

Code:
Do
  Gosub ReadTemperature
  Gosub DisplayTemperature
  Gosub SendTemperatureVia433MhzTransmitter
Loop
Thanks for that.

I remember you saying that, and have just written some new code for the first time in ages and put what you said into play. It made so much difference to my being able to follow my own code. I look at some of the stuff I did last, and cant follow what it does :)
 

120ThingsIn20Years

Senior Member
The advantage I see of DO-LOOP is that you can easily tell which code is within it if you indent, and yes, you don't have to worry about where GOTO goes to, where that label is and not having it reduces typing and errors through mistyping.

As for large amounts of code in any structure, it's often best to take that out into a separate routine so that makes the control loop clearer and then the routine can be inspected for what it does. For example, this code would be quite understandable even without knowing the routines which may be quite long or complicated ...

Code:
Do
  Gosub ReadTemperature
  Gosub DisplayTemperature
  Gosub SendTemperatureVia433MhzTransmitter
Loop
Excellent work and well done.

Good trick to count down byte variables and count up word variables.

There are a few tricks which can be used to remove GOTO commands which can improve the readability of code and which may make the functionality clearer though it's perhaps a matter of personal preference ...

Code:
Mode3:
    if pin3 = 1 then 
        gosub WaitForRelease
        return
    endif
    ' code for mode 3
    goto Mode3
could become ...

Code:
Mode3:
    do until pin3 = 1
      ' code for mode 3
    loop 
    gosub WaitForRelease
    return
And ...
Code:
    ContinueWaiting:
        if pinc.3 = 1 then
            high b.1
            high b.2
            high b.4  
        goto ContinueWaiting
    end if
could become ...

Code:
   ContinueWaiting:
        do while pinc.3 = 1
            high b.1
            high b.2
            high b.4  
       loop

I was doing a google search for something, and as is often the case, this thread of my own came up.

I haven't been around for a while due to all my vomit falling out over the last few years (general health issues), but I dont think I ever thanked you quite enough for all the help you gave me when I first got into this electronics caper.

It's been a bit of a life changer.

So...

Thanks*.




*I suspect I speak for a very large number of people.
 

hippy

Senior Member
I haven't been around for a while due to all my vomit falling out over the last few years (general health issues), but I dont think I ever thanked you quite enough for all the help you gave me when I first got into this electronics caper.
Sorry to hear about your health problems and I would imagine the oft said "better out than in" doesn't quite feel that way when going through it!

Good to see you back and and don't worry about any lack of thanks. I, and I am sure other people, who have helped you and others, are simply delighted that it does.
 

techElder

Well-known member
Health, yours and others, holds us all back. Hope this means your health is better.

How many of those “120” things are left for you to do?
 

120ThingsIn20Years

Senior Member
Health, yours and others, holds us all back. Hope this means your health is better.

How many of those “120” things are left for you to do?
Getting there. It's been nothing life threatening, just gut issues. I spent a year in bed though.

How many things to go... checks blog.... 101 to go as far as the blog is concerned. oh well :)

I have still been learning, (for instance I now make websites rather than my 25 years as a production potter making domestic ware), and I've picked up a few other things as well, but there's obviously so much more to electronics than can be understood in any one lifetime. Not that you can learn everything about making cheese or even bread in a single lifetime.

That's why I limited myself to only 120 things :)

Currently I'm doing a refresher in electronics fault finding by looking at old projects, and trying to figure out what they probably did, and learning about the HC-12 transceiver.
 
Top