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);
}