/* Prototype for haloween costume 2016, read input string from serial and perfrom action character by character with delay */
// Built from StringLength Arduino example and Adafruit StrandTest example
#include "Adafruit_WS2801.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#define ONDELAY 750
#define OFFDELAY 500
String txtMsg = "welcome"; // Initialize default string for incoming text
uint8_t dataPin = 3; // Yellow wire on Adafruit Pixels
uint8_t clockPin = 2; // Green wire on Adafruit Pixels
Adafruit_WS2801 strip = Adafruit_WS2801(25, dataPin, clockPin);
// Array to handle colors of alphabet, 3 element subarrays represent rgb
const int colors[26][3] {
{255, 100, 0}, // a
{0, 100, 255}, // b
{255, 0, 100}, // c
{100, 255, 0}, // d
{0, 100, 255}, // e
{255, 100, 0}, // f
{255, 0, 100}, // g
{100, 255, 0}, // h
{0, 100, 255}, // i
{255, 0, 100}, // j
{0, 100, 255}, // k
{100, 255, 0}, // l
{255, 100, 0}, // m
{255, 0, 100}, // n
{255, 0, 100}, // o
{100, 255, 0}, // p
{255, 0, 100}, // q
{100, 255, 0}, // r
{255, 100, 0}, // s
{255, 100, 0}, // t
{0, 100, 255}, // u
{255, 0, 100}, // v
{0, 100, 255}, // w
{255, 100, 0}, // x
{255, 0, 100}, // y
{255, 0, 100}, // z
};
// Array to transpose incoming ascii positions to corresponding positions in light fixture
const int charPos[25] {
19, //A
20, //B
21, //C
22, //D
23, //E
24, //F
18, //G
17, //H
16, //I
15, //J
14, //K
13, //L
12, //M
5, //N
6, //O
7, //P
8, //Q
9, //R
10, //S
11, //T
4, //U
3, //V
2, //W
1, //X
0, //Y
};
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
strip.begin();
// Update LED contents, to start they are all 'off'
strip.show(); // write all pixels out
}
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b) {
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
// Program to step through string character by character and perform an action
void printChar(String input) {
for (int i = 0; i < input.length(); i++) {
int pos = (((int)input[i]) - 97);
// Get color from colors array
// Subtract one from pos to properly index charPos Array
strip.setPixelColor(charPos[pos], Color(colors[pos][0], colors[pos][1], colors[pos][2]));
strip.show();
delay(ONDELAY);
strip.setPixelColor(charPos[pos], Color(0, 0, 0));
strip.show();
delay(OFFDELAY);
}
}
// Function to blink all lights at constant speed
void blinkConst(void) {
int blinkCount = 0;
int i;
while (blinkCount < 3) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Color(colors[i][0], colors[i][1], colors[i][2]));
}
strip.show();
delay(ONDELAY);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Color(0, 0, 0));
}
strip.show();
delay(OFFDELAY);
blinkCount++;
}
}
// Function to blink all lights at variable speed
void blinkVar(int speed) {
int i;
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Color(colors[i][0], colors[i][1], colors[i][2]));
}
strip.show();
delay(speed);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Color(0, 0, 0));
}
strip.show();
delay(speed);
}
// Function to blink all lights randomly
void blinkRandom(int speed) {
int i, randomNum;
for (i = 0; i <= 50; i++) {
randomNum = random(strip.numPixels());
strip.setPixelColor(randomNum, Color(colors[i][0], colors[i][1], colors[i][2]));
strip.show();
delay(speed);
strip.setPixelColor(randomNum, Color(0, 0, 0));
strip.show();
delay(speed);
}
}
// Function to blink all lights in a row
void blinkRow(int speed) {
int i;
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(charPos[i], Color(colors[i][0], colors[i][1], colors[i][2]));
strip.show();
delay(speed);
}
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Color(0, 0, 0));
}
strip.show();
}
// Main program body, loop checks for new serial input and runs functions based on character input or default print function
void loop() {
// Set string to incoming text
while (Serial.available() > 0) {
delay(10);
char inChar = Serial.read();
if(inChar == '*'){ // Clear string using '*' input
txtMsg = "";
}
else if (inChar == '%'){ // Run other functions using specified inputs
blinkConst();
}
else if (inChar == '&'){ // Run other functions using specified inputs
for (int i = 500; i >= 50; i -= 50)
{
blinkVar(i);
}
}
else if (inChar == '$'){ // Run other functions using specified inputs
blinkRandom(50);
}
else if (inChar == '#'){ // Run other functions using specified inputs
blinkRow(50);
}
else { // Other wise add input serial character to cleared string
txtMsg += inChar;
}
}
//blinkBlink();
printChar(txtMsg);
}
Comments