Shop OBEX P1 Docs P2 Docs Learn Events
Need help with code, Is this right? What am i doing wrong? — Parallax Forums

Need help with code, Is this right? What am i doing wrong?

motosk8er2motosk8er2 Posts: 6
edited 2011-07-16 01:18 in BASIC Stamp
This is for a basic stamp 2 homework board. It is a project for homemade emergency lights, with different patterns and adjustable flash rate. pin 0 has the switch on it, changes modes when high, pins 1-8 are LED outs, they will go to transistors, which will turn on and off 8 3w LED drivers, which will go to the actual LED's. 4 red and 4 blue, and pin 15 is the input for the RCTIME POT circuit to control flash rate. I need a lot more flahs patterns if anyone has any ideas, and also im having problems. When i press the button it will not change pattern until the full pattern cycles, also same with the pot, when i adjust the speed it doesnt actually change until the pattern fully cycles. Here is the code i have so far.
' {$STAMP BS2}
' {$PBASIC 2.5}
time        VAR         Word
 
DO            'Wig wag
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
HIGH 2
HIGH 3
HIGH 4
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 4
PAUSE time
HIGH 5
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 5
LOW 6
LOW 7
LOW 8
PAUSE time
LOOP UNTIL IN0=1
 
DO         'left3x-right3x
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
HIGH 2
HIGH 3
HIGH 4
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 4
PAUSE time
HIGH 1
HIGH 2
HIGH 3
HIGH 4
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 4
PAUSE time
HIGH 1
HIGH 2
HIGH 3
HIGH 4
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 4
PAUSE time
HIGH 5
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 5
LOW 6
LOW 7
LOW 8
PAUSE time
HIGH 5
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 5
LOW 6
LOW 7
LOW 8
PAUSE time
HIGH 5
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 5
LOW 6
LOW 7
LOW 8
PAUSE time
LOOP UNTIL IN0=1
 
 

DO        'Inner, outer, flash
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
HIGH 2
HIGH 3
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 6
LOW 7
LOW 8
PAUSE time
HIGH 1
HIGH 2
HIGH 3
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 6
LOW 7
LOW 8
PAUSE time
HIGH 1
HIGH 2
HIGH 3
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 6
LOW 7
LOW 8
PAUSE time
HIGH 4
HIGH 5
PAUSE time
LOW 4
LOW 5
PAUSE time
HIGH 4
HIGH 5
PAUSE time
LOW 4
LOW 5
PAUSE time
HIGH 4
HIGH 5
PAUSE time
LOW 4
LOW 5
PAUSE time
LOOP UNTIL IN0=1
DO         'shifting
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
HIGH 3
HIGH 5
HIGH 7
PAUSE time
LOW 1
LOW 3
LOW 5
LOW 7
PAUSE time
HIGH 2
HIGH 4
HIGH 6
HIGH 8
PAUSE time
LOW 2
LOW 4
LOW 6
LOW 8
PAUSE time
LOOP UNTIL IN0=1
DO        '4-3-2-1
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 7
HIGH 8
PAUSE time
LOW 7
LOW 8
PAUSE time
HIGH 7
HIGH 8
PAUSE time
LOW 7
LOW 8
PAUSE time
HIGH 3
HIGH 4
PAUSE time
LOW 3
LOW 4
PAUSE time
HIGH 3
HIGH 4
PAUSE time
LOW 3
LOW 4
PAUSE time
HIGH 5
HIGH 6
PAUSE time
LOW 5
LOW 6
PAUSE time
HIGH 5
HIGH 6
PAUSE time
LOW 5
LOW 6
PAUSE time
HIGH 1
HIGH 2
PAUSE time
LOW 1
LOW 2
PAUSE time
HIGH 1
HIGH 2
PAUSE time
LOW 1
LOW 2
LOOP UNTIL IN0=1
RETURN
 

DO            'knightrider
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
PAUSE time
LOW 1
PAUSE time
HIGH 2
PAUSE time
LOW 2
PAUSE time
HIGH 3
PAUSE time
LOW 3
PAUSE time
HIGH 4
PAUSE time
LOW 4
PAUSE time
HIGH 5
PAUSE time
LOW 5
PAUSE time
HIGH 6
PAUSE time
LOW 6
PAUSE time
HIGH 7
PAUSE time
LOW 7
PAUSE time
HIGH 8
PAUSE time
LOW 8
PAUSE time
HIGH 7
PAUSE time
LOW 7
PAUSE time
HIGH 6
PAUSE time
LOW 6
PAUSE time
HIGH 5
PAUSE time
LOW 5
PAUSE time
HIGH 4
PAUSE time
LOW 4
PAUSE time
HIGH 3
PAUSE time
LOW 3
PAUSE time
HIGH 2
PAUSE time
LOW 2
PAUSE time
LOOP UNTIL IN0=1

DO            'Flash all
HIGH 15
PAUSE 10
RCTIME 15, 1, time
time = time */ 185                                ' scale by 0.724 (x 256 for */).
time = time + 50                                 ' Offset by 50.
HIGH 1
HIGH 2
HIGH 3
HIGH 4
HIGH 5
HIGH 6
HIGH 7
HIGH 8
PAUSE time
LOW 1
LOW 2
LOW 3
LOW 4
LOW 5
LOW 6
LOW 7
LOW 8
PAUSE time
LOOP UNTIL IN0=1

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2011-07-10 17:29
    Your program can only do one thing at a time; that's common with most microcontrollers.

    In your program, a lot of time is spent in PAUSE and when the processor is doing that, it can't do anything else.

    There are numerous techniques to program around the issue. One is to use an external clock which the stamp can check to see how long its been doing a particular thing. When not checking the clock, it can do other things like watch the RCTIME loop or watch for another input.

    A second method is to use a subroutine to be a short pause, maybe PAUSE 10 or PAUSE 15. Your main program will keep a count of how many times the subroutine is called and add up the time until its time to do something else. Also, every time the program comes back from the subroutine, you can check for a switch or RCTIME or whatever else must be updated while the rest of the program runs.

    Keep at it.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-07-11 09:39
    One way to condense the code is to put the states of the lamps DATA statements, something like:
    wigwag DATA 4, %00001111, %00000000, %11110000, %00000000
    
    Then make a "player" subroutine that is entered with a pointer to the sequence to play, wigwag in this case, where it finds the number of elements and then the patterns to transfer in sequence to the outputs using READ point, outL commands. If the player has to be more responsive to the input pin, then check it between each element of the pattern. And for faster response for timing, the execution time of the RCTIME command could be made much faster (lower capacitor value, time constant in a fraction of a millisecond), so with scaling it could fit in between each step of the sequence too.
  • motosk8er2motosk8er2 Posts: 6
    edited 2011-07-12 20:37
    One way to condense the code is to put the states of the lamps DATA statements, something like:
    wigwag DATA 4, %00001111, %00000000, %11110000, %00000000
    
    Then make a "player" subroutine that is entered with a pointer to the sequence to play, wigwag in this case, where it finds the number of elements and then the patterns to transfer in sequence to the outputs using READ point, outL commands. If the player has to be more responsive to the input pin, then check it between each element of the pattern. And for faster response for timing, the execution time of the RCTIME command could be made much faster (lower capacitor value, time constant in a fraction of a millisecond), so with scaling it could fit in between each step of the sequence too.

    Thanks a lot, that helps but i dont quite understand how the binary code works and how to write it to make each pattern work. Ive tried before but couldnt quite get it.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-07-13 10:26
    I haven't actually run the following to check it, but I hope it will help. It is convenient to keep all the LEDs on p0 to p7, so I "moved" your input pushbutton up to p8. The pushbutton needs a pulldown resistor, right? The program makes p0 to p7 outputs using the DIRL=$FF command. Then it points to the first sequence and READs out patterns of bits that are passed to the LEDs you have on OUTL. All that is required for the next sequence is another DATA set, here named "wumpus", and the same player subroutine.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' plays sequences to LEDs on p0 to p7,
    ' pushbutton on p8 to move from pattern to pattern
    
    pntr VAR WORD
    idx  VAR BYTE
    N    VAR BYTE
    
    demo:
      DIRL = $FF    ' make p0 to p7 outputs to LEDs
      pntr=wigwag   ' choose a sequence
      GOSUB player    ' play it
      DO : LOOP UNTIL IN8
      pntr=wumpus   ' next sequence
      GOSUB player
      DO : LOOP UNTIL IN8
      GOTO demo    ' or play more sequences!
      
    
    player:
      ' enter with pntr to the DATA sequence to play
      READ pntr, N   ' each sequence starts with the number of states to be played
      FOR idx=1 TO N
        READ pntr+idx,OUTL
        PAUSE 1000    ' RCTIIME delay would go here
        IF IN8 THEN EXIT  ' if user presses button, time to move on
      NEXT
      RETURN
    
    wigwag DATA 4, %00001111, %00000000, %11110000, %00000000
    wumpus DATA 6,%10001000,%00010001,%01100110,%01000010,%00100100,%10011001
    
  • ercoerco Posts: 20,256
    edited 2011-07-13 14:46
    @motosk8er2: Your lucky day! Tracy Allen is a SERIOUS guru here in the forums, and he's throwing you gold for FREE!

    As I promised in your other thread, you won't find a more helpful group of people than in this forum. They're smart.

    Some of us are just smart alecs. ;)
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-07-13 17:31
    Seriously erco, thanks! if it be gold, it is freely given, just as I learned it from somebody else, maybe here...
  • motosk8er2motosk8er2 Posts: 6
    edited 2011-07-16 01:18
    Wow tracy, thank you so much. With your help i managed to get the binary, and all other commands figured out. This took me to a whole new level. I really do appreciate it. This is a great forum, and full of smart people, and parallax has been nothing but amazing.
Sign In or Register to comment.