November 23, 2024, 10:44:43 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Random code for 2 valves?

Started by JackMan, April 15, 2011, 03:31:26 PM

Previous topic - Next topic

JackMan

April 15, 2011, 03:31:26 PM Last Edit: April 15, 2011, 03:33:41 PM by JackMan
Is there a way to do RANDOM (like the example below) for 2 OUTPUTS simultaneously without both of them being HIGH at the same time?



Main:
 timer = 0

Rock_It:
 RANDOM lottery
 delay = PULSE_MAX - PULSE_MIN + 1
 delay = lottery // delay + PULSE_MIN
 TOGGLE Valve1
 PAUSE delay
 timer = timer + delay
 IF timer < ROCK_TIME THEN Rock_It
 LOW Valve1

JonnyMac

Yes, sort of.  You can't control the timing independently but you can do this (from code I wrote for the Casa Fear Zombie):

Thrasher:
  FOR idx = 1 TO 3
    RANDOM lottery
  NEXT
  valves = lottery & %00000011

  IF valves = last THEN Thrasher                ' no repeats
    last = valves                               ' save for next cycle

  PINS = valves
  RANDOM lottery
  delay = lottery // 251 + 100
  POT Adjust, 100, tAdj
  tDelay = delay
  tDelay = tDelay * tAdj / 255
  delay = delay + tDelay
  PAUSE delay
  timer = timer + delay
  IF timer < 10000 THEN Thrasher
    GOTO Reset


Note that we're using two bits from a random value and checking to make sure that they change every cycle.
Jon McPhalen
EFX-TEK Hollywood Office

JackMan

This does do a good job of 2 simultaneous RANDOM outputs but they are both frequently high at the same time. This is what I'm trying to avoid. I could do it using a SPDT relay so if one valve output goes high it will break the circuit to the other but I was hoping to accomplish this with code. Probably asking a bit much for the BS1.

JonnyMac

If you don't want them both on at the same time you could add another conditional test; update the section that ensures the outputs have changed to this:

  valves = lottery & %00000011                  ' isolate bits
  IF valves = %11 THEN Thrasher                 ' only one on at a time
    IF valves = last THEN Thrasher              ' no repeats
      last = valves                             ' save for next cycle
Jon McPhalen
EFX-TEK Hollywood Office


JonnyMac

Quotewithout both of them being HIGH at the same time?

Sorry I missed that requirement the first time -- glad it's working.
Jon McPhalen
EFX-TEK Hollywood Office

JackMan

What was that little trick where the Prop-1 can store the last random seed on power down? I searched but couldn't find it.

JonnyMac

You can use the WRITE command to save it, the READ command to reload it.  That said, do not write the the EEPROM in a tight loop as the code above.  EEPROMs do have write cycle limits and if you put WRITE in a tight loop you can in fact wear out an EEPROM location.

Should this question have been in a new thread?....
Jon McPhalen
EFX-TEK Hollywood Office