Picking one LED randomly using 74HC595 register

Hi everyone,
I'm new in the arduino world.
I'm starting a project. The idea is to make 6 Leds blinking randomly (74HC595 outputs). Once you Push a button only one led should be ON.
I tried break function but it only turn off all leds.
Some one can help me for the code?
Thanks in advance
Here below a partial part of the code:

void updateShiftRegisterR()
 {
 digitalWrite(latchPin_2, LOW);
 shiftOut(dataPin_2, clockPin_2, LSBFIRST, leds);
 digitalWrite(latchPin_2, HIGH);
 }

void loop()
{
 leds =0;
 updateShiftRegisterR();
 delay(100);

 
   for (int i = 0; i < 8; i++)
    {
     bitSet(leds, i);
     updateShiftRegisterR();
     delay(100);
       if (digitalRead(start)==HIGH)
       {break;
     
       }
    }

Picture of wiring:

Welcome to the forum

Please post your full sketch

What about

int interval = 100; //somewhere above
void loop() {
  int ledNum;
  currentMillis = millis();
  if (currentMillis - lastMillis >= interval) {
     ledNum = random(6);
     leds = 1 << ledNum;
     updateShiftRegisterR();
     lastMillis = currentMillis;
  }
}

You will have to add the button logic

Hi UKHeliBob
Thanks.

#include <SD.h>
#include <SPI.h>

//Boutons Choix du verbe conjugé
const int r1 = A5;
const int r2 = A4;
const int r3 = A3;
const int r4 = A2;
const int r5 = A1;
const int r6 = A0;

//Bouton de choix du Pronom personel
const int start = 0;
//const byte ledpins[6] = {0B10000000, 0B01000010, 0B00100100, 0B00010000, 0B00001000, 0B00000100};

// Connexion arduino avec 74HC595:
///Pin connected to DS of 74HC595
int dataPin = 1;
///Pin connected to ST_CP of 74HC595
int latchPin = 2;
///Pin connected to SH_CP of 74HC595
int clockPin = 3;

// Connexion arduino avec 74HC595 (2):
///Pin connected to DS of 74HC595 (2)
int dataPin_2 = 4;
///Pin connected to ST_CP of 74HC595 (2)
int latchPin_2 = 5;
///Pin connected to SH_CP of 74HC595 (2)
int clockPin_2 = 6;

byte leds = 0;

void setup()
{
  pinMode(start, INPUT);
  //
  pinMode(r1, INPUT_PULLUP);
  pinMode(r2, INPUT_PULLUP);
  pinMode(r3, INPUT_PULLUP);
  pinMode(r4, INPUT_PULLUP);
  pinMode(r5, INPUT_PULLUP);
  pinMode(r6, INPUT_PULLUP);

  //set pins to output so you can control the shift register 74HC595
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  //set pins to output so you can control the shift register 74HC595 (2)
  pinMode(latchPin_2, OUTPUT);
  pinMode(clockPin_2, OUTPUT);
  pinMode(dataPin_2, OUTPUT);

}

 void updateShiftRegisterR()
 {
 digitalWrite(latchPin_2, LOW);
 shiftOut(dataPin_2, clockPin_2, LSBFIRST, leds);
 digitalWrite(latchPin_2, HIGH);
 }

void loop()
{
 leds =0;
 updateShiftRegisterR();
 delay(100);

 
   for (int i = 0; i < 8; i++)
    {
     bitSet(leds, i);
     updateShiftRegisterR();
     delay(100);
       if (digitalRead(start)==HIGH)
       {break;
     
       }
    }

Hello @hosninfogate ,

is your sketch and the wiring available on Wokwi? The picture from your first post looks like that ...

In case that yes, would you mind to share the link to that Wokwi simulation? That would make support much easier ...

1 Like

Hi ec2021
Here we go: --> Random light - Wokwi ESP32, STM32, Arduino Simulator

Thanks.

something like this?
(Compiles, NOT tested!)

uint8_t latchPin = 5;      // Latch pin of 74HC595 is connected to Digital pin 5
uint8_t clockPin = 6;      // Clock pin of 74HC595 is connected to Digital pin 6
uint8_t dataPin = 4;       // Data pin of 74HC595 is connected to Digital pin 4

uint8_t leds = 0;         // Variable to hold the pattern of which LEDs are currently turned on or off
uint8_t button_LED = 7;    // pin to which button is connected
uint8_t stop_state = LOW;

void setup()
{
  // Set all the pins of 74HC595 as OUTPUT
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(button_LED, INPUT_PULLUP);
  leds = 0;        // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
  updateShiftRegister();
  delay(500);
}

/*
   loop() - this function runs over and over again
*/
void loop()
{

  if (stop_state == LOW && digitalRead(button_LED) == HIGH) {
    leds = 0;
    bitSet(leds, random(0, 6));         // Set the bit that controls that LED in the variable 'leds'
    updateShiftRegister();
    delay(100);
  }
  else {
    stop_state = HIGH;
  }


}

/*
   updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function 'shiftOut' to shift out contents of variable 'leds' in the shift register before putting the 'latchPin' high again.
*/
void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

hope that helps....

The first thing that comes to my mind is that you are using pins 0 and 1 of the UNO which are usually reserved for Serial communication while pins 7, 8 and 9 are still free ...

I would change this because you restrict the use of Serial without need and Serial output may be of assistance during the development.

@ec2021

OK thanks. Understood.

And now regarding my issue do you have any ideas?

Thanks again.

Takes some time to follow the wiring and Wokwi throws some unexpected errors ...

:wink:

@ec2021

Perfect.

regarding the pin 7 , 8 and 9 i will need them for presence detection sensors wiring.

Nevertheless i can use another register and get rid of pin0 and 1

Also for the wired SD module still working on it...

HI,
Thanks.

Didn't work.
Nothing is happening.

@mancera1979,

Thanks for the code but i can not really follow you since i'm not understanding the logic behind.
some comments may be ?

Thanks again.

So here is a quick shot:

  • "start" changed to pin 7, but is not used in this sketch
  • "dataPin" changed to pin 8 and used

Added

  • Function buttonPressed() that returns true once(!) when the button r1 (connected to A5) is pressed. For a next "true" the button has to be released first!
  • Function updateShiftRegisterL() that copies the state of leds to the yellow leds

If you want only one yellow led to be illuminated change the behaviour of updateShiftRegisterL() so that the bit set is calculated from the variable count.

/*
  Forum: https://forum.arduino.cc/t/picking-one-led-randomly-using-74hc595-register/1211816/10
  Wokwi: https://wokwi.com/projects/387086675901677569
*/

#include <SD.h>
#include <SPI.h>

//Boutons Choix du verbe conjugé
const int r1 = A5;
const int r2 = A4;
const int r3 = A3;
const int r4 = A2;
const int r5 = A1;
const int r6 = A0;

//Bouton de choix du Pronom personel
const int start = 7;
const byte ledpins[6] = {0B10000000, 0B01000010, 0B00100100, 0B00010000, 0B00001000, 0B00000100};

// Connexion arduino avec 74HC595:
///Pin connected to DS of 74HC595
int dataPin = 8;
///Pin connected to ST_CP of 74HC595
int latchPin = 2;
///Pin connected to SH_CP of 74HC595
int clockPin = 3;

// Connexion arduino avec 74HC595 (2):
///Pin connected to DS of 74HC595 (2)
int dataPin_2 = 4;
///Pin connected to ST_CP of 74HC595 (2)
int latchPin_2 = 5;
///Pin connected to SH_CP of 74HC595 (2)
int clockPin_2 = 6;

byte leds = 0;

void setup()
{
  pinMode(start, INPUT);
  //
  pinMode(r1, INPUT_PULLUP);
  pinMode(r2, INPUT_PULLUP);
  pinMode(r3, INPUT_PULLUP);
  pinMode(r4, INPUT_PULLUP);
  pinMode(r5, INPUT_PULLUP);
  pinMode(r6, INPUT_PULLUP);

  //set pins to output so you can control the shift register 74HC595
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  //set pins to output so you can control the shift register 74HC595 (2)
  pinMode(latchPin_2, OUTPUT);
  pinMode(clockPin_2, OUTPUT);
  pinMode(dataPin_2, OUTPUT);

}



unsigned long lastLedSet = 0;
int count = 0;

void loop()
{
  if (millis() - lastLedSet > 100)
  {
    count++;
    lastLedSet = millis();
    bitSet(leds, count);
    updateShiftRegisterR();
    if (count >= 7) {
      count = 0;
      leds = 0;
    }
  }
  if (buttonPressed()) {
    updateShiftRegisterL();
  }
}

void updateShiftRegisterR()
{
  digitalWrite(latchPin_2, LOW);
  shiftOut(dataPin_2, clockPin_2, LSBFIRST, leds);
  digitalWrite(latchPin_2, HIGH);
}

void updateShiftRegisterL()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}


boolean buttonPressed() {
  static unsigned long lastChange = 0;
  static byte state = HIGH;
  static byte lastState = HIGH;
  byte actState = digitalRead(r1);
  if (actState != lastState) {
    lastChange = millis();
    lastState = actState;
  }
  if (actState != state && millis() - lastChange > 20) {
    state = actState;
    return !state;
  }
  return false;
}

See on Wokwi: https://wokwi.com/projects/387086675901677569

The option to set only one led might look like this:

void updateShiftRegisterL()
{
  byte out = 0;
  bitSet(out, count);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, out);
  digitalWrite(latchPin, HIGH);
}

Nothing is happening.

just to clarify some points:

  • I'm here speaking about the Blue button and Blue leds.

  • Yellow leds will comes later in the coding. They are not included yet (Later on ....)

How many of the LEDs can be on at any time ?

Should that LED be blinking or on solidly and what should happen once you release the button ?

See here:

Sketch:

/*
  Forum: https://forum.arduino.cc/t/picking-one-led-randomly-using-74hc595-register/1211816/10
  Wokwi: https://wokwi.com/projects/387088789913913345
*/

#include <SD.h>
#include <SPI.h>

//Boutons Choix du verbe conjugé
const int r1 = A5;
const int r2 = A4;
const int r3 = A3;
const int r4 = A2;
const int r5 = A1;
const int r6 = A0;

//Bouton de choix du Pronom personel
const int start = 7;
const byte ledpins[6] = {0B10000000, 0B01000010, 0B00100100, 0B00010000, 0B00001000, 0B00000100};

// Connexion arduino avec 74HC595:
///Pin connected to DS of 74HC595
int dataPin = 8;
///Pin connected to ST_CP of 74HC595
int latchPin = 2;
///Pin connected to SH_CP of 74HC595
int clockPin = 3;

// Connexion arduino avec 74HC595 (2):
///Pin connected to DS of 74HC595 (2)
int dataPin_2 = 4;
///Pin connected to ST_CP of 74HC595 (2)
int latchPin_2 = 5;
///Pin connected to SH_CP of 74HC595 (2)
int clockPin_2 = 6;

byte leds = 0;




void setup()
{
  pinMode(start, INPUT_PULLUP);
  //
  pinMode(r1, INPUT_PULLUP);
  pinMode(r2, INPUT_PULLUP);
  pinMode(r3, INPUT_PULLUP);
  pinMode(r4, INPUT_PULLUP);
  pinMode(r5, INPUT_PULLUP);
  pinMode(r6, INPUT_PULLUP);

  //set pins to output so you can control the shift register 74HC595
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  //set pins to output so you can control the shift register 74HC595 (2)
  pinMode(latchPin_2, OUTPUT);
  pinMode(clockPin_2, OUTPUT);
  pinMode(dataPin_2, OUTPUT);

}


boolean blueRunning = true;
unsigned long lastLedSet = 0;
int count = 0;

void loop()
{
  if (millis() - lastLedSet > 100 && blueRunning)
  {
    count++;
    lastLedSet = millis();
    bitSet(leds, count);
    updateShiftRegisterR();
    if (count >= 7) {
      count = 0;
      leds = 0;
    }
  }
  if (buttonPressed()) {
    if (blueRunning){
      blueRunning = false;
      setShiftRegisterR();
    } else {
      blueRunning = true;
    }
    
  }
}

void updateShiftRegisterR()
{
  digitalWrite(latchPin_2, LOW);
  shiftOut(dataPin_2, clockPin_2, LSBFIRST, leds);
  digitalWrite(latchPin_2, HIGH);
}

void setShiftRegisterR()
{
  byte out = 0;
  bitSet(out, count);
  digitalWrite(latchPin_2, LOW);
  shiftOut(dataPin_2, clockPin_2, LSBFIRST, out);
  digitalWrite(latchPin_2, HIGH);
}


boolean buttonPressed() {
  static unsigned long lastChange = 0;
  static byte state = HIGH;
  static byte lastState = HIGH;
  byte actState = digitalRead(start);
  if (actState != lastState) {
    lastChange = millis();
    lastState = actState;
  }
  if (actState != state && millis() - lastChange > 20) {
    state = actState;
    return !state;
  }
  return false;
}

Once you got the principle you should be able to adopt it also to the other buttons and leds ...

Be aware that in your sketch the pinMode for the "start" button was only "INPUT" and not "INPUT_PULLUP" as required! I changed it in the sketch above.

The Led should remain ON .
If you release the button nothing happen.
Next push leds start/continu blinking again.

But i think better to make life easier...
Let's go in the direction where there is 2 buttons:
One is called "start" --> like start turning a "spinning wheel"
the other is called "stop" --> Stop at certain position

And the number of LEDs on at a time when they are blinking ?

Not necessary; look at the sketch from post 17 ... That is toggling between both states ...