can anyone correct this issue?
ERM
Posts: 34
I am trying to create an electonic flasher using the BS2 to flash a pair (or pairs) of led's. I have coded several flash patterns using high, low and pause commands. For repeating cycles, I have used FOR and NEXT commands. I have included a sample code below. (Don't worry about the outputs, I will be using MOSFets)
'{$STAMP BS2}
reps VAR NIB
BtnWk VAR BYTE
singleflash:
high 1
pause 50
low 1
pause 50
high 2
pause 50
low 2
pause 50
BUTTON 0,1,255,250,BtnWk,0,singleflash
GOTO doubleflash
doubleflash:
FOR reps = 1 TO 2
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 2
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,doubleflash
GOTO tripleflash
tripleflash:
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,tripleflash
GOTO quadflash
quadflash:
FOR reps = 1 TO 4
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 4
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,quadflash
GOTO singletripleflash
singletripleflash:
high 1
pause 50
low 1
pause 50
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
high 2
pause 50
low 2
pause 50
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,singletripleflash
GOTO singleflash
What I am trying to do is program this into the eeprom. Each flash pattern will have it's own memory slot. I want to write a code that will say, when the "mode" button (flash pattern change button) is pressed, then switch to the next flash pattern. Right now the way this code is written, it will only recognize the button being pressed at the end of the flash pattern. For the last flash pattern above, singletripleflash, that could take a couple of seconds. I am looking for the flash pattern to change when the button is pressed regardless of what part of the flash sequence it's on. Can this be done?
Thanks,
Tony
'{$STAMP BS2}
reps VAR NIB
BtnWk VAR BYTE
singleflash:
high 1
pause 50
low 1
pause 50
high 2
pause 50
low 2
pause 50
BUTTON 0,1,255,250,BtnWk,0,singleflash
GOTO doubleflash
doubleflash:
FOR reps = 1 TO 2
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 2
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,doubleflash
GOTO tripleflash
tripleflash:
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,tripleflash
GOTO quadflash
quadflash:
FOR reps = 1 TO 4
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 4
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,quadflash
GOTO singletripleflash
singletripleflash:
high 1
pause 50
low 1
pause 50
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
high 2
pause 50
low 2
pause 50
FOR reps = 1 TO 3
high 1
pause 50
low 1
pause 50
NEXT
FOR reps = 1 TO 3
high 2
pause 50
low 2
pause 50
NEXT
BUTTON 0,1,255,250,BtnWk,0,singletripleflash
GOTO singleflash
What I am trying to do is program this into the eeprom. Each flash pattern will have it's own memory slot. I want to write a code that will say, when the "mode" button (flash pattern change button) is pressed, then switch to the next flash pattern. Right now the way this code is written, it will only recognize the button being pressed at the end of the flash pattern. For the last flash pattern above, singletripleflash, that could take a couple of seconds. I am looking for the flash pattern to change when the button is pressed regardless of what part of the flash sequence it's on. Can this be done?
Thanks,
Tony
Comments
·This is written in psuedocode, but it easily translates into·PBASIC code. The state machine starts in state 0 and alternates between states 0 and 1 until the button is pressed, then it will alternate between states 2 and 3 until the next button press. You can set the outputs to whatever you desire for a particular state (like you can have the LEDS alternate blinking by stating in STATE 0: TURN LED1 ON, TURN LED2 OFF and in STATE 1: TURN LED1 ON, TURN LED2 OFF), so you can mimic each of your code sections by creating state(s) which replicate your desired behavior.·And as you can see the button is checked every ~50 ms (this will cause it to flip between 0,1 and 2,3 until you release the button, this can be made to only switch once per button press by rembering the button's value from the last state and only switching over when the buttons previous value is not equal to the current value, and the current value is that the button is pressed (1).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Everyone can see the beauty in a rose, not everyone can see the beauty in a cockroach.
Post Edited (Paul Baker) : 7/12/2005 7:56:19 PM GMT
main:
if input = pressed then gosub DoSomething
outputs = leds(n)
if counter < time(n) then
counter = counter + 1
else
counter = 0
n = n + 1
endif
pause 1 ' this isn't really necessary, but will make the units of the time() variable in approximately milliseconds
goto main
By doing it this way, the stamp can check to see if the button has been pressed while it's working its way through the flashing pattern.· It's also less work for you, since the patterns are stored as data instead of as code.· If you're feeling really clever, you can make an additional instruction that's stored in memory that allows you to jump to another place in the array (probably incrementing or decrementing a counter in the process) so that you can do repetitions of sequences.
Post Edited (Yanroy) : 7/12/2005 7:54:08 PM GMT
The state machine was for the multiple switch idea I had. Unfortunately, I realized that my code was written defining pins 1 + 2. In order to do what I wanted earlier, I would have to save to the EEPROM three sets of codes in respect to the outputs pins (One for pins 1+2, one for pins 4+5 and one for pins 7+8. Pins 0, 3 + 6 would have been the ipnut from the switch pins.) Too much code to be saving to the EEPROM, especially since I wanted to have multiple flash patterns.
In what I was originally trying to do, I want to save each flash pattern (single flash, double flash, etc.) into its own bank on the EEPROM. I want to connect pin 0 to the button and have it recognize when the button is pressed to change from it's current selected pattern to advance to the next pattern. If possible, I would also like to tell it that if the button is held down for more than 3 seconds to goto STOP. It's not crutial though. I just thought it would be cool.
Thanks,
Tony.
By changing the BUTTON TargetState, it is used as an "ESCAPE" command. The EXIT is to properly exit the FOR..NEXT loop·it jumped from. The button is checked every inner loop rather than at the end of the outerloop,·so each flash pattern will have close to the same responsiveness to the button.
Post Edited (Paul Baker) : 7/13/2005 5:14:42 AM GMT