Selecting several programs using a push button

ashleylad

Member
Picaxe is still very new to me but having a lot of fun. I have been looking at sample programs to try an understand how things work.

I have noticed that lots of commercial PIC circuits allow program changes by the use of a push button.

Is there an example program to detect a switch press then run the first program, press the button jump to the second and so on until 10 presses then back to the start.

Thanks in advance.

Ash
 
Last edited:

papaof2

Senior Member
If using an X2 chip, you could have 4 program slots which are selectable.

Otherwise, you would have to create a loop structure for each "program" and perhaps use an interrupt to detect the button press, increment a variable, then jump to the correct loop structure.

Just be aware of the limited amount of program space on the smaller PICAXE chips - it would be somewhat difficult to fit 10 "programs" into a 08M chip...

John
 

BeanieBots

Moderator
There are 1001 ways to do that.
The 'best' method really depends on what you want to do.

My favourite method is to build what is known as a "state machine" or "state engine".
I use interrupts to detect a button press which then changes the value of a variable. This can be as simple as adding 1 each time the button is pressed.

The main program then has a structure which does something different depending on the value of that variable (state).

A simple structure for doing that is the "select case" method.
 

ashleylad

Member
Thanks guys, have done some searches for example code to get me started and try and understand what to do but cannot find anything. Anyone know of any code examples?

cheers.

Ash
 

BeanieBots

Moderator
I'll start you off...

Code:
Symbol State = b0

Do
  Select case State
    Case 0
       'do something when in state 0

    Case 1
      'do something when in state 1

    Case 2
       'do something when in state 2

.......
    Case 255
       'do something at the highest possible state for a byte

    Case else
       'do something for a state you have not considered

  End select
Loop
I'll leave to somebody else to suggest ways of changing the value of Sate.
Interrupts would be my choice.
 

RavenKallen

New Member
I would use the Picaxe X2 series because, as mentioned, they have 4 program slots. All you do is add the slot number(0-3) at the begining of the program and it will save it to that slot number. You should write a new program and make sure to save it to slot 0. Then just use a simple if/then command to choose the correct run command. You should check out the syntax manual of the picaxe, along with the X2 briefing. It talks in great detail about the two commands/ directives. Hope it helps
 

westaust55

Moderator
I would use the Picaxe X2 series because, as mentioned, they have 4 program slots. All you do is add the slot number(0-3) at the begining of the program and it will save it to that slot number. You should write a new program and make sure to save it to slot 0. Then just use a simple if/then command to choose the correct run command. You should check out the syntax manual of the picaxe, along with the X2 briefing. It talks in great detail about the two commands/ directives. Hope it helps
As you say the X2 parts have 4 program slots.

But Ashleylad in post 1 states that he wants 10 separate program routines
therefore use of on XX goto/gosub or better still the case stryucture as BeanieBiots has mentioned is tentativelly the better options.
 

RavenKallen

New Member
Yeah, Beaniebots i was about to say that. I would still use the X2 parts anyway, because of their larger program space. It all depends on how big the 10 programs are.
 

ashleylad

Member
Guys, thanks to all. I just stated 10 programs as an example. I am trying to run several different sequences of leds flashing, I thought it would be a good way of trying to understand basic programming. I think as the programs will be short 10 may be do-able in an 08M. After reading the interfacing datasheet (3) It gives an easy way of obtaining some working code in the debounce example, this I can get me head round as it reminds me of BBC micro days. I would like to go one step further and learn to store values in the EEROM memory and recall to change programmes.

Not trying to take any shortcuts, but looking for pointers. By reading the interfacing documentation I have discovered you can drive a model servo directly. This is really very useful as most of my hobby apps will be for R/C projects. Even driving an LCD looks pretty straightforward. Like so many things in life I am very late jumping on the PICAXE bandwagon!

Thanks for all the help, you are all very kind. Appreciate it.

Ash
 

westaust55

Moderator
If you store all of the LED sequences in EEPROM,
then all you need is a pointer to the start of a sequence and
one piece of code. which uses the pointer and an index to the next step in the sequence.

some pseudo code:
Code:
EEPROM addr0, (sequence 0 data here ending in say $FF)
EEPROM addr1, (sequence 1 data here ending in say $FF)
EEPROM addr2, (sequence 2 data here ending in say $FF)

; etc

SYMBOL sequence = b0
SYMBOL seqstep = b1
SYMBOL baseaddr = b2 ; (may need a word variable)
SYMBOL currentaddr = b3 ; (may need a word variable)
SYMBOL LEDinfo = b4

; here determine the sequence to use - by monitoring or interrupt

If new sequence then 
save sequence No in variable "sequence"
endif

restart: step = 0

VLOOKUP sequence, (addr0, addr1, addr2, etc), baseaddr 

currentaddr = baseaddr + seqstep

READ currentaddr, LEDinfo
IF LEDinfo = $FF THEN restart 
; use $FF or maybe $00 to flag end of a sequence - 
; that way each sequence can be a different length

; here send the data to the LED display

inc seqstep
GOTO restart
 
Top