Components and supplies
9V to Barrel Jack Connector
Arduino UNO
Male-Header 5 Position- 1 Row- Long (0.1")
Male/Female Jumper Wires
9V battery (generic)
ws2812 8x8 RGB LED matrix
Tools and machines
Solder Wire, Lead Free
Soldering iron (generic)
Project description
Code
LED_Arduino
c_cpp
This sketch forms the foundation of the project. On saving it should be set to Read-Only and further usage to be as 'Save As' for archiving to prevent overwriting and allow easy reedit.
1//VARIABLES AND DEFINES HERE - NEEDED BY THE WS2812 DRIVER CODE 2#define WS2812_pin 8 // only digital pin 8 works right now 3#define numberOfLEDs 64// total number of RGB LEDs [256] 4byte RGB[192];//take your number of LEDs and multiply by 3 [768] 5 6// FUNCTIONS HERE 7void RGB_update(int LED, byte RED, byte GREEN, byte BLUE);//function to drive LEDs 8 9void mapLEDXY(int x, int y, byte RED, byte GREEN, byte BLUE) { 10 int RGBlocation = 0; 11 12 //if (y % 2 == 0) { //even column [Uncomment] 13 14 RGBlocation = x + y * 8; //[16] 15 // } else { //odd column [Uncomment] 16 17 //RGBlocation = 7 - x + y * 8; //[15] and [16] 18 // } [Uncomment] 19 20 RGB[RGBlocation * 3] = BLUE; 21 RGB[RGBlocation * 3 + 1] = RED; 22 RGB[RGBlocation * 3 + 2] = GREEN; 23} 24void clearLEDs() { 25 memset(RGB, 0, sizeof(RGB)); 26} 27 28 29void setup() { 30 pinMode(WS2812_pin, OUTPUT); 31 clearLEDs(); 32 RGB_update(-1, 0, 0, 0); 33 34}//setup0 35 36 37void loop() { 38//Paste mapLEDXY line(s) directly above the RGB_update below. 39 40RGB_update(-1, 0, 0, 0); 41 delay(1000); 42 clearLEDs(); 43 RGB_update(-1, 0, 0, 0); 44 delay(1000); 45 46}//loop 47 48 49//WS2812 Driver Function 50void RGB_update(int LED, byte RED, byte GREEN, byte BLUE) { 51 // LED is the LED number starting with 0 52 // RED, GREEN, BLUE is the brightness 0..255 setpoint for that LED 53 byte ExistingPort, WS2812pinHIGH;//local variables here to speed up pinWrites 54 55 if (LED >= 0) { //map the REG GREEN BLUE Values into the RGB[] array 56 RGB[LED * 3] = GREEN; 57 RGB[LED * 3 + 1] = RED; 58 RGB[LED * 3 + 2] = BLUE; 59 } 60 61 noInterrupts();//kill the interrupts while we send the bit stream out... 62 ExistingPort = PORTB; // save the status of the entire PORT B - let's us write to the entire port without messing up the other pins on that port 63 WS2812pinHIGH = PORTB | 1; //this gives us a byte we can use to set the whole PORTB with the WS2812 pin HIGH 64 int bitStream = numberOfLEDs * 3;//total bytes in the LED string 65 66 //This for loop runs through all of the bits (8 at a time) to set the WS2812 pin ON/OFF times 67 for (int i = bitStream - 1; i >= 0; i--) { 68 69 PORTB = WS2812pinHIGH;//bit 7 first, set the pin HIGH - it always goes high regardless of a 0/1 70 71 //here's the tricky part, check if the bit in the byte is high/low then right that status to the pin 72 // (RGB[i] & B10000000) will strip away the other bits in RGB[i], so here we'll be left with B10000000 or B00000000 73 // then it's easy to check if the bit is high or low by AND'ing that with the bit mask ""&& B10000000)"" this gives 1 or 0 74 // if it's a 1, we'll OR that with the Existing port, thus keeping the pin HIGH, if 0 the pin is written LOW 75 PORTB = ((RGB[i] & B10000000) && B10000000) | ExistingPort; 76 __asm__("nop\ 77\ ""nop\ 78\ ""nop\ 79\ ""nop\ 80\ ""nop\ 81\ ");//these are NOPS - these let us delay clock cycles for more precise timing 82 PORTB = ExistingPort;//okay, here we know we have to be LOW regardless of the 0/1 bit state 83 __asm__("nop\ 84\ ""nop\ 85\ ""nop\ 86\ ""nop\ 87\ ""nop\ 88\ ""nop\ 89\ ""nop\ 90\ ");//minimum LOW time for pin regardless of 0/1 bit state 91 92 // then do it again for the next bit and so on... see the last bit though for a slight change 93 94 PORTB = WS2812pinHIGH;//bit 6 95 PORTB = ((RGB[i] & B01000000) && B01000000) | ExistingPort; 96 __asm__("nop\ 97\ ""nop\ 98\ ""nop\ 99\ ""nop\ 100\ ""nop\ 101\ "); 102 PORTB = ExistingPort; 103 __asm__("nop\ 104\ ""nop\ 105\ ""nop\ 106\ ""nop\ 107\ ""nop\ 108\ ""nop\ 109\ ""nop\ 110\ "); 111 112 PORTB = WS2812pinHIGH;//bit 5 113 PORTB = ((RGB[i] & B00100000) && B00100000) | ExistingPort; 114 __asm__("nop\ 115\ ""nop\ 116\ ""nop\ 117\ ""nop\ 118\ ""nop\ 119\ "); 120 PORTB = ExistingPort; 121 __asm__("nop\ 122\ ""nop\ 123\ ""nop\ 124\ ""nop\ 125\ ""nop\ 126\ ""nop\ 127\ ""nop\ 128\ "); 129 130 PORTB = WS2812pinHIGH;//bit 4 131 PORTB = ((RGB[i] & B00010000) && B00010000) | ExistingPort; 132 __asm__("nop\ 133\ ""nop\ 134\ ""nop\ 135\ ""nop\ 136\ ""nop\ 137\ "); 138 PORTB = ExistingPort; 139 __asm__("nop\ 140\ ""nop\ 141\ ""nop\ 142\ ""nop\ 143\ ""nop\ 144\ ""nop\ 145\ ""nop\ 146\ "); 147 148 PORTB = WS2812pinHIGH;//bit 3 149 PORTB = ((RGB[i] & B00001000) && B00001000) | ExistingPort; 150 __asm__("nop\ 151\ ""nop\ 152\ ""nop\ 153\ ""nop\ 154\ ""nop\ 155\ "); 156 PORTB = ExistingPort; 157 __asm__("nop\ 158\ ""nop\ 159\ ""nop\ 160\ ""nop\ 161\ ""nop\ 162\ ""nop\ 163\ ""nop\ 164\ "); 165 166 PORTB = WS2812pinHIGH;//bit 2 167 PORTB = ((RGB[i] & B00000100) && B00000100) | ExistingPort; 168 __asm__("nop\ 169\ ""nop\ 170\ ""nop\ 171\ ""nop\ 172\ ""nop\ 173\ "); 174 PORTB = ExistingPort; 175 __asm__("nop\ 176\ ""nop\ 177\ ""nop\ 178\ ""nop\ 179\ ""nop\ 180\ ""nop\ 181\ ""nop\ 182\ "); 183 184 PORTB = WS2812pinHIGH;//bit 1 185 PORTB = ((RGB[i] & B00000010) && B00000010) | ExistingPort; 186 __asm__("nop\ 187\ ""nop\ 188\ ""nop\ 189\ ""nop\ 190\ ""nop\ 191\ "); 192 PORTB = ExistingPort; 193 __asm__("nop\ 194\ ""nop\ 195\ ""nop\ 196\ ""nop\ 197\ ""nop\ 198\ ""nop\ 199\ ""nop\ 200\ "); 201 202 PORTB = WS2812pinHIGH;//bit 0 203 __asm__("nop\ 204\ ");//on this last bit, the check is much faster, so had to add a NOP here 205 PORTB = ((RGB[i] & B00000001) && B00000001) | ExistingPort; 206 __asm__("nop\ 207\ ""nop\ 208\ ""nop\ 209\ ""nop\ 210\ ""nop\ 211\ "); 212 PORTB = ExistingPort;//note there are no NOPs after writing the pin LOW, this is because the FOR Loop uses clock cycles that we can use instead of the NOPS 213 }//for loop 214 215 216 interrupts();//enable the interrupts 217 218 // all done! 219}//void RGB_update 220
Sketches.zip
These are some of or more sketches used during development. They are available as 'support'.
LED_Arduino
c_cpp
This sketch forms the foundation of the project. On saving it should be set to Read-Only and further usage to be as 'Save As' for archiving to prevent overwriting and allow easy reedit.
1//VARIABLES AND DEFINES HERE - NEEDED BY THE WS2812 DRIVER CODE 2#define WS2812_pin 8 // only digital pin 8 works right now 3#define numberOfLEDs 64// total number of RGB LEDs [256] 4byte RGB[192];//take your number of LEDs and multiply by 3 [768] 5 6// FUNCTIONS HERE 7void RGB_update(int LED, byte RED, byte GREEN, byte BLUE);//function to drive LEDs 8 9void mapLEDXY(int x, int y, byte RED, byte GREEN, byte BLUE) { 10 int RGBlocation = 0; 11 12 //if (y % 2 == 0) { //even column [Uncomment] 13 14 RGBlocation = x + y * 8; //[16] 15 // } else { //odd column [Uncomment] 16 17 //RGBlocation = 7 - x + y * 8; //[15] and [16] 18 // } [Uncomment] 19 20 RGB[RGBlocation * 3] = BLUE; 21 RGB[RGBlocation * 3 + 1] = RED; 22 RGB[RGBlocation * 3 + 2] = GREEN; 23} 24void clearLEDs() { 25 memset(RGB, 0, sizeof(RGB)); 26} 27 28 29void setup() { 30 pinMode(WS2812_pin, OUTPUT); 31 clearLEDs(); 32 RGB_update(-1, 0, 0, 0); 33 34}//setup0 35 36 37void loop() { 38//Paste mapLEDXY line(s) directly above the RGB_update below. 39 40RGB_update(-1, 0, 0, 0); 41 delay(1000); 42 clearLEDs(); 43 RGB_update(-1, 0, 0, 0); 44 delay(1000); 45 46}//loop 47 48 49//WS2812 Driver Function 50void RGB_update(int LED, byte RED, byte GREEN, byte BLUE) { 51 // LED is the LED number starting with 0 52 // RED, GREEN, BLUE is the brightness 0..255 setpoint for that LED 53 byte ExistingPort, WS2812pinHIGH;//local variables here to speed up pinWrites 54 55 if (LED >= 0) { //map the REG GREEN BLUE Values into the RGB[] array 56 RGB[LED * 3] = GREEN; 57 RGB[LED * 3 + 1] = RED; 58 RGB[LED * 3 + 2] = BLUE; 59 } 60 61 noInterrupts();//kill the interrupts while we send the bit stream out... 62 ExistingPort = PORTB; // save the status of the entire PORT B - let's us write to the entire port without messing up the other pins on that port 63 WS2812pinHIGH = PORTB | 1; //this gives us a byte we can use to set the whole PORTB with the WS2812 pin HIGH 64 int bitStream = numberOfLEDs * 3;//total bytes in the LED string 65 66 //This for loop runs through all of the bits (8 at a time) to set the WS2812 pin ON/OFF times 67 for (int i = bitStream - 1; i >= 0; i--) { 68 69 PORTB = WS2812pinHIGH;//bit 7 first, set the pin HIGH - it always goes high regardless of a 0/1 70 71 //here's the tricky part, check if the bit in the byte is high/low then right that status to the pin 72 // (RGB[i] & B10000000) will strip away the other bits in RGB[i], so here we'll be left with B10000000 or B00000000 73 // then it's easy to check if the bit is high or low by AND'ing that with the bit mask ""&& B10000000)"" this gives 1 or 0 74 // if it's a 1, we'll OR that with the Existing port, thus keeping the pin HIGH, if 0 the pin is written LOW 75 PORTB = ((RGB[i] & B10000000) && B10000000) | ExistingPort; 76 __asm__("nop\ 77\ ""nop\ 78\ ""nop\ 79\ ""nop\ 80\ ""nop\ 81\ ");//these are NOPS - these let us delay clock cycles for more precise timing 82 PORTB = ExistingPort;//okay, here we know we have to be LOW regardless of the 0/1 bit state 83 __asm__("nop\ 84\ ""nop\ 85\ ""nop\ 86\ ""nop\ 87\ ""nop\ 88\ ""nop\ 89\ ""nop\ 90\ ");//minimum LOW time for pin regardless of 0/1 bit state 91 92 // then do it again for the next bit and so on... see the last bit though for a slight change 93 94 PORTB = WS2812pinHIGH;//bit 6 95 PORTB = ((RGB[i] & B01000000) && B01000000) | ExistingPort; 96 __asm__("nop\ 97\ ""nop\ 98\ ""nop\ 99\ ""nop\ 100\ ""nop\ 101\ "); 102 PORTB = ExistingPort; 103 __asm__("nop\ 104\ ""nop\ 105\ ""nop\ 106\ ""nop\ 107\ ""nop\ 108\ ""nop\ 109\ ""nop\ 110\ "); 111 112 PORTB = WS2812pinHIGH;//bit 5 113 PORTB = ((RGB[i] & B00100000) && B00100000) | ExistingPort; 114 __asm__("nop\ 115\ ""nop\ 116\ ""nop\ 117\ ""nop\ 118\ ""nop\ 119\ "); 120 PORTB = ExistingPort; 121 __asm__("nop\ 122\ ""nop\ 123\ ""nop\ 124\ ""nop\ 125\ ""nop\ 126\ ""nop\ 127\ ""nop\ 128\ "); 129 130 PORTB = WS2812pinHIGH;//bit 4 131 PORTB = ((RGB[i] & B00010000) && B00010000) | ExistingPort; 132 __asm__("nop\ 133\ ""nop\ 134\ ""nop\ 135\ ""nop\ 136\ ""nop\ 137\ "); 138 PORTB = ExistingPort; 139 __asm__("nop\ 140\ ""nop\ 141\ ""nop\ 142\ ""nop\ 143\ ""nop\ 144\ ""nop\ 145\ ""nop\ 146\ "); 147 148 PORTB = WS2812pinHIGH;//bit 3 149 PORTB = ((RGB[i] & B00001000) && B00001000) | ExistingPort; 150 __asm__("nop\ 151\ ""nop\ 152\ ""nop\ 153\ ""nop\ 154\ ""nop\ 155\ "); 156 PORTB = ExistingPort; 157 __asm__("nop\ 158\ ""nop\ 159\ ""nop\ 160\ ""nop\ 161\ ""nop\ 162\ ""nop\ 163\ ""nop\ 164\ "); 165 166 PORTB = WS2812pinHIGH;//bit 2 167 PORTB = ((RGB[i] & B00000100) && B00000100) | ExistingPort; 168 __asm__("nop\ 169\ ""nop\ 170\ ""nop\ 171\ ""nop\ 172\ ""nop\ 173\ "); 174 PORTB = ExistingPort; 175 __asm__("nop\ 176\ ""nop\ 177\ ""nop\ 178\ ""nop\ 179\ ""nop\ 180\ ""nop\ 181\ ""nop\ 182\ "); 183 184 PORTB = WS2812pinHIGH;//bit 1 185 PORTB = ((RGB[i] & B00000010) && B00000010) | ExistingPort; 186 __asm__("nop\ 187\ ""nop\ 188\ ""nop\ 189\ ""nop\ 190\ ""nop\ 191\ "); 192 PORTB = ExistingPort; 193 __asm__("nop\ 194\ ""nop\ 195\ ""nop\ 196\ ""nop\ 197\ ""nop\ 198\ ""nop\ 199\ ""nop\ 200\ "); 201 202 PORTB = WS2812pinHIGH;//bit 0 203 __asm__("nop\ 204\ ");//on this last bit, the check is much faster, so had to add a NOP here 205 PORTB = ((RGB[i] & B00000001) && B00000001) | ExistingPort; 206 __asm__("nop\ 207\ ""nop\ 208\ ""nop\ 209\ ""nop\ 210\ ""nop\ 211\ "); 212 PORTB = ExistingPort;//note there are no NOPs after writing the pin LOW, this is because the FOR Loop uses clock cycles that we can use instead of the NOPS 213 }//for loop 214 215 216 interrupts();//enable the interrupts 217 218 // all done! 219}//void RGB_update 220
Sketches.zip
These are some of or more sketches used during development. They are available as 'support'.
Led_Utility.xlsm
This application generates the various text for copy/paste into the sketch.
Comments
Only logged in users can leave comments
Murrayman
0 Followers
•0 Projects
4
0
Excel for WS2812 RGB LED Array Animations | Arduino Project Hub