Go Down

Topic: Using shift registers to control 24 leds with only 3 pins on the arduino (Read 2371 times) previous topic - next topic

marco_arduino

Jun 03, 2014, 03:16 am Last Edit: Jun 06, 2014, 11:24 am by marco_arduino Reason: 1
using three shift registers to control 24 leds via only 3 pins on the arduino
http://youtu.be/f7S6K3T7dS4
http://www.youtube.com/watch?v=f7S6K3T7dS4
code:
Code: [Select]


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

}

marco_arduino

#1
Jun 03, 2014, 07:36 am Last Edit: Jun 06, 2014, 11:23 am by marco_arduino Reason: 1
more fun with shift registers:
adding a potentiometer to control the led being lit up.
http://youtu.be/JL8Lj0Lw0d8
http://www.youtube.com/watch?v=JL8Lj0Lw0d8

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

the code:
Code: [Select]

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




marco_arduino

#2
Jun 06, 2014, 07:21 am Last Edit: Jun 06, 2014, 11:20 am by marco_arduino Reason: 1
And now conrolling 48 leds with 6 shift registers:
http://youtu.be/uJvQNJfIMeI
http://www.youtube.com/watch?v=uJvQNJfIMeI
the code:
Code: [Select]

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




Go Up