Help with blinking single LED

Hardware: Arduino UNO

So I am new to shift registers and I am looking to convert my current code into shift register code to free up pins. At its current state, it sweeps the LEDs right then left, then goes to another script and that chooses a random number and then tells the shift register to sweep up to that particular LED and stop. What I am trying to do is make that LED that the random number chooses and make it blink. With my original code, I could just use digitalWrite(something, LOW) to turn it off and the opposite to turn it back on. How do I replicate that in shift register language? Thanks for any help I can receive!

const int tDelay = 25;
const int dataPin = 12;
const int latchPin = 11;
const int clockPin = 9;
bool DirectionState = 0;
long randNumber;
int number = 0;
int count = 0;
int reLoop = true;

void updateShiftRegister(unsigned int leds, bool isMSBFIRST = false) {
byte lowLED = lowByte(leds);
byte highLED = highByte(leds);
digitalWrite(latchPin, LOW);
if (isMSBFIRST == false) {
shiftOut(dataPin, clockPin, LSBFIRST, lowLED);
shiftOut(dataPin, clockPin, LSBFIRST, highLED);
} else {
shiftOut(dataPin, clockPin, MSBFIRST, highLED);
shiftOut(dataPin, clockPin, MSBFIRST, lowLED);
}
digitalWrite(latchPin, HIGH);
}

void setup() {
// put your setup code here, to run once:
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
randomSeed(analogRead(0));
}

void loop() {

if (reLoop == true) {
unsigned int leds = 0B0000000000000000;
if (count < 2) {
for (int i = 0; i < 16; i++) {
leds = 0B0000000000000000;
bitSet(leds, i);
updateShiftRegister(leds, DirectionState);
delay(tDelay);
}
count += 1;
}
DirectionState = !DirectionState;
numberPicker();
}
}
void numberPicker() {
if (count == 2) {
unsigned int leds = 0B0000000000000000;
randNumber = random(1, 15);
for (int i = 0; i < randNumber; i++) {
leds = 0B0000000000000000;
bitSet(leds, i);
updateShiftRegister(leds);
delay(25);
}
number = randNumber;
reLoop = false;
colorSelect();
}
}

void colorSelect() {
digitalWrite(latchPin, LOW);
for (int i = 0; i < 4; i++) {
}

}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

Turn the bit off or on and update the shift registers. The bits are numbered from right to left so the rightmost bit is 0 and the leftmost is 15.

To turn a bit off:

    uint16_t mask = 1 << bitNumber;
    leds &= ~mask;

The '~' operator inverts all of the bits in 'mask'. The '&=' operator turns off any bit in 'leds' where the matching bit in 'mask' was a one (before being inverted to zero).

To turn on a bit:

    uint16_t mask = 1 << bitNumber;
    leds |= mask;

The '|=' operator turns on any bit in 'leds' where the matching bit in 'mask' is a one.

Keep @johnwasser's 'mask' in memory so you always know what is currently "out there" in the shift register.

You should break the activity into separate functions that do the following -

  1. Set a LED on or off by its position number
  2. Update the shift register

For blink, you would use something like BlinkWithoutDelay sketch, except use 1 and 2 above in sequence to access the appropriate LED.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.