Shop OBEX P1 Docs P2 Docs Learn Events
Random output pin generator — Parallax Forums

Random output pin generator

I'm using a BS2 with PBasic. I would like a super small bit of code that will allow a single input pin to run a random out put to one of nine pins. Each single input would turn on a different out pin each time. I'm trying to make an electronic wind chime. Any ideas would be much appreciated.

Comments

  • tomcrawfordtomcrawford Posts: 1,129
    edited 2017-07-30 22:44
    maybe something like this to get you started
    Trigger  PIN   I/ONumber of "single Input Pin"
    Chimes  CON    I/ONumber of first of nine contiguous output pins
    
    RNum    VAR    Word
    Temp     VAR    Word
    
    top:
    
    do while Trigger = 0     'wait for the single input
    Loop
    
    Random RNum            'choose an output
    Temp = RNum // 9          'zero through eight
    Pulse Chimes+Temp, period    'send a pulse to one of the chimes
    goto top
    

    Edit: Should have said PULSOUT, rather than Pulse.

    Be aware: You likely cannot directly drive the chime plungers from BS-2 pins. You will likely need at least a buffer of some sort or even a relay. EndEdit
  • Thanks I will give it a try. I'm driving opto-isolators to dive LED strips.
  • ercoerco Posts: 20,257
    Two of my favorite BS2 multi-pin rhythmic videos from the same guy, for inspiration. Not random by any means!



  • Fun stuff!! reminds me of my elevator guy days!!
  • Tom, here is my run at your random code.
    trigger PIN 1 ' push button or other.
    chimes CON 7 ' does output to pins 7 through 15 based on divider.
    led VAR Word ' chimes here will produce an error, symbol already defined.
    Temp VAR Word
    start:
    DO WHILE trigger = 0
    LOOP
    RANDOM led
    Temp = led // 9
    PULSOUT chimes+Temp, 2000 ' A period number must be here, it regulates brightness, signal strength.
    PAUSE 30 ' This gives a time delay between pin "on", also delays multiple pin "on" for short inputs.
    GOTO start

    ' Random is relative, but really good enough.
    Now all I need to do is turn this output into a gosub bit of code. In other words random output starts a random bit of code from a list.
    Thanks.
  • :thumb:

    Anyone who attempts to generate random numbers...is living in a state of sin. von Neumann

Sign In or Register to comment.