I want to ON one by one 16 led using two shift register

I want to on 16 leds one after one using two 74HC595 shift register. I have write this code but not working. anybody can help me ?

const int latchPin = 3;
const int clockPin = 2;
const int dataPin = 4;

byte led[16] = {0b1000000000000000, 
                0b0100000000000000, 
                0b0010000000000000, 
                0b0001000000000000, 
                0b0000100000000000,
                0b0000010000000000,
                0b0000001000000000,
                0b0000000100000000,
                0b0000000010000000,
                0b0000000001000000,
                0b0000000000100000,
                0b0000000000010000,
                0b0000000000001000,
                0b0000000000000100,
                0b0000000000000010,
                0b0000000000000001};

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 16; i++)
  {
    byte lowLED = lowByte(led[i]);
    byte highLED = highByte(led[i]);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, lowLED);
    shiftOut(dataPin, clockPin, LSBFIRST, highLED);
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }
}

Please help.

0b00001111 syntax can be applied only to byte values - so no more than 8 bits long

You should store your high and lowbyte in two separate uint8_t variables if you want to use "0b" notation.
But the best is calculate it in place before shifting out with shifting bits operations

@b707
then how can i fix it ?

you do not need the led[] array at all

for (int i = 0; i < 16; i++)
  {
    byte lowLED = lowByte(1 << i );
    byte highLED = highByte(1 << i );
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, lowLED);
    shiftOut(dataPin, clockPin, LSBFIRST, highLED);
    digitalWrite(latchPin, HIGH);
    delay(1000);
  }

@b707
Thanks for your help it's working. but it is starting from 16th led how can i starting from 1st led ?
and one more question is if i want to on only 12 number of led then how can i ON ?

and can you help me to understand you code ? i am new to arduino so please

for example

byte lowLED = lowByte(1 << (15 -i) );
byte highLED = highByte(1 <<(15 - i) );

I changed only one operator in your code. All you need to understand is to read something about the bit shift operator x << y

Sorry, but it's hard for me to explain this in more detail because I don't know English well

@b707
can you please help me for this ?

Thank you!

just replace 16 to 12 in for loop condition

@b707
Not in loop i want say just only 12th LED. not other

you could guess yourself, no?

byte lowLED = lowByte(1 << 12);
byte highLED = highByte(1 <<12 );

Please mark the problem solved if you have received answers to questions :slight_smile:

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