Random Blinking LED Coding

I'm using a modified code of the starter project, Spaceship Interface. With one additional red LED. The setup is the same. The objective was to get the red LEDs to randomly blink when pressing the button. I tried using random() to get the LEDs to randomise but I'm unsure as to how the coding works.My code is as shown below.

int switchstate = 0;

void setup(){
// declare the LED pins as outputs
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);

Serial.begin(9600);

randomSeed(analogRead(0));

// declare the switch pin as an input
pinMode(2,INPUT);
}

void loop()
{

randNumber=random(300);

}
{

// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);

// if the button is not pressed
// blink the red LEDs
if (switchstate == LOW) {
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, LOW); // turn the red LED on pin 5 off
digitalWrite(6, LOW);
}
// this else is part of the above if() statement.
// if the switch is not LOW (the button is pressed)
// the code below will run
else {
digitalWrite(3, LOW); // turn the green LED on pin 3 off
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
digitalWrite(6, randNumber);
// wait for a quarter second before changing the light
delay(250);
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
digitalWrite(5, LOW); // turn the red LED on pin 5 off
digitalWrite(6, randNumber);
// wait for a quarter second before changing the light
delay(250);
}
}

why not make it completely random, when you push the button, and get rid of this dodgy delay()s too...

use arrays...
use blinkWithoutDelay...
use a function...

take a look and see if you like it

const int buttonPin = 2;
const int ledPin [4] = { 3,4,5,6 } ;
unsigned long startTime [4] = { 0,0,0,0 };
unsigned long duration [4] ;
//
void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  for (int i = 0; i < 4; i++)
  {
    pinMode(i,OUTPUT);
  }
  randomSeed(analogRead(0));
  for (int i = 0; i < 4; i++)
  {
    duration [i] = random(50, 400);
  }   
  pinMode(2,INPUT);
}
//
void loop()
{
  if (digitalRead(buttonPin) == LOW)
  {
    for (int i = 0; i < 4; i++)
    {
      randomBlink(i);
    }
  }
  else 
  {
    for (int i = 0; i < 4; i++)
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
}
void randomBlink(int pin)
{
  if (millis() - startTime[pin] >= duration[pin])
  {
    digitalWrite(ledPin[pin], !digitalRead(ledPin[pin]));
    duration[pin] = random(50, 750);  //smallest and longest ON/OFF times.
    startTime[pin] = millis();
  }
}

Your code makes no sense. I think you are in way over your head, and are just slapping together bits and pieces of code without understanding what they do.

You set 4 LED pins to specific states until a button is pressed.

Once the button is pressed, you invert some of the pins, keep others the same, and set pin 6 to a random value from 0-300. (You should use 0 or 1 in a digitalWrite command). Then you delay for 1/4 second, invert pins 4 and 5, set pin 6 to the SAME random value, delay for another 1/4 second, and repeat that loop as long as the button is pressed.

What about that code is supposed to create random blink sequences?

Writing random bits of code without understanding what it's doing is not going to get you very far.

Walk us through what you think each step in your code is supposed to be doing. Think like a computer. Then run your program and see how the results compare with what you expected. If the results are different from your expectations then go back and study the code again to try to figure out why it's not doing what you expected.

But you need to be able to articulate step by step what you are asking the program to do.

whiteknight747:
I'm using a modified code of the starter project, Spaceship Interface. With one additional red LED. The setup is the same. The objective was to get the red LEDs to randomly blink when pressing the button. I tried using random() to get the LEDs to randomise but I'm unsure as to how the coding works.My code is as shown below.

int switchstate = 0;

void setup(){
// declare the LED pins as outputs
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);

Serial.begin(9600);

randomSeed(analogRead(0));

// declare the switch pin as an input
pinMode(2,INPUT);
}

void loop()
{

randNumber=random(300);

}
{

// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);

// if the button is not pressed
// blink the red LEDs
if (switchstate == LOW) {
digitalWrite(3, HIGH); // turn the green LED on pin 3 on
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, LOW); // turn the red LED on pin 5 off
digitalWrite(6, LOW);
}
// this else is part of the above if() statement.
// if the switch is not LOW (the button is pressed)
// the code below will run
else {
digitalWrite(3, LOW); // turn the green LED on pin 3 off
digitalWrite(4, LOW); // turn the red LED on pin 4 off
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
digitalWrite(6, randNumber);
// wait for a quarter second before changing the light
delay(250);
digitalWrite(4, HIGH); // turn the red LED on pin 4 on
digitalWrite(5, LOW); // turn the red LED on pin 5 off
digitalWrite(6, randNumber);
// wait for a quarter second before changing the light
delay(250);
}
}

@BulldogLowell Tried using your code,it made the green LED blink,whereas the red LEDs did not light up at all regardless of button press. :~

@DuncanC I'm not really sure what random() does....I basically used the example code and mixed in code from the random() example in the reference.

The main idea was to use the current setup "Spaceship Interface default setup". Add 1 extra red LED light and make the 3 red LED lights blink randomly when the push buttton was pressed.

whiteknight747:
I'm not really sure what random() does....

All of the commands are explained here.

How do I declare the variable for random in the setup?

did you look at the example I gave you....?

the function and the assignment are here:

duration[pin] = random(50, 750);  //smallest and longest ON/OFF times.

whiteknight747:
How do I declare the variable for random in the setup?

Just like any other declaration. But you probably don't want to declare it inside setup(), since that means it will only be known about inside setup() but you probably want to use it inside loop().

If you declare it above setup, it's known everywhere in the program.

See this example, where it's declared right at the top.

So I input the pin numbers where it says pin? I just started using Arduino :confused:

I think I get where to use random now, but I'm unsure as to how to get the LEDs to randomise.

I'm unsure as to how to get the LEDs to randomise.

The first thing to do is to sit down and define exactly what you mean by "LEDs blinking randomly".

Here are some of the many possibilities:
Do you want each LED to blink for a short time, with random waits in between blinks?
Do you want each LED to have randomly chosen on times and off times?
Do you want the order in which the LEDs blink (singly?) to be random?

Once you have decided what you want, then you figure out how to write the program to do that.

it may be better to think about it this way:

basics....

int ledPin = 13;
int myRandomNumber;

void setup()
{
  pinMode(ledPin, OUTPUT);
  randomSeed(analogRead(A0));
}

void loop()
{
  digitalWrite(ledPin, !digitalRead(ledPin));
  myRandomNumber = random(100,3000);
  delay(myRandomNumber);
}

digitalWrite(ledPin, !digitalRead(ledPin));

The use of ! might be "basics" to you BL, but I'll bet it's not "basics" to the OP.....

I want each LED to have randomly chosen on times and off times.

JimboZA:

digitalWrite(ledPin, !digitalRead(ledPin));

The use of ! might be "basics" to you BL, but I'll bet it's not "basics" to the OP.....

yeah... good point.

digitalWrite(ledPin, !digitalRead(ledPin));

means... read the value of ledPin :

 digitalRead(ledPin)

and take the opposite of that (! or NOT) :

!digitalRead(ledPin)

and assign that value to ledPin

digitalWrite(ledPin, !digitalRead(ledPin));

or simply stated, set ledPin HIGH when it returns LOW or LOW when it returns HIGH...

or just make ledPin its opposite state.

whiteknight747:
I want each LED to have randomly chosen on times and off times.

did you try the sample code I wrote for you?

Yeah, but its only blinking the internal LED, is there any way to change it to pins 4,5 and 6?

no... this:

it will blink on 3, 4, 5 & 6

const int buttonPin = 2;
const int ledPin [4] = { 3,4,5,6 } ;//<<<<<<<<<<<<<<<<<your led pins
unsigned long startTime [4] = { 0,0,0,0 };
unsigned long duration [4] ;
//
void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  for (int i = 0; i < 4; i++)
  {
    pinMode(i,OUTPUT);
  }
  randomSeed(analogRead(0));
  for (int i = 0; i < 4; i++)
  {
    duration [i] = random(50, 400);
  }   
  pinMode(2,INPUT);
}
//
void loop()
{
  if (digitalRead(buttonPin) == LOW)
  {
    for (int i = 0; i < 4; i++)
    {
      randomBlink(i);
    }
  }
  else 
  {
    for (int i = 0; i < 4; i++)
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
}
void randomBlink(int pin)
{
  if (millis() - startTime[pin] >= duration[pin])
  {
    digitalWrite(ledPin[pin], !digitalRead(ledPin[pin]));
    duration[pin] = random(50, 750);  //smallest and longest ON/OFF times.
    startTime[pin] = millis();
  }
}

Hmm...for some reason only the green LED attached to pin 3 is lighted up, it acts as if the button is not there. When I open the serial monitor I don't see the button press registering.