Pages: [1]   Go Down
Author Topic: Using shift registers to control 24 leds with only 3 pins on the arduino  (Read 2160 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

using three shift registers to control 24 leds via only 3 pins on the arduino
http://youtu.be/f7S6K3T7dS4

code:
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);
    }
 
 }
« Last Edit: June 06, 2014, 04:24:48 am by marco_arduino » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

more fun with shift registers:
adding a potentiometer to control the led being lit up.
http://youtu.be/JL8Lj0Lw0d8


feel free to subscribe:
https://www.youtube.com/channel/UC1_tz9mcSpWdx-ektovx6GQ?sub_confirmation=1

the code:
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);
    }

 
 
« Last Edit: June 06, 2014, 04:23:40 am by marco_arduino » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And now conrolling 48 leds with 6 shift registers:
http://youtu.be/uJvQNJfIMeI

the code:
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);
    }

 
 
« Last Edit: June 06, 2014, 04:20:43 am by marco_arduino » Logged

Pages: [1]   Go Up
Jump to: