Shop OBEX P1 Docs P2 Docs Learn Events
question about seemingly random control of solenoids with Basic I — Parallax Forums

question about seemingly random control of solenoids with Basic I

Elliot BrambletElliot Bramblet Posts: 1
edited 2006-09-18 18:43 in BASIC Stamp
A student of mine would like to control 13 solenoids in a seemingly random pattern with a basic stamp.· Is it possible to use "random" syntax to·select··I/O pins to control?

Comments

  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-09-18 05:44
    Yes, the entire 16-bit output can be changed at once using OUTS = value.
    If value is a randomized WORD value, the outputs will cycle pseudo-randomly.

    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Personal Links with plenty of BASIC Stamp info

    StampPlot - Graphical Data Acquisition and Control
    AppBee - XBee ZigBee / IEEE 802.15.4 Adapters & Devices
  • mikethomasmikethomas Posts: 6
    edited 2006-09-18 17:03
    Hello, I am the student that this is written about. Since im new to this I have a sharp learning curve. Do you have an example of the OUTS = value syntax. I have been looking at the RANDOM syntax without understanding it very well, and this seems similar to the OUTS syntax. Will the OUTS require a line of code for each pin, or will it affect all of the designated pins (I do want to retain one pin for a PIR to trigger the random firing). On top of that, I would like to possibly ramp the speed up of the random firing and then back down....can that happen??? Im trying to replicate the rain, so variation of speed would be wonderful.
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-18 18:43
    OUTS is just a special variable that effectively is the output register for the 16 I/O pins. RANDOM takes the current value of its variable and changes it in a pseudo-random way to produce a new value. The value is in the range 0-65535. Usually to reduce this 16 bit value to something smaller, you'd use the modulo operator or sometimes a bit-wise and (&) operator. If your solenoids were on pins 0-12, you'd do something like:
      RANDOM seed
      OUTS = (OUTS & $E000) | (seed // $2000)
    
    


    seed would be a word variable that you'd initialize to some value at the beginning of your program. The assignment statement has two parts. One (OUTS & $E000) extracts the current output settings for pins 13-15. This way, they are not affected by changing the whole output register. The other (seed // $2000) does a modulo operation that takes the current value of seed and produces a 13 bit remainder, a value from 0-$1FFF that's effectively random. Each time you execute these two statements, you'd change the pattern of outputs for pins 0-12 in a pseudo-random way. It's pseudo-random in that the values are reproducible given the initial seed value, but the numeric properties of the sequence of values is effectively random.
Sign In or Register to comment.