Ramdom LED's

im writing some code for a bubble machine

basically its a tube full of water, with either a single or multipul pumps with an airstone attached.

ive got some code which allows me to change led pattern's from the push of a button.

however is there some function i could use to turn LED's on and off randomly ?
at the moment i only have 2x leds, but my plan is to use 10/15 LED's some which will be doubled up. others will be singles.

Regards

Turn them off/on based on the result of random()

random

Use a random number generator say 100 to 1000. Use this as the time in milliseconds to change a LED.

Weedpharma

Hi,
You go from bubbles to pumps to LEDs how are these tied together.
Can you explain your project a little more informatively?

Tom..... :slight_smile:

ive attached the code.

basically there are a set of pre-defined patterns we wish to use on the led’s

but id like another function where when the button is pressed again the led’s turn on and off randomly.

LED_pattern2.ino (2.41 KB)

long randNumber;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
void setup(){
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}
void Pattern1() {
 
  digitalWrite (LED1, HIGH);
  delay (random(3000));
  digitalWrite (LED1, LOW);
  delay (random(3000));
}
void Pattern2() {

  digitalWrite (LED2, HIGH);
  delay (random(3000));
  digitalWrite (LED2, LOW);
  delay (random(3000));
}
void Pattern3() {

  digitalWrite (LED3, HIGH);
  delay (random(3000));
  digitalWrite (LED3, LOW);
  delay (random(3000));
}
void loop() {
  // print a random number from 0 to 299
Pattern1();
Pattern2();
Pattern3();
}

so this is the code im using for the random time for delay()
however id like them to all be random not just as the loop defines them ?

can someone point me into the direction i need to goto ?

Is this what you want to do? something like (untested, and a little pseudocode)?

It could be done more efficiently and more accurately. This code subtracts the delta time from each delay time in the array, every time through the loop. You could store the last times, then check instead, but this is slightly easier code.

const int MAX_DELAY_TIME = 3000; //ms
const int NumberOfLEDS = 10;
const int LEDPins[NumberOfLEDS] = {LED1, LED2, LED3, ...};
long LEDOff[NumberOfLEDS];
long LEDDelays[NumberOfLEDS];
long lastMillisTime;

void loop()
{
   long deltaTime = millis() - lastMillisTime;
   lastMillisTime = millis();
   for (int i=0; i < NumberOfLEDS; i++)
   {
       LEDDelays[i] -= deltaTime;
       if (LEDDelays[i] <= 0)
       {
             LEDOff[i] = !LEDOff[i];   // toggle the stored LED state
             turn_led_on(LEDOff[i]); // and actually toggle the LED
             LEDDelays[i] = random(MAX_DELAY_TIME); // start the new time delay
       }
   }
}

when i use this (after modifying it)

const int MAX_DELAY_TIME = 3000; //ms
const int NumberOfLEDS = 3;
const int LEDPins[NumberOfLEDS] = {13, 12, 11};
long LEDOff[NumberOfLEDS];
long LEDDelays[NumberOfLEDS];
long lastMillisTime;

void loop()
{
   long deltaTime = millis() - lastMillisTime;
   lastMillisTime = millis();
   for (int i=0; i < NumberOfLEDS; i++)
   {
       LEDDelays[i] -= deltaTime;
       if (LEDDelays[i] <= 0)
       {
             LEDOff[i] = !LEDOff[i];   // toggle the stored LED state
             turn_led_on(LEDOff[i]); // and actually toggle the LED
             LEDDelays[i] = random(MAX_DELAY_TIME); // start the new time delay
       }
   }
}
[code]

i get the following error

[code]
sketch_jun25a.ino: In function 'void loop()':
sketch_jun25a.ino:20:35: error: 'turn_led_on' was not declared in this scope
'turn_led_on' was not declared in this scope

yes, that was the pseudocode. You need to set the led at that line.

Something like:

                    digitalWrite(LEDPins[i], LEDOff[i] ? HIGH : LOW);

oh i see, i shall have alook and see what damage i can do lol...

thanks arduinodlb