Created
April 2, 2017 03:43
-
-
Save runewake2/8b40408818087d4a61e79becfab44857 to your computer and use it in GitHub Desktop.
Data Cube arduino code for reading serial information and displaying it as pixels on a neopixel light strip.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DATA CUBE | |
// Written as a part of World of Zero see: youtube.com/worldofzerodevelopment | |
// for videos on creating this and other fun projects | |
#include <Adafruit_NeoPixel.h> | |
#define PIN 6 | |
#define N_LEDS 240 | |
typedef struct { | |
uint8_t r; | |
uint8_t g; | |
uint8_t b; | |
} color; | |
color initColor(uint8_t r, uint8_t g, uint8_t b) { | |
color c; | |
c.r = r; | |
c.g = g; | |
c.b = b; | |
return c; | |
} | |
color colors[N_LEDS + 10]; | |
void moveColors() { | |
for(int i = N_LEDS - 2 + 10; i >= 0; i--) { | |
colors[i+1] = colors[i]; | |
} | |
colors[0] = initColor(0,0,0); | |
} | |
int start = 0; | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800); | |
void setup() { | |
Serial.begin(9600); | |
strip.begin(); | |
//On program startup initialize all pixels to blank. Handy if too much data causes a restart. | |
for(int i = 0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, strip.Color(0,0,0)); | |
} | |
strip.show(); | |
colors[0] = initColor(255,0,0); | |
colors[N_LEDS/3] = initColor(0,255,0); | |
colors[(N_LEDS/3) * 2] = initColor(0,0,255); | |
} | |
void drawPixel(int index, color targetColor) { | |
uint8_t currentR = targetColor.r; | |
uint8_t currentG = targetColor.g; | |
uint8_t currentB = targetColor.b; | |
for(int i = index; i > index - 10; --i) { | |
strip.setPixelColor(i, strip.Color(currentR, currentG, currentB)); | |
currentR /= 2; | |
currentG /= 2; | |
currentB /= 2; | |
} | |
} | |
uint8_t generatedColor[3]; | |
int generatedColorIndex = 0; | |
void loop() { | |
moveColors(); | |
while(Serial.available() > 0) { | |
Serial.println(Serial.available()); | |
uint8_t info = Serial.read(); | |
if (info >= 0) { | |
generatedColor[generatedColorIndex++] = info; | |
if (generatedColorIndex >= 3) { | |
colors[0] = initColor(generatedColor[0], generatedColor[1], generatedColor[2]); | |
generatedColorIndex = 0; | |
generatedColor[0] = 0; | |
generatedColor[1] = 0; | |
generatedColor[2] = 0; | |
break; | |
} | |
} | |
} | |
for(int i=strip.numPixels() + 10; i>=0; i--) { | |
if (colors[i].r != 0 || colors[i].g != 0 || colors[i].b != 0) { //TODO: invert | |
drawPixel(i, colors[i]); | |
} | |
} | |
strip.show(); | |
delay(25); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment