Skip to content

Instantly share code, notes, and snippets.

@jef79m
Created June 25, 2014 11:58
Show Gist options
  • Save jef79m/86fb5846fb72f326b6f2 to your computer and use it in GitHub Desktop.
Save jef79m/86fb5846fb72f326b6f2 to your computer and use it in GitHub Desktop.
ws2812 dancefloor code
// Lightup Dancefloor sketch
// Lots of stuff borrowed from the NeoMatrix demo
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
#define BALLPIN 9
#define BAT A0
#define PIN 6
#define SENSOR_PIN 4
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = 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)
// Example for NeoPixel Shield. In this application we'd like to use it
// as a 5x8 tall matrix, with the USB port positioned at the top of the
// Arduino. When held that way, the first pixel is at the top right, and
// lines are arranged in columns, progressive order. The shield uses
// 800 KHz (v2) pixels that expect GRB color data.
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(10, 10, PIN,
NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
const uint16_t colors[] = {
matrix.Color(0, 150, 150),
matrix.Color(0, 150, 200),
matrix.Color(0, 200, 150),
matrix.Color(120, 60, 150),
matrix.Color(150, 150, 0),
matrix.Color(150, 0, 150)
};
const uint8_t numColors = 6;
const uint16_t WHITE = matrix.Color(255,255,255);
typedef unsigned long (*func)(int);
//list of effect functions
func normal_effects[] = {
rainbow,
discoFloor,
sparkle,
message
};
// effects for when battery is low.
// same size as normal effects for laziness.
func batt_low_effects[] = {
redsparkle,
batt_low_message,
redsparkle,
batt_low_message
};
const int numEffects = 4;
const unsigned long effectTime = 15 * 1000; //time to display each effect in milliseconds
func *effects;
bool mirrorOn;
void setup() {
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(70);
matrix.setTextColor(colors[0]);
effects = normal_effects;
pinMode(SENSOR_PIN, INPUT_PULLUP);
mirrorOn = true;
analogWrite(BALLPIN, 128);
}
int w = matrix.width();
int h = matrix.height();
int x,y = 0;
int colorindex = 0;
int effectIndex = 0;
int newEffect = 1;
int radius = 1;
unsigned long nextFrame = millis();
unsigned long nextEffect = millis() + effectTime;
void loop() {
if (digitalRead(SENSOR_PIN) == 1) {
if (! mirrorOn) {
mirrorOn = true;
analogWrite(BALLPIN, 128);
}
if (millis() > nextFrame) {
nextFrame = effects[effectIndex](newEffect);
}
if (millis() > nextEffect) {
if (analogRead(BAT) < 830) {
effects = batt_low_effects;
} else {
effects = normal_effects;
}
effectIndex++;
effectIndex = effectIndex % numEffects;
nextEffect = millis() + effectTime;
newEffect = 1;
}
} else {
digitalWrite(BALLPIN, LOW);
mirrorOn = false;
matrix.fillScreen(0);
matrix.show();
}
}
uint16_t nextColor() {
colorindex++;
colorindex = colorindex % numColors;
return colors[colorindex];
}
unsigned long discoFloor(int i) {
matrix.fillScreen(0);
for (int i = 0; i < matrix.numPixels(); i++) {
matrix.drawPixel(random(w), random(h), nextColor());
}
matrix.show();
return millis() + 1000;
}
unsigned long rainbow(int i) {
if ( i == 1 ) {
matrix.fillScreen(nextColor());
newEffect = 0;
}
if (radius == 0) {
x = random(w);
y = random(h);
}
matrix.drawCircle(x, y, radius, nextColor());
radius++;
radius = radius % (2*h);
matrix.show();
return millis() + 80;
}
unsigned long strobe(int i) {
matrix.fillScreen(WHITE);
matrix.show();
delay(10);
matrix.fillScreen(0);
matrix.show();
return millis() + 100;
}
unsigned long sparkle(int i) {
matrix.fillScreen(0);
for (int j = 0; j < 10; j++) {
matrix.drawPixel(random(w), random(h), WHITE);
}
matrix.show();
delay(10);
matrix.fillScreen(0);
matrix.show();
return millis() + 100;
}
unsigned long redsparkle(int i) {
matrix.fillScreen(0);
for (int j = 0; j < 10; j++) {
matrix.drawPixel(random(w), random(h), matrix.Color(255,0,0));
}
matrix.show();
delay(10);
matrix.fillScreen(0);
matrix.show();
return millis() + 100;
}
unsigned long message(int i) {
matrix.fillScreen(0);
if (i == 1) {
newEffect = 0;
x = w;
}
matrix.setCursor(x, 0);
matrix.print(F("Happy 40th Sara!"));
if(--x < -126) {
x = w;
matrix.setTextColor(nextColor());
}
matrix.show();
return millis() + 100;
}
unsigned long batt_low_message(int i) {
matrix.fillScreen(0);
matrix.setTextColor(matrix.Color(255,0,0));
if (i == 1) {
newEffect = 0;
x = w;
}
matrix.setCursor(x, 0);
matrix.print(F("low battery"));
if(--x < -70) {
x = w;
}
matrix.show();
return millis() + 100;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment