// hobbyprojects.com // apa102_rgb_LED_color_change.ino int apa102Clock = 7; int apa102Data = 8; int i; //--------------------------- void setup() { pinMode(apa102Data,OUTPUT); pinMode(apa102Clock,OUTPUT); } //--------------------------- void loop() { //Red Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- //Green Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- // Blue Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- // Yellow Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- // Cyan Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- // Magenta Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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) // End frame spiWrite(0b11111111); delay(1000); //-------------- // White Color // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); // LED frame spiWrite(0b11100111); // 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); //-------------- // LED OFF // Start Frame spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); spiWrite(0x00); //LED frame spiWrite(0b11100000); 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); }