Skip to content

Instantly share code, notes, and snippets.

@andywarr
Last active October 24, 2016 01:25
Show Gist options
  • Save andywarr/0830685c86cd1c4f742de59846e02343 to your computer and use it in GitHub Desktop.
Save andywarr/0830685c86cd1c4f742de59846e02343 to your computer and use it in GitHub Desktop.
Light 12mm LED Pixels
#include "Adafruit_WS2801.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#ifdef __AVR_ATtiny85__
#include <avr/power.h>
#endif
// 2 pins used for output.
uint8_t dataPin = 2; // Yellow wire on Adafruit Pixels
uint8_t clockPin = 3; // Green wire on Adafruit Pixels
// Initialize strip
Adafruit_WS2801 strip = Adafruit_WS2801(25, dataPin, clockPin);
// 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;
}
// Declare Colors
uint32_t orange = Color(255, 153, 0);
uint32_t red = Color(255,0,0);
uint32_t blue = Color(0,0,255);
uint32_t white = Color(255, 255, 255);
uint32_t green = Color(0,255,0);
uint32_t Colors[] = {orange, red, blue, white, green};
int size = sizeof(Colors)/sizeof(Colors[0]);
String sequence = "abcdefghqponmlkjirstuvwxy";
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif
strip.begin();
// Update LED contents, to start they are all 'off'
strip.show();
}
void loop() {
display("righthere", 1500);
turnOn(false);
delay(3000);
display("run", 3000);
delay(1000);
flash(100);
turnOn(false);
delay(6000);
}
void display(String letters, int wait) {
for (int i=0; i < letters.length(); i++) {
int index = letterIndex(letters[i]);
for (int j=0; j < strip.numPixels(); j++) {
if (j == index) {
strip.setPixelColor(j, Colors[j % size]);
}
else {
strip.setPixelColor(j, Color(0, 0, 0));
}
}
strip.show();
delay(wait);
}
}
void flash(int times) {
for (int i=0; i < times; i++) {
if (i % 2 == 0) {
turnOn(true);
}
else {
turnOn(false);
}
delay(100);
}
}
void turnOn(boolean on) {
for (int i=0; i < strip.numPixels(); i++) {
if (on == true) {
strip.setPixelColor(i, Colors[i % size]);
}
else {
strip.setPixelColor(i, Color(0, 0, 0));
}
}
strip.show();
}
int letterIndex(char letter) {
for (int i=0; i < sequence.length(); i++) {
if (sequence[i] == letter) {
return i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment