Random, simultaneous circuit control

I have three circuits I want to run simultaneously that each turn on and off randomly for varying lengths of time, independent of one another. I am using the example described here http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads, multiplied times three, to drive the three circuits using an external power supply to power the circuits. The process seemed obvious enough using the sample code supplied with the circuit by using longRandom(min,max) for the time parameter in the delay(ms) function. I quickly realized that delay does not allow for simultaneous events and, instead, loops through each pin successively.

int transistor2Pin = 2;    // connected to the base of the transistor 1
int transistor9Pin = 9;    // connected to the base of the transistor 2
int transistor10Pin = 10;  // connected to the base of the transistor 3
long randNumber;


void setup() {
  // set  the transistor pin as output:
  pinMode(transistor2Pin, OUTPUT);
  pinMode(transistor9Pin, OUTPUT);
  pinMode(transistor10Pin, OUTPUT);
}

void loop() {
  
  randNumber = random(1000,5000);

  digitalWrite(transistor2Pin, HIGH);
  delay(randNumber);
  digitalWrite(transistor2Pin, LOW);
  delay(randNumber);
  
  digitalWrite(transistor9Pin, HIGH);
  delay(randNumber);
  digitalWrite(transistor9Pin, LOW);
  delay(randNumber);
  
  digitalWrite(transistor10Pin, HIGH);
  delay(randNumber);
  digitalWrite(transistor10Pin, LOW);
  delay(randNumber);
}

Being new to both coding and Arduino, I am at a loss for how to go about this. The MsTimer2 library looks promising, but I am not sure if this is the best option. Most likely there is an obvious solution to my question, but I am not experienced enough to figure it out. Any help or suggestions is greatly appreciated.

First of all, you have to call randomSeed in setup() to initiate the random number generator.

randomSeed(n);

A good input for randomSeed is an unconnected analog pin, this will ensure that the seed number is different each time.

randomSeed(analogRead(0));

Secondly, you are only setting randNumber once, at the beginning of the loop. So every delay is equal until the next iteration of the loop.

Try this,

int getRandNumber() { randNumber = random(1000,5000); return randNumber; }

and just call the function in the code like this

delay(getRandNumber);

alternatively if you dont want to write a function, you could just simply keep re-assigning the value of randNumber after each delay. However I reccomend getting used to writing your own functions, as it makes programs easier to read, debug, and keep organized as they increase in size.

Hope it helps a bit.

CCurti,

If you want all three processes to run “at once”, you might try a little trick like this

const int transistor2Pin = 2;    // connected to the base of the transistor 1
const int transistor9Pin = 9;    // connected to the base of the transistor 2
const int transistor10Pin = 10;  // connected to the base of the transistor 3
 
int pinState[3] = {LOW, LOW, LOW};
int pins[3] = {transistor2Pin, transistor9Pin, transistor10Pin};
int delay_time[3];

void setup()
{
  // set the transistor pins as output:
  pinMode(transistor2Pin, OUTPUT);
  pinMode(transistor9Pin, OUTPUT);
  pinMode(transistor10Pin, OUTPUT);

  // Define initial random delays for each process
  for (int i=0; i<3; ++i)
    delay_time[i] = random(1000, 5000);
}

// We maintain separate delay variables
// for each of the three distinct "processes".
// Each time through the loop, we calculate
// which process is next to change state and
// operate on it.

void loop() 
{
  // Find which of the 3 "processes" is the next to expire
  // (has the smallest delay_time).
  int next_delay_time = delay_time[0];
  if (delay_time[1] < next_delay_time) next_delay_time = delay_time[1];
  if (delay_time[2] < next_delay_time) next_delay_time = delay_time[2];

  delay(next_delay_time);
  for (int i=0; i<3; ++i)
  {
    delay_time[i] -= next_delay_time;
    if (delay_time[i] == 0) // has this process's delay expired?
    {
      pinState[i] = pinState[i] == LOW ? HIGH : LOW; // change pin state
      digitalWrite(pins[i], pinState[i]);
      delay_time[i] = random(1000, 5000); // give this process a new delay
    }
  }
}

This should manage all three simultaneously.

Mikal