Using shift registers to control 24 leds with only 3 pins on the arduino

using three shift registers to control 24 leds via only 3 pins on the arduino

code:

int dataPin = 11;
int latchPin = 8;
int clockPin = 12;

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
long unsigned  bin=1;
  for (int tensColumn = 1; tensColumn < 25; tensColumn++) {
      // take the latchPin low so 
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
    //  shiftOut(dataPin, clockPin, MSBFIRST, bin);
   //   shiftOut(dataPin, clockPin, MSBFIRST, bin>>8);     
      shiftOut(dataPin, clockPin, MSBFIRST, bin);
      shiftOut(dataPin, clockPin, MSBFIRST,bin>>8); 
        shiftOut(dataPin, clockPin, MSBFIRST,bin>>16); 
      bin*=2; // 1 becomes 10 , becomes 100 , becomes 1000 and so on in binary
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
     delay(30);
    }
bin=8388608;// 1000000000000000000000000 in binary
  for (int tensColumn = 1; tensColumn < 25; tensColumn++) {
 
      // take the latchPin low so 
      // the LEDs don't change while you're sending in bits:
      digitalWrite(latchPin, LOW);
      // shift out the bits:
      shiftOut(dataPin, clockPin, MSBFIRST, bin);
      shiftOut(dataPin, clockPin, MSBFIRST, bin>>8);    
      shiftOut(dataPin, clockPin, MSBFIRST, bin>>16); 
      bin/=2;
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
      // pause before next value:
     delay(30);
    }
 
 }

more fun with shift registers:
adding a potentiometer to control the led being lit up.

feel free to subscribe:

the code:

int dataPin = 11;
int latchPin = 8;
int clockPin = 12;
long unsigned binary[]= {1 , 2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,
32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608};// 24 values since there are 24 leds
// 1 , 10 ,100 , 1000 , 10000 .......  in binary

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop() {
long unsigned  bin=1;
 int pot = analogRead(A0);
int mymap=map(pot,0,1014,1,24);//map the range of the values from led 1 to led 24
bin=binary[mymap-1];//gets into bin the correct value to turn it on from the array
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, bin);
      shiftOut(dataPin, clockPin, MSBFIRST,bin>>8); 
        shiftOut(dataPin, clockPin, MSBFIRST,bin>>16); 
      //take the latch pin high so the LEDs will light up:
      digitalWrite(latchPin, HIGH);
    }

And now conrolling 48 leds with 6 shift registers:

the code:

int dataPin = 11;
int latchPin = 8;
int clockPin = 12;
long unsigned binary[]= {1 , 2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,
32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,
1073741824,2147483648};
// 1 , 10 ,100 , 1000 , 10000 .......  in binary

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop() {
  long unsigned  bin=0;
    long unsigned  bin2=0;
 int pot = analogRead(A0);
int mymap=map(pot,0,1014,1,48);//map the range of the values from led 1 to led 24
  digitalWrite(latchPin, LOW);
if  (mymap<=32) 
bin=binary[mymap-1];//gets into bin the correct value to turn it on from the array
{
      
     shiftOut(dataPin, clockPin, MSBFIRST, bin);
    shiftOut(dataPin, clockPin, MSBFIRST,bin>>8); 
    shiftOut(dataPin, clockPin, MSBFIRST,bin>>16); 
       shiftOut(dataPin, clockPin, MSBFIRST,bin>>24); 
       shiftOut(dataPin, clockPin, MSBFIRST,0); 
       shiftOut(dataPin, clockPin, MSBFIRST,0); 
       
  
  
}

if(mymap>=33)

{
bin2=binary[mymap-1-32];
}
     if(mymap>=33)
       {
            shiftOut(dataPin, clockPin, MSBFIRST,0); 
       shiftOut(dataPin, clockPin, MSBFIRST,0); 
          shiftOut(dataPin, clockPin, MSBFIRST,0); 
       shiftOut(dataPin, clockPin, MSBFIRST,0); 
           shiftOut(dataPin, clockPin, MSBFIRST,bin2);
               shiftOut(dataPin, clockPin, MSBFIRST,bin2>>8);
               bin=0;
       }



      digitalWrite(latchPin, HIGH);
    }