Skip to content

Instantly share code, notes, and snippets.

@adonixis
Created June 27, 2017 13:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adonixis/a5e38bd2b415d58a085c431f821b5994 to your computer and use it in GitHub Desktop.
Save adonixis/a5e38bd2b415d58a085c431f821b5994 to your computer and use it in GitHub Desktop.
Lightbox Sketch for Arduino Nano
#include <Adafruit_NeoPixel.h>
#define PIN 5
#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 2); // RX | TX
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(58, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
int j_rainbow_cycle = 256*5;
int j_rainbow = 256;
char mode;
byte countSegments;
float ratio;
byte startPixelArray[60];
byte countPixelsArray[60];
byte alphaArray[60];
byte redArray[60];
byte greenArray[60];
byte blueArray[60];
String command;
void setup() {
Serial.begin(9600);
BTserial.begin(38400);
BTserial.setTimeout(50); //It turns out that the Serial class (which extends the Stream class) uses the Stream.setTimeout() function. The default value for the timeout is 1000 (one second)
strip.begin();
strip.show(); // Initialize all pixels to 'off'
mode = EEPROM[0];
//Serial.println("mode - " + String(mode));
switch (mode) {
case 's':
countSegments = EEPROM[1];
//Serial.println("countSegments - " + String(countSegments));
for (int i = 0; i < countSegments; i++) {
startPixelArray[i] = EEPROM[2 + i*6];
countPixelsArray[i] = EEPROM[3 + i*6];
alphaArray[i] = EEPROM[4 + i*6];
redArray[i] = EEPROM[5 + i*6];
greenArray[i] = EEPROM[6 + i*6];
blueArray[i] = EEPROM[7 + i*6];
ratio = (float) alphaArray[i] / 255.0;
//Serial.println("startPixelArray[" + String(i) + "] - " + String(startPixelArray[i]) + ", countPixelsArray[" + String(i) + "] - " + String(countPixelsArray[i]) + ", redArray[" + String(i) + "] - " + String(redArray[i]) + ", greenArray[" + String(i) + "] - " + String(greenArray[i]) + ", blueArray[" + String(i) + "] - " + String(blueArray[i]));
segmentColor(startPixelArray[i], countPixelsArray[i], strip.Color((int) (redArray[i] * ratio), (int) (greenArray[i] * ratio), (int) (blueArray[i] * ratio))); //segments color
}
break;
case 'r':
case 'c':
break;
}
}
void loop() {
// if there's any serial available, read it:
while (BTserial.available() > 0) {
char tempMode = BTserial.read();
switch (tempMode) {
case 's':
BTserial.read();
countSegments = BTserial.parseInt();
for (int i = 0; i < countSegments; i++) {
startPixelArray[i] = BTserial.parseInt();
countPixelsArray[i] = BTserial.parseInt();
alphaArray[i] = BTserial.parseInt();
redArray[i] = BTserial.parseInt();
greenArray[i] = BTserial.parseInt();
blueArray[i] = BTserial.parseInt();
}
BTserial.read();
BTserial.flush();
mode = tempMode;
EEPROM[0] = mode;
EEPROM[1] = countSegments;
//Serial.println("mode - " + String(mode));
//Serial.println("countSegments - " + String(countSegments));
for (int i = 0; i < countSegments; i++) {
EEPROM[2 + i*6] = startPixelArray[i];
EEPROM[3 + i*6] = countPixelsArray[i];
EEPROM[4 + i*6] = alphaArray[i];
EEPROM[5 + i*6] = redArray[i];
EEPROM[6 + i*6] = greenArray[i];
EEPROM[7 + i*6] = blueArray[i];
//Serial.println("startPixelArray[" + String(i) + "] - " + String(startPixelArray[i]) + ", countPixelsArray[" + String(i) + "] - " + String(countPixelsArray[i]) + ", alphaArray[" + String(i) + "] - " + String(alphaArray[i]) + ", redArray[" + String(i) + "] - " + String(redArray[i]) + ", greenArray[" + String(i) + "] - " + String(greenArray[i]) + ", blueArray[" + String(i) + "] - " + String(blueArray[i]));
ratio = (float) alphaArray[i] / 255.0;
segmentColor(startPixelArray[i], countPixelsArray[i], strip.Color((int) (redArray[i] * ratio), (int) (greenArray[i] * ratio), (int) (blueArray[i] * ratio))); //segments color
}
break;
case 'r':
case 'c':
BTserial.read();
BTserial.flush();
mode = tempMode;
EEPROM[0] = mode;
//Serial.println("mode - " + String(mode));
break;
case '?':
BTserial.read();
BTserial.flush();
//send lightbox parameters:
//model: 0 - deers
//start corner: 0 - left-bottom, 1 - left-top, 2 - right-top, 3 - right-bottom
//0 - clockwise, 1 - counterclockwise
//all pixels count
//left pixels count
//top pixels count
//right pixels count
//bottom pixels count
//mode
//command (optional)
switch (mode) {
case 's':
command = String(countSegments);
for (int i = 0; i < countSegments; i++) {
command += " " + String(startPixelArray[i]);
command += " " + String(countPixelsArray[i]);
command += " " + String(alphaArray[i]);
command += " " + String(redArray[i]);
command += " " + String(greenArray[i]);
command += " " + String(blueArray[i]);
}
BTserial.println("Info: 0 0 1 58 17 13 18 10 " + String(mode) + " " + command);
break;
case 'r':
case 'c':
BTserial.println("Info: 0 0 1 58 17 13 18 10 " + String(mode));
break;
}
break;
}
}
switch (mode) {
case 's':
break;
case 'r':
//rainbow
if (j_rainbow > 0) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, wheel((i + j_rainbow) & 255));
}
strip.show();
j_rainbow--;
delay(200);
} else {
j_rainbow = 256;
}
break;
case 'c':
//rainbow cycle
if (j_rainbow_cycle > 0) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, wheel(((i * 256 / strip.numPixels()) + j_rainbow_cycle) & 255));
}
strip.show();
j_rainbow_cycle--;
delay(200);
} else {
j_rainbow_cycle = 256*5;
}
break;
}
//delay(50); // delay in between reads for stability
}
void segmentColor(int startPixel, int countPixels, uint32_t color) {
for (int i = startPixel; i < startPixel + countPixels; i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t wheel(byte wheelPos) {
wheelPos = 255 - wheelPos;
if (wheelPos < 85) {
return strip.Color(255 - wheelPos * 3, 0, wheelPos * 3);
}
if (wheelPos < 170) {
wheelPos -= 85;
return strip.Color(0, wheelPos * 3, 255 - wheelPos * 3);
}
wheelPos -= 170;
return strip.Color(wheelPos * 3, 255 - wheelPos * 3, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment