// hobbyprojects.com // APA102_RGB_7LEDs_STRIP_BLINK.ino int apa102Clock = 7; int apa102Data = 8; int i; //--------------------------- void setup() { pinMode(apa102Data,OUTPUT); pinMode(apa102Clock,OUTPUT); } //--------------------------- void loop() { // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // Red color // LED1 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b11111111); // red LED ON // Green color // LED2 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b11111111); // green LED ON spiWrite(0b00000000); // red LED OFF // Blue color // LED3 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b11111111); // blue LED ON spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF // Yellow color // LED4 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b11111111); // green LED ON spiWrite(0b11111111); // red LED ON // Cyan color // LED5 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b11111111); // blue LED ON spiWrite(0b11111111); // green LED ON spiWrite(0b00000000); // red LED OFF // Magenta color // LED6 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b11111111); // blue LED ON spiWrite(0b00000000); // green LED OFF spiWrite(0b11111111); // red LED ON // White color // LED7 frame spiWrite(0b11101000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b11111111); // blue LED ON spiWrite(0b11111111); // green LED ON spiWrite(0b11111111); // red LED ON // End frame spiWrite(0b11111111); //-------------------- delay(1000); //-------------------- // All LEDs OFF // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); //LED1 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED2 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED3 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED4 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED5 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED6 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF //LED7 frame spiWrite(0b11100000); // The first three bits(MSB) of the LED frame should be ‘1’. The next 5 bits(LSB) are brightness value (0–31) spiWrite(0b00000000); // blue LED OFF spiWrite(0b00000000); // green LED OFF spiWrite(0b00000000); // red LED OFF // End frame spiWrite(0b11111111); //-------------------- delay(500); //-------------------- } //=========================================================================== void spiWrite(uint8_t c) { uint8_t i; for (i=0; i<8 ;i++) { if (!(c&0x80)) { digitalWrite(apa102Data, LOW); } else { digitalWrite(apa102Data, HIGH); } digitalWrite(apa102Clock, HIGH); c <<= 1; digitalWrite(apa102Clock, LOW); } digitalWrite(apa102Data, HIGH); }