Devices & Components
Arduino Nano
Jumper wires (generic)
USB-A to Mini-USB Cable
Resistor 100 ohm
74HC595 Shift Register
Solderless Breadboard Full Size
Software & Tools
Arduino IDE
Project description
Code
ShiftRegister RGB LED Code
c_cpp
1const int ShiftPWM_latchPin=8; 2 3#include <ShiftPWM.h> 4 5const bool ShiftPWM_invertOutputs = false; 6const bool ShiftPWM_balanceLoad = false; 7unsigned char maxBrightness = 255; 8unsigned char pwmFrequency = 75; 9int numRegisters = 4; 10int numRGBleds = numRegisters*8/3; 11 12void setup() 13 { 14 Serial.begin(9600); 15 16// Sets the number of 8-bit registers that are used. 17ShiftPWM.SetAmountOfRegisters(numRegisters); 18ShiftPWM.SetPinGrouping(1); 19ShiftPWM.Start(pwmFrequency,maxBrightness); 20} 21 22void loop() 23 { 24ShiftPWM.SetAll(0); 25ShiftPWM.PrintInterruptLoad(); 26ShiftPWM.OneByOneFast(); 27 28 for(int j=0;j<maxBrightness;j++) 29 { 30 ShiftPWM.SetAll(j); 31 delay(20); 32 } 33 34 for(int j=maxBrightness;j>=0;j--) 35 { 36 ShiftPWM.SetAll(j); 37 delay(20); 38 } 39 40 41 for(int output=0;output<numRegisters*8-1;output++) 42 { 43 ShiftPWM.SetAll(0); 44 for(int brightness=0;brightness<maxBrightness;brightness++) 45 { 46 ShiftPWM.SetOne(output+1,brightness); 47 ShiftPWM.SetOne(output,maxBrightness-brightness); 48 delay(1); 49 } 50 } 51 52 53 for(int hue = 0; hue<360; hue++) 54 { 55 ShiftPWM.SetAllHSV(hue, 255, 255); 56 delay(50); 57 } 58 59 60 for(int shift=0;shift<6;shift++) 61 { 62 for(int led=0; led<numRGBleds; led++) 63 { 64 switch((led+shift)%6) 65 { 66 case 0: 67 ShiftPWM.SetRGB(led,255,0,0); 68 break; 69 case 1: 70 ShiftPWM.SetRGB(led,0,255,0); 71 break; 72 case 2: 73 ShiftPWM.SetRGB(led,0,0,255); 74 break; 75 case 3: 76 ShiftPWM.SetRGB(led,255,128,0); 77 break; 78 case 4: 79 ShiftPWM.SetRGB(led,0,255,255); 80 break; 81 case 5: 82 ShiftPWM.SetRGB(led,255,0,255); 83 break; 84 } 85 } 86 delay(2000); 87 } 88 89 90 for(int i=0;i<1000;i++){ 91 ShiftPWM.SetHSV(random(numRGBleds),random(360),255,255); 92 delay(15); 93 } 94int peak=0; 95int prevPeak=0; 96int currentLevel = 0; 97 98for(int i=0;i<40;i++) 99 { 100 prevPeak = peak; 101 while(abs(peak-prevPeak)<5) 102 { 103 peak = random(numRGBleds); 104 } 105 while(currentLevel!=peak) 106 { 107 if(currentLevel<peak) 108 { 109 currentLevel++; 110 } 111 else 112 { 113 currentLevel--; 114 } 115 for(int led=0;led<numRGBleds;led++) 116 { 117 if(led<=currentLevel) 118 { 119 int hue = (numRGBleds-1-led)*120/numRGBleds; 120 ShiftPWM.SetHSV(led,hue,255,255); 121 } 122 else 123 { 124 ShiftPWM.SetRGB(led,0,0,0); 125 } 126 } 127delay((64/numRGBleds)*(numRGBleds-currentLevel)); 128 } 129 } 130rgbLedRainbow(numRGBleds, 5, 3, numRegisters*8/3); 131rgbLedRainbow(numRGBleds, 10, 3, numRegisters*8/3*4); 132} 133 134void rgbLedRainbow(int numRGBLeds, int delayVal, int numCycles, int rainbowWidth) 135 { 136 ShiftPWM.SetAll(0); 137 for(int cycle=0;cycle<numCycles;cycle++) 138 { 139 for(int colorshift=0;colorshift<360;colorshift++) 140 { 141 for(int led=0;led<numRGBLeds;led++) 142 { 143 int hue = ((led)*360/(rainbowWidth-1)+colorshift)%360; 144 ShiftPWM.SetHSV(led, hue, 255, 255); 145 } 146delay(delayVal); 147 } 148 } 149}
Downloadable files
Shift Register RGB LED Circuit diagram pdf
PDF File
Shift Register RGB LED Circuit diagram pdf
Shift Register RGB LED Circuit diagram pdf
PDF File
Shift Register RGB LED Circuit diagram pdf
Comments
Only logged in users can leave comments