Files
exploring-christmas-lights/christmas-lights.c.ino
2022-03-17 00:35:16 -07:00

90 lines
1.7 KiB
C++

enum Pattern {
STEADY = 0,
FADE = 0x10,
SPARKLE = 0x20,
FIREFLY= 0x30
};
enum Color {
OFF,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
MULTICOLOR
};
Pattern patterns[] = {STEADY, FADE, SPARKLE, FIREFLY};
Color colors[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, MULTICOLOR, OFF};
int PULSE_DURATION = 100; // microseconds
int SHORT_PAUSE = 4964; // microseconds
int LONG_PAUSE = 16992; // miccroseconds
// output pin to send the signals to
int command_pin = 8;
/**
* Send a single short pulse of "off".
*/
void send_pulse() {
digitalWrite(command_pin, LOW);
delayMicroseconds(PULSE_DURATION);
digitalWrite(command_pin, HIGH);
}
/**
* Send a single bit.
*/
void send_bit(bool b) {
send_pulse();
delayMicroseconds(b ? LONG_PAUSE : SHORT_PAUSE);
}
/**
* Send a whole command. Commands start with a 0 bit then six bits of data.
*/
void send_command(int cmd) {
// start command
send_bit(0);
for (int i = 0x20; i > 0; i >>= 1) {
send_bit(cmd & i);
}
// end command
send_pulse();
// minimum time before sending another command
delayMicroseconds(LONG_PAUSE);
}
/**
* Send a whole command based on a pattern and color.
*/
void send_command(Pattern pattern, Color color) {
send_command(pattern | color);
}
void setup() {
pinMode(command_pin, OUTPUT);
digitalWrite(command_pin, HIGH);
}
void loop() {
delay(1000);
// Sample "demo" mode where it cycles between all the colors of all the patterns
for (int p = 0; p < sizeof(patterns) / sizeof(Pattern); p++) {
for (int c = 0; c < sizeof(colors) / sizeof(Color); c++) {
send_command(patterns[p], colors[c]);
delay(10000);
}
}
}