Shop OBEX P1 Docs P2 Docs Learn Events
can anyone correct this issue? — Parallax Forums

can anyone correct this issue?

ERMERM Posts: 34
edited 2005-07-13 05:06 in BASIC Stamp
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

Comments

  • Philip GamblinPhilip Gamblin Posts: 202
    edited 2005-07-12 05:29
    Set up a loop in which your button or buttons are read. When a button or the button is pressedd then gosub to the appropriate flasher routine.
  • ERMERM Posts: 34
    edited 2005-07-12 18:53
    Will the stamp read both programs at the same time? i.e. Will it read the sub program and the button loop command at the same time? I thought it could only do one thing at a time like me [noparse]:)[/noparse].
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-12 19:48
    Hey Tony, you asked a question about this project regarding interfacing with the button already, correct? I know it may appear daunting, but a state machine as suggested in the other thread is your best bet. A state machine is a simple control scheme which modifies its internal state based on inputs (your buttons), output variable are defined in terms of internal state. State machines are very useful, so you would benefit from learning about them. I do not have the time right now, but if I am not busy this evening, I will do a little write up showing its operation in pseudo-code.·To give a brief taste here is a couple states of a fictitious machine which changes which LED is flashed based on a button press:

    STATE 0:
    TURN LED1 ON
    PAUSE 50 MS
    IF BUTTON IS PRESSED STATE = 2 ELSE STATE = 1
    ENDSTATE
     
    STATE 1:
    TURN LED1 OFF
    PAUSE 50 MS
    IF BUTTON IS PRESSED STATE = 2 ELSE STATE = 0
    ENDSTATE
     
    STATE 2:
    TURN LED2 ON
    PAUSE 50 MS
    IF BUTTON IS PRESSED STATE = 0 ELSE STATE = 3
    ENDSTATE
     
    STATE 3:
    TURN LED2 OFF
    PAUSE 50 MS
    IF BUTTON IS PRESSED STATE = 0 ELSE STATE = 2
    ENDSTATE
    

    ·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
  • YanroyYanroy Posts: 96
    edited 2005-07-12 19:51
    Here's the way I would do this...· Store an array in EEPROM (I forget the command) that contains a byte of data concerning which pins are high and which are low, and a byte (or more) of time to wait before moving on to the next step.· Then have a main loop that looks like this (pseudocode):

    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
  • ERMERM Posts: 34
    edited 2005-07-12 22:02
    Hey Paul,

    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.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-13 05:06
    To readdress your original question, yes it can be done. Here is an example how:

    quadflash:
    
    FOR reps = 1 TO 4
    high 1
    pause 50
    low 1
    pause 50
    BUTTON 0,1,255,250,BtnWk,1,singletripleflash
    NEXT
    
    FOR reps = 1 TO 4
    high 2
    pause 50
    low 2
    pause 50
    BUTTON 0,1,255,250,BtnWk,1,singletripleflash
    NEXT
    GOTO quadflash
    
     
    singletripleflash:
    EXIT
    ...
    rest of singletripleflash code
    
    

    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
Sign In or Register to comment.