Skip to content

Instantly share code, notes, and snippets.

@andrewdalpino
Last active December 15, 2017 03:24
Show Gist options
  • Save andrewdalpino/47fb069f6142267a551d5acbdeba030f to your computer and use it in GitHub Desktop.
Save andrewdalpino/47fb069f6142267a551d5acbdeba030f to your computer and use it in GitHub Desktop.
Degeneratesmas 2017 White Elephant Gift - Trippy Arduino Disco Light
/*
White elephant gift for Degeneratesmas 2017
By: Andrew DalPino
*/
#include <Metro.h>
#include <Adafruit_NeoPixel.h>
#include <Bounce2.h>
#define PIXEL_PIN 3
#define BUTTON_PIN 4
#define NUM_PIXELS 8
#define BRIGHTNESS 50
#define MAX_LUMINANCE 255
#define DEBOUNCE_INTERVAL 50
int mode = 0;
int button_state = LOW;
int cube_positions[4] = {0, 1, 2, 3};
int cube_colors[4][3] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 191, 0}};
int strobe_color[3] = {255, 255, 255};
int sort_positions[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int sort_colors[8][3] = {{255, 0, 0}, {255, 128, 0}, {255, 255, 0}, {0, 255, 0}, {0, 255, 255}, {0, 0, 255}, {128, 0, 255}, {255, 0, 128}};
int binary_counter = 0;
int binary_colors[2][3] = {{50, 0, 0}, {64, 0, 255}};
int pulse_position = 0;
int pulse_color[3] = {255, 0, 64};
bool pulse_reverse = false;
Metro metro = Metro();
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
Bounce debouncer = Bounce();
void setup() {
Serial.begin(9600);
Serial.println("Merry Christmas mother fuckers. -Andrew");
debouncer.attach(BUTTON_PIN);
debouncer.interval(DEBOUNCE_INTERVAL);
pixels.setBrightness(BRIGHTNESS);
pixels.begin();
}
void loop() {
switch (mode) {
case 0:
blackout();
break;
case 1:
acid();
break;
case 2:
binary_count();
break;
case 3:
rubix();
break;
case 4:
pulse();
break;
case 5:
bubble_sort();
break;
case 6:
strobe();
break;
}
pushMyButtons();
}
void acid() {
for (int i = 0; i < NUM_PIXELS; i++) {
int r = adjustLuminance(random(0, 255));
int g = adjustLuminance(random(0, 255));
int b = adjustLuminance(random(0, 255));
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
metro.interval(100);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void rubix() {
int first = random(0, 4), second;
int swap, count = 0;
int r, g, b;
if (first >= 3) {
second = 2;
} else if (first <= 0) {
second = 1;
} else {
if (random(0, 1) == 0) {
second = first - 1;
} else {
second = first + 1;
}
}
swap = cube_positions[first];
cube_positions[first] = cube_positions[second];
cube_positions[second] = swap;
for (int i = 0; i < 4; i++) {
r = adjustLuminance(cube_colors[cube_positions[i]][0]);
g = adjustLuminance(cube_colors[cube_positions[i]][1]);
b = adjustLuminance(cube_colors[cube_positions[i]][2]);
for (int j = 0; j < round(NUM_PIXELS / 4); j++) {
pixels.setPixelColor(count, pixels.Color(r, g, b));
count++;
}
}
pixels.show();
metro.interval(500);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void pulse() {
int r, g, b;
if (pulse_position > NUM_PIXELS - 1) {
pulse_position = NUM_PIXELS - 1;
pulse_reverse = true;
} else if (pulse_position < 0) {
pulse_position = 0;
pulse_reverse = false;
}
if (pulse_reverse != true) {
pulse_position++;
} else {
pulse_position--;
}
r = adjustLuminance(pulse_color[0]);
g = adjustLuminance(pulse_color[1]);
b = adjustLuminance(pulse_color[2]);
blackout();
pixels.setPixelColor(pulse_position, pixels.Color(r, g, b));
pixels.show();
metro.interval(25);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void bubble_sort() {
int swap;
int r, g, b;
shuffle(sort_positions, 8);
for (int i = 0; i < NUM_PIXELS; i++) {
r = adjustLuminance(sort_colors[sort_positions[i]][0]);
g = adjustLuminance(sort_colors[sort_positions[i]][1]);
b = adjustLuminance(sort_colors[sort_positions[i]][2]);
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
metro.interval(1000);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
for (int i = 0; i < NUM_PIXELS - 1; i++) {
for (int j = i + 1; j < NUM_PIXELS; j++) {
if (sort_positions[i] >= sort_positions[j]) {
swap = sort_positions[i];
sort_positions[i] = sort_positions[j];
sort_positions[j] = swap;
for (int k = 0; k < NUM_PIXELS; k++) {
r = adjustLuminance(sort_colors[sort_positions[k]][0]);
g = adjustLuminance(sort_colors[sort_positions[k]][1]);
b = adjustLuminance(sort_colors[sort_positions[k]][2]);
pixels.setPixelColor(k, pixels.Color(r, g, b));
}
pixels.show();
metro.interval(100);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
}
}
metro.interval(1000);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void strobe() {
int r, g, b;
for (int i = 0; i < NUM_PIXELS; i++) {
if (random(0, 10) == 5) {
r = adjustLuminance(strobe_color[0]);
g = adjustLuminance(strobe_color[1]);
b = adjustLuminance(strobe_color[2]);
} else {
r = 0; g = 0; b = 0;
}
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
metro.interval(10);
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void binary_count() {
int value;
int r, g, b;
for (int i = 0; i < NUM_PIXELS; i++) {
value = bitRead(binary_counter, i);
r = adjustLuminance(binary_colors[value][0]);
g = adjustLuminance(binary_colors[value][1]);
b = adjustLuminance(binary_colors[value][2]);
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
if (binary_counter >= pow(2, NUM_PIXELS) - 1) {
metro.interval(1000);
binary_counter = 0;
} else {
metro.interval(100);
binary_counter++;
}
metro.reset();
while (metro.check() != true) {
pushMyButtons();
}
}
void pushMyButtons() {
debouncer.update();
button_state = debouncer.read();
if (button_state == HIGH) {
mode++;
if (mode > 6) {
mode = 0;
}
}
}
void blackout() {
pixels.clear();
}
void setAllPixels(int r, int g, int b) {
r = adjustLuminance(r);
g = adjustLuminance(g);
b = adjustLuminance(b);
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
}
int adjustLuminance(int value) {
return constrain(value, 0, MAX_LUMINANCE);
}
void shuffle(int *array, size_t n)
{
if (n > 1) {
size_t i;
for (int i = 0; i < n - 1; i++) {
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment