Shop OBEX P1 Docs P2 Docs Learn Events
Newbie - How to program a certain sequence — Parallax Forums

Newbie - How to program a certain sequence

tbsmithtbsmith Posts: 3
edited 2005-10-24 18:05 in BASIC Stamp
Hi, I am trying to program a BS1 (or can get a BS2, if necessary) to do the following:

On power up, activate ouptut 1
When button 1 is pressed, turn off ouput 1.
When button 1 is released, turn on output 2
When button 1 is pressed again, turn off ouput 2
When button 1 is released, turn on output 3
...and so on for all the outputs.

When the last output is turned off, go back to the beginning and start over with output 1.

I may need to do this for up to 40 outputs and read somewhere that a shift register might work with the PWM command, to get that many ouputs (or maybe I will wire the ouputs in a matrix fashion) but I need to walk before running, so maybe you could help with the first part at least.

Thanks!
Tom

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2005-10-21 23:33
    That much is fundamental I/O --·but if·"we" write your code then you won't learn anything.·

    How about researching the commands DIRS, OUT, and IN?
  • tbsmithtbsmith Posts: 3
    edited 2005-10-22 02:20
    Thanks for the hints. I think I know how to make a pin an ouput or an input, but I'm not sure how to do the logic to keep track of the input state, and make the necessary output change and do it in sequence. I checked out DIRS, but that doesn't seem to do it. I'm used to ladder logic and this "basic" seems much more complicated with all the commands to choose from. Can you give me another clue where to look?

    Thanks,
    Tom
  • metron9metron9 Posts: 1,100
    edited 2005-10-22 05:09
    i like to set up arrays with bit positions and XOR the byte with the pin register
    The xor "^" will turn on only the bits set in the array byte and turn all others off
    You only have to keep track of the index. You will have to do some more if then logic for the 2 banks of pins on the BS2 1-7 and 8-15


    'Variables
    
    I VAR Byte
    DIRH=%11111111
    
    PINBITS VAR Byte(8)
    PINBITS(1)=1
    PINBITS(2)=2
    PINBITS(3)=4
    PINBITS(4)=8
    PINBITS(5)=16
    PINBITS(6)=32
    PINBITS(7)=64
    PINBITS(8)=128
    
    
    OUTH = (OUTH) ^ (PINBITS(1))    '' Set the first pin on powerup
    i=2
    MAIN:
    
    'IF BUTTON PRESSED
     OUTH = (OUTH) ^ (PINBITS(I))    'Turn off all but one pin
     i=i+1                           'index for next pin
    if i>8 then i=1 'go back to start
     ' wait loop:
     'FOR BUTTON unpress (debouncing etc...)
      OUTH = (OUTH) ^ (PINBITS(I))   'Turn off all but one pin
      i=i+1                           'index for next pin
     if i>8 then i=1   'go back to start
    ' endif
    
    GOTO MAIN
    
    
    
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-10-22 06:00
    Hi Tom,

    You have a BS1? In that case, you have 8 pins total and not as many fancy commands as the BS2. It is still possible. Something like this will move the high output around the circle of 7 pins slowly in the pattern you want, maybe: (Sorry, my BS1 syntax rules are kind of rusty)

    top:
      b0 = %00000010   ' a variable
      PINS = b0     , p1 only high as output at top
      DIRS = %11111110   ' make p0 input for the button, all other are outputs
      
     main:
      PAUSE 1000   ' one second
      b0 = b0 * 2    ' move high bit one left using *2
      PINS = 0   ' all off 
      PAUSE 1000    ' for one second
      IF b0=0 THEN top    ' have to start over at right end
      PINS = b0     ' next high bit
      goto main
    



    To make it happen in response to a button instead of pauses, you need to replace the pauses with something like this:
         wait4buttonDown:   ' first pause
            IF PIN0 = 1 THEN wait4buttonDown   
    
          wait4buttonUp:     ' second pause
            IF PIN0 = 0 THEN wait4buttonUP
    



    Expansion to more outputs with shift registers will be more work, but the logic should be about the same.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tbsmithtbsmith Posts: 3
    edited 2005-10-24 18:05
    Metron9 and Tracy, thanks for the suggestions! I'll have to study some of the commands and think about how this would work, but I'll try them out, and report back how it works. These are ideas I never would have thought of, so thanks a bunch for getting me started!

    Tom
Sign In or Register to comment.