Need help with random functions on sensor input

Hello everyone. I am just learning all things arduino and coding. I have very little experience and this is my first project. Here is what I would like to accomplish.

I want to simulate 4 camera flashes with lights triggered by 3 different motion sensors.

When motion is detected by any of the sensors, I want 4 lights to flash for 20 ms with random off times between 20-2500 ms. The lights must flash independently of each other. I have the hardware part of this figured out with my relays and power to the lights, however it is the code I am stumped on. Each light is triggered by a 5v signal from the arduino(a HIGH signal).

I can make an led light on motion. I can make an led flash randomly. I can make several led's flash randomly without using the delay function. What I can't figure out is how to make them flash for 20 ms only, with random off times, separately, on motion.

Any clues?

What does your code look like?

If you post it, it should be simple to fix.

This is what I am using to make the led’s flash randomly, independently. I do not know how to set the ON time to a set time.

const int led10Pin = 10;    // led 1
const int led11Pin = 11;    // led 2
const int led12Pin = 12;  // led 3

int pinState[3] = {LOW, LOW, LOW};
int pins[3] = {led10Pin, led11Pin, led12Pin};
int delay_time[3];

void setup()
{
  // set the led pins as output:
  pinMode(led10Pin, OUTPUT);
  pinMode(led11Pin, OUTPUT);
  pinMode(led12Pin, OUTPUT);

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

// 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(20, 1000); // give this process a new delay
    }
  }
}

Someone else posted this code and I have modified it slightly.

Once I get this part, then I will work on triggering it on motion.

I’m doing it a piece at a time, is that a bad way to go about it? It’s helping me learn the functions separately instead of all at once.

I'm doing it a piece at a time, is that a bad way to go about it? It's helping me learn the functions separately instead of all at once.

There maybe people that don't do it that way, but I sure do. It also applies to the hardware components one my add to their Arduino. I tend to build a subset of the external circuit, write a little 'test' software to test out the circuit and then move on to the next portion. It does leave a later step to combine all the software functions together but by then I have a clearer picture of what I want to do and what it will take to do it.

Of course this method works best if you are doing the project for yourself with no 'boss' looking over your shoulder offering help. ;)

Good luck

Lefty

I do not know how to set the ON time to a set time.

You know when you're turning the LED ON, so simply set a fixed time there, rather than a random one.

(untested):

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