This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
2.RGB-LED-Chaser.ino

2.RGB-LED-Chaser

This is an example of the ShiftRegisterOut class of the Control Surface library. It creates light effects using three 8-bit shift registers and some RGB LEDs.

Boards:
AVR, AVR USB, Teensy 3.x, ESP32

Connections

Connect three daisy-chained shift registers to the SPI pins.
Connect 8 RGB LEDs (+ current limiting resistors) to the outputs of the shift registers.

SCK >───────────┬──────────────────────┬──────────────────────┐
┏━━━━━━━┷━━━━━━━┓ ┏━━━━━━━┷━━━━━━━┓ ┏━━━━━━━┷━━━━━━━┓
┃ SH_CP ┃ ┃ SH_CP ┃ ┃ SH_CP ┃
MOSI >───┨ DS Q7S ┠──────┨ DS Q7S ┠──────┨ DS Q7S ┃
┃ ST_CP ┃ ┃ ST_CP ┃ ┃ ST_CP ┃
┗━━━━━━━┯━━━━━━━┛ ┗━━━━━━━┯━━━━━━━┛ ┗━━━━━━━┯━━━━━━━┛
SS >───────────┴──────────────────────┴──────────────────────┘

Remember to connect the enable pin of the shift register to ground and the master reset pin to Vcc in order to enable it.

The order of the colors doesn't matter. You can change them in software using the ShiftRegisterOutRGB::redBit, ShiftRegisterOutRGB::greenBit and ShiftRegisterOutRGB::blueBit constants.

If you wired the LEDs as RGB (red first, then green and then blue), the red bit is 0, the green bit is 1 and the blue bit is 2.
If you wired the LEDs as BGR (blue first, then green and then red), the red bit is 2, the green bit is 1 and the blue bit is 0.
Other combinations are possible as well.

Behavior

This example will turn on all red LEDs one by one, then turn them off one by one, next, it will turn on and off all green LEDs in the same manner, and finally the same for the blue LEDs. This is repeated indefinitely.

Mapping

None.

Demo

Written by Pieter P, 2018-07-13
https://github.com/tttapa/Control-Surface

#include <Control_Surface.h> // Include the Control Surface library.
using namespace ExtIO; // Bring the ExtIO pin functions into your sketch
// If you wired your LEDs as RGB
const uint8_t ShiftRegisterOutRGB::redBit = 0;
const uint8_t ShiftRegisterOutRGB::blueBit = 2;
// If you wired your LEDs as BGR
// const uint8_t ShiftRegisterOutRGB::redBit = 2;
// const uint8_t ShiftRegisterOutRGB::greenBit = 1;
// const uint8_t ShiftRegisterOutRGB::blueBit = 0;
// Create a new shift register output connected to pins 11, 13 and 10,
// shift the data out with the most significant bit first.
// There are 24 (= 3×8) outputs in total.
SPIShiftRegisterOut<24> sreg = {SS, MSBFIRST};
void setup() {
// Initialize the shift registers
sreg.begin();
}
constexpr unsigned long delayTime = 50;
void loop() {
// Red on
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.red(i), HIGH);
delay(delayTime);
}
// Red off
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.red(i), LOW);
delay(delayTime);
}
// Green on
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.green(i), HIGH);
delay(delayTime);
}
// Green off
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.green(i), LOW);
delay(delayTime);
}
// Blue on
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.blue(i), HIGH);
delay(delayTime);
}
// Blue off
for (uint8_t i = 0; i < 8; i++) {
digitalWrite(sreg.blue(i), LOW);
delay(delayTime);
}
}
SPIShiftRegisterOut
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
Definition: SPIShiftRegisterOut.hpp:23
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:34
ShiftRegisterOutRGB::blueBit
const static uint8_t blueBit
The position of the blue output pin for 3-color LEDs.
Definition: ShiftRegisterOutRGB.hpp:28
Control_Surface.h
The main header file that includes all Control-Surface header files.
ShiftRegisterOutBase::blue
pin_t blue(pin_t id)
Get the blue output pin of the given LED.
Definition: ShiftRegisterOutBase.ipp:46
ShiftRegisterOutRGB::redBit
const static uint8_t redBit
The position of the red output pin for 3-color LEDs.
Definition: ShiftRegisterOutRGB.hpp:18
ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:45
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:35
ExtIO
A namespace with alternatives to the standard Arduino IO functions that can be used with extended IO ...
Definition: ExtendedInputOutput.cpp:8
ShiftRegisterOutRGB::greenBit
const static uint8_t greenBit
The position of the green output pin for 3-color LEDs.
Definition: ShiftRegisterOutRGB.hpp:23
ShiftRegisterOutBase::green
pin_t green(pin_t id)
Get the green output pin of the given LED.
Definition: ShiftRegisterOutBase.ipp:24
SPIShiftRegisterOut::begin
void begin() override
Initialize the shift register.
Definition: SPIShiftRegisterOut.ipp:14
ShiftRegisterOutBase::red
pin_t red(pin_t id)
Get the red output pin of the given LED.
Definition: ShiftRegisterOutBase.ipp:35