Help with push-button to change LED flash pattern

dbeatty

New Member
Hi all,

I have learned a lot from the people here. To-day I am asking for some help with my program. Been working with it since July 2015.
I want a push button to be detected immediately to change a LED flash sequence.

Manual 3 says SETINT is polled between program lines. I thought that would get it done.
As written the program will leave flash1 and proceed to flash2 but will not leave flash2.

The program is below. An experienced eye is appreciated.

Thank you for your help.

David

Code

Code:
'08M2 FBI style LED strobing_Jul_26_2015_1007.bas
'Program by David Beatty @ Edmonton, Alberta
'inspired by a Youtube video [URL]https://www.youtube.com/watch?v=tQoZCix_1mY[/URL]
'Labelled RC car flasher (FBI style) by Vicsys, [URL]http://vicsys.hu[/URL]
'

#Picaxe 08M2
#no_data
setint %00010000,%00010000            'button on C.4'
pinC.4 = b1                        'declare button on C.4
b1 = 0                        'initialize b1'

    


    
Interrupt:                        'used to get an imediate button response
        
        high C.0: high C.1        'turn 'em all on to show flash sequence change
        pause 1000                'time to see the button push was detected
        
        b1 = b1 + 1                'cause the program to select the next flash sequence
        
        if b1 = 1 then flash1         'array of flash sequences
        if b1 = 2 then flash2 
        if b1 = 3 then flash3 
        if b1 = 4 then flash4 
        if b1 = 5 then flash5 
        if b1 = 6 then flash6 
        if b1 = 7 then flash7 
        if b1 = 8 then flash8 
        if b1 > 8 then flash1 
        
        setint %00010000,%00010000    'resets the setint for the next button push 
        
        return    
                
        
                            'flash sequence catalogue
                                
                    
flash1:
    do
        high C.0: high C.1        'all on for area illumination
        
    loop
    
flash2:    
    do
        pulsout C.0, 255: pulsout C.1,255    'simultaneous flash of both LEDs
        pause 150
        pulsout C.0,255: pulsout C.1,255
        
    loop

flash3:                            'LEDs both pulse 3X, wait 1 sec. - loop
    do
        high C.0, C.1
        pause 100
        
        low C.0, C.1
        pause 100
        
        high C.0, C.1
        pause 100
        
        low C.0, C.1
        pause 100
        
        high C.0, C.1
        pause 100
        
        low C.0, C.1
        pause 600
        
    loop
        
flash4:                            'LED right pulses 2X then LED left pulses 2X loop
    do
        pulsout C.0, 255:pause 60: pulsout C.0,255 pause 400
        
        pulsout C.1, 255:pause 60: pulsout C.1,255 pause 400
        
    loop
        
flash5:                            'LED right pulses 2X and remains high while LED left pulses 2X and remains high -                                 loop
    
    do
        pulsout C.0,150
        pause    80
        high C.0
        pause 30
        low C.0
        Pulsout C.1,150
        pause 80
        high C.1
        pause 30
        low C.1
        pause 30
        
    loop

flash6:                            'LED right pulses 2X pausing while LED left pulses 2X and pauses, loop
    do
        pulsout C.0,255
        pause 100
        pulsout C.0,255
        pause 100
        
        pulsout C.1,255
        pause 100
        pulsout C.1,255
        pause 125
    loop
    
flash7:                            ' wig wag pattern
    do
        pulsout C.0,255
        pause 100
        pulsout C.1,255
        pause 100
        
        pulsout C.0,255
        pause 100
        pulsout C.1,255
        pause 100
        
        pulsout C.0,255
        pause 100
        pulsout C.0,255
        pause 100
        
        pulsout C.1,255
        pause 100
        pulsout C.1,255
        pause 100
        
        pulsout C.0,255
        pause 100
        pulsout C.1,255
        pause 100
        pulsout C.0,255
        pause 100
        pulsout C.1,255
        pause 100
    loop
    
flash8:                            'alternating wig wag pattern
    do
        pulsout C.0,255
        pause 100
        high C.0
        pause 170
        low C.0
        pause 600
        
        pulsout C.1,255
        pause 100
        high C.1
        pause 170
        low C.1
        pause 600
    loop
 
Last edited by a moderator:

techElder

Well-known member
Define what you mean by "... to be detected immediately ...."

You probably don't need an Interrupt routine. Just keep the detection of "button on C.4" in a tight loop.
 

hippy

Technical Support
Staff member
There was an almost similar question a couple of weeks ago for which I provided a solution. Don't have time to search for which thread it was at present but it's there somewhere.
 

Buzby

Senior Member
When your interrupt routine runs, work out on paper what happens. The simulator is no use here.

The sequence you have is :

Interrupt triggered, inc b1, goto selected flash routine, loop forever in that routine.

You never reach the code that re-enables the interrupt.


Solution : In the interrupt inc b1, then return. In the main loop do the if/then stuff.
 

hippy

Technical Support
Staff member
Perhaps this post contains the information that Hippy suggested.

http://www.picaxeforum.co.uk/showthread.php?29444-Multiple-Interrupts
That's the one and many thanks for tracking it down.

But while I was away I realised there's an easier way. Simply RESET and start the program again, jump to whatever routine one now wants to run.

Of course, the problem there is that 'everything' is zeroed or re-initialised on RESET, and one doesn't like to wear EEPROM out ( and it may be being used for something else anyway ), so how can a number indicating what to do next be preserved through RESET ... ?

http://www.picaxeforum.co.uk/showthread.php?29548-Preserving-data-for-after-a-RESET-command
 

techElder

Well-known member
I forgot how slick that RESET save idea is.

dbeatty, only one program, but switch the variable values and save them with Hippy's RESET save idea, then RESET to run your program with the new values.

Slick!
 

dbeatty

New Member
Thanks everyone.

Texasclodhopper I had one variation of the program which included a goto in eacg of the flash1 to flash8 sequences. I had to hit the button at the right moment while the subroutine was running to get a response. I tried Start: routines for a parallel program to catch the button press, that didn't work. Now I tried the setinit as the pin is polled between program lines to catch the button push.
As can be seen in the youtube video the original programmer is able to change flash sequences on demand.
I'll read the help offered above and pitch it against what I'm after.

Cheers

David
 

marks

Senior Member
Hi dbeatty,
merry Christmas and welcome to the forum.
I didn't look at the video just modded your code abit
I think its what your trying to do hopefully should be enough to get you rolling !
Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Navy]#no_data[/color]
[color=Blue]setint [/color][color=Navy]%00010000[/color][color=Black],[/color][color=Navy]%00010000 
               
  [/color][color=Black]main:      
    [/color][color=Green]'Starts in OFF STATE                        'flash sequence catalogue                    [/color]
[color=Black]flash1:
     [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then
        high C.0[/color][color=Black]: [/color][color=Blue]high C.1        [/color][color=Green]'all on for area illumination    
    [/color][color=Blue]goto [/color][color=Black]flash1 :[/color][color=Blue]endif
    [/color]
[color=Black]flash2:
    [/color][color=Blue]low C.0[/color][color=Black], [/color][color=Blue]C.1
    if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]2 [/color][color=Blue]then  
        pulsout C.0[/color][color=Black], [/color][color=Navy]255[/color][color=Black]: [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255    [/color][color=Green]'simultaneous flash of both LEDs
        [/color][color=Blue]pause [/color][color=Navy]150     
    [/color][color=Blue]goto [/color][color=Black]flash2 :[/color][color=Blue]endif[/color]

[color=Black]flash3:                            [/color][color=Green]'LEDs both pulse 3X, wait 1 sec. - loop
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]3 [/color][color=Blue]then
        high C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]100
        
        [/color][color=Blue]low C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]100
        
        [/color][color=Blue]high C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]100
        
        [/color][color=Blue]low C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]100
        
        [/color][color=Blue]high C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]100
        
        [/color][color=Blue]low C.0[/color][color=Black], [/color][color=Blue]C.1
        pause [/color][color=Navy]600
        
    [/color][color=Blue]goto [/color][color=Black]flash3 :[/color][color=Blue]endif
        [/color]
[color=Black]flash4:                            [/color][color=Green]'LED right pulses 2X then LED left pulses 2X loop
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]4 [/color][color=Blue]then
        pulsout C.0[/color][color=Black], [/color][color=Navy]255[/color][color=Black]:[/color][color=Blue]pause [/color][color=Navy]60[/color][color=Black]: [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255 [/color][color=Blue]pause [/color][color=Navy]400
        
        [/color][color=Blue]pulsout C.1[/color][color=Black], [/color][color=Navy]255[/color][color=Black]:[/color][color=Blue]pause [/color][color=Navy]60[/color][color=Black]: [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255 [/color][color=Blue]pause [/color][color=Navy]400
        
    [/color][color=Blue]goto [/color][color=Black]flash4 :[/color][color=Blue]endif
        [/color]
[color=Black]flash5:                            [/color][color=Green]'LED right pulses 2X and remains high while LED left pulses 2X and remains high -                                 loop
    
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]5 [/color][color=Blue]then
        pulsout C.0[/color][color=Black],[/color][color=Navy]150
        [/color][color=Blue]pause    [/color][color=Navy]80
        [/color][color=Blue]high C.0
        pause [/color][color=Navy]30
        [/color][color=Blue]low C.0
        Pulsout C.1[/color][color=Black],[/color][color=Navy]150
        [/color][color=Blue]pause [/color][color=Navy]80
        [/color][color=Blue]high C.1
        pause [/color][color=Navy]30
        [/color][color=Blue]low C.1
        pause [/color][color=Navy]30
        
    [/color][color=Blue]goto [/color][color=Black]flash5 :[/color][color=Blue]endif[/color]

[color=Black]flash6:                            [/color][color=Green]'LED right pulses 2X pausing while LED left pulses 2X and pauses, loop
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]6 [/color][color=Blue]then
        pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]125
    [/color][color=Blue]goto [/color][color=Black]flash6 :[/color][color=Blue]endif
    [/color]
[color=Black]flash7:                            [/color][color=Green]' wig wag pattern
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]7 [/color][color=Blue]then
        pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
    [/color][color=Blue]goto [/color][color=Black]flash7 :[/color][color=Blue]endif
    [/color]
[color=Black]flash8:                            [/color][color=Green]'alternating wig wag pattern
    [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]= [/color][color=Navy]8 [/color][color=Blue]then
        pulsout C.0[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]high C.0
        pause [/color][color=Navy]170
        [/color][color=Blue]low C.0
        pause [/color][color=Navy]600
        
        [/color][color=Blue]pulsout C.1[/color][color=Black],[/color][color=Navy]255
        [/color][color=Blue]pause [/color][color=Navy]100
        [/color][color=Blue]high C.1
        pause [/color][color=Navy]170
        [/color][color=Blue]low C.1
        pause [/color][color=Navy]600
    [/color][color=Blue]goto [/color][color=Black]flash8 :[/color][color=Blue]endif
goto [/color][color=Black]main[/color]

[color=Blue]Interrupt:                        [/color][color=Green]'used to get an imediate button response
          [/color][color=Blue]do while [/color][color=Purple]pinc.4[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]loop
         inc [/color][color=Purple]b1                [/color][color=Green]'cause the program to select the next flash sequence   
         [/color][color=Blue]setint [/color][color=Navy]%00010000[/color][color=Black],[/color][color=Navy]%00010000    [/color][color=Green]'resets the setint for the next button push   
        [/color][color=Blue]if [/color][color=Purple]b1 [/color][color=DarkCyan]> [/color][color=Navy]8 [/color][color=Blue]then [/color][color=Black]: [/color][color=Purple]b1[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Black]:[/color][color=Blue]endif    
      return[/color]
 
Last edited:

oracacle

Senior Member
the first thing that stands out to me is you call the interrupt and then a goto command, you never return to the interrupt and never return from the interrupt which I would think will cause a stack overflow once you have called however many returns can be stacked into the 08M2. I quick glance through the code and Marks version should resolve that, however do note that it will finish running which ever flash sequence its on.
 

dbeatty

New Member
This gets pretty close to what I had in mind.

Hi dbeatty,
merry Christmas and welcome to the forum.
I didn't look at the video just modded your code abit
I think its what your trying to do hopefully should be enough to get you rolling !


The button responds immediately.

A longer flash sequence will cycle one time before the next one activates.
I must give this some attention.

Otherwise it works a treat.

Thank you for your time.

David
 

mortifyu

New Member
Hi dbeatty,

Looks like Marks nailed it for you.

Only thing I didn't see in there was any form of software debounce for your button.

Perhaps...

Code:
Interrupt:                             'used to get an immediate button response
          do while pinc.4=1 loop
         pause 600                     'Software debounce for potential button noise.
         inc b1                        'cause the program to select the next flash sequence   
         setint %00010000,%00010000    'resets the setint for the next button push   
        if b1 > 8 then : b1=0 :endif    
      return
As you are using setint, without software debouncing it may be susceptible to multiple triggers in the blink of an eye.



Regards,
Morty.
 

dbeatty

New Member
Thanks Morty,

You make a good point. I have some time this morning to work with the program. I'll include the debounce.

Happy New Year

David @ Edmonton, AB
 
Top