Random Blink

I want to know how to make an LED blink randomly, not just a random set of numbers i.e. 2949328491841- but random on it's own. How can you do this?

I'm not sure i quite understand your question. Especially the "random on it's own" part.

If what you want is a LED that's blinking for random lengths of time at random intervals you could use the Delay() function both with random "on time" and random "off time" something like:

turn led on
delay("random ms")
turn led off
delay("random ms")

EDIT: if you also want it to "flicker" you could use a random PWM value

Try this -

/*

  • Blink_Randomly
  • Modified from the basic Arduino example. Turns an LED on for a random time,
  • then off for a (most likely) different random time. We use pin 13 because,
  • depending on your Arduino board, it has either a built-in LED
  • or a built-in resistor so that you need only an LED.
  • Original at - http://www.arduino.cc/en/Tutorial/Blink
    */

int ledPin = 13; // LED connected to digital pin 13
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup() // run once, when the sketch starts
{
randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF
}

Hope this helps.

D

if you don't want to use the random function, you'll need an external random generator. i think most linux distros have an entropy device (to produce pseudo random behaviour), you /might/ be able to pipe that back into arduino via serial.

otherwise stick a LDR outside your window and fine tune the sensor to trigger on passing shadows?

i too would like to know why you need this type of randomness...

I should probably point out that randomSeed if initialised with an unused analogue pin will generate fairly random numbers, prolly a damn sight better than most computer implementations...

http://www.arduino.cc/en/Reference/RandomSeed

hi all!

i'm trying to use this code to make a simple random audio "click" generator... i want to plug it directly to a speaker(don't want to use amps here) and i'm getting very low singla out put....

is there any way to put it as loud as when you use the melody exemple code?

edit: it could be simply a note, playing at random, as long as it's allways the same note...

thank you for your time,
manatee

@manatee
I'm not sure if driving a speaker (8[ch937]) directly is a good idea. You may need to drive it through a transistor our use a series resistor. Other's will comment on this.

However to get the loudest possible click if you run the piezo on 2 pins - one out of phase with the other, you get twice the deflection on the "cone" and a louder sound.

I used this technique with a Geiger counter (only with inverting buffers) and it worked quite well. :slight_smile:

your talking about a transistor as a voltage follower?
i didn't want to do that because, i want to keep it as simple as possible. thats the reason i didn't want to build a small amp

and about using 2 pins, if i run exacly the same program on 2 pins and conect them to the same speaker i'll get a louder sound? i'll try that:D

by the way, i want to conect 5 speaker, each making a "click" sound at random times

thanks!

Again, I'll let others handle the topic about driving the speaker (My gut tells me you should be able to get by with a resistor, but a transistor might get you better volume.)

As for the 2 pins - no - not exactly the "same program". If you program your own osc (or perhaps modify the tone lib ?) then when you set 1 pin HIGH, set the other LOW, and visa versa.

i'm not getting any luck...
running 4 digital pins at the same time seems to make the click louder, but something says to me that it's not a great ideia...

i'll make a more elaboratad post outside the "frequently asked questions" forum, maybe more people will see it...if this is allright with the moderators.

thank you again!

There's a very similar post to this here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271878736

Is there a way to do this without using delay?

I found some code to blink without delay, using millis, but I couldn't figure out how to incorporate random into that, without it seeding random every time.

I also couldn't figure out how to make the random blinking toggleable on and off, while using millis.

Ideally, I want an on/off switch, to be able to engage a random blinking, without using delay (I'm using values up to 500ms, and don't want everything else on hold while that's happening).

but I couldn't figure out how to incorporate random into that, without it seeding random every time

Put your random seed in "setup"

I guess I didn't mean the seed, but the actually randomly generated number.

So the first time it produces a 40, so 40ms later, it produces a 521, 521ms later it makes, etc....

Unless I'm misunderstanding how this works overall.

But I figured, using millis to do a random blink, it would generate a random number, then loop through the code until it reaches that number, then do something, and make a new random number.

Decide what you mean by "random" and then let's work from that.

Like turns on/off at random times.

Lights up for 200ms, turns off for 30ms, lights up for 15ms, turns off for 420ms, etc...

Like a random flickering.

And do you want to do this while doing other things or is that it?

If that is it simply put the random number in the delay function otherwise implement a state machine, see blink without delay:-

I saw the blink without delay, but didn't know how to implement a random number refreshing every go around. Nor could I figure out how to turn on/off that system, with the millis part of the code still running.

Other things would be happening at the same time too, which is why I thought it would have to be a millis type system.

This is for 18 LEDs for the MEGA. Just change the number of LEDs defined and the pin numbers to what you are using:-

#define numberOfLEDs 18
long nextFlash[18];
int ledPin[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // LED pins to use.
int ledState[18];

void setup(){
 for(int i = 0; i<numberOfLEDs; i++){
   pinMode(ledPin[i],OUTPUT);
   ledState[i] = LOW;
   digitalWrite(ledPin[i], LOW); // all LEDs off
     nextFlash[i] = millis() +random(100, 1000);
 } 
}

void loop(){
for(int i = 0; i<numberOfLEDs; i++){
if(millis() > nextFlash[i]){
  if(ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
  digitalWrite(ledPin[i],ledState[i]);
  nextFlash[i] = millis()+random(100, 1000) ; // next toggle random time
} }
}
1 Like

Grumpy Mike,

That code is exactly what I am looking for, except one thing. I only want the LED's to stay on for a set time, say 20 ms. Where do I change that?