Skip to content

Instantly share code, notes, and snippets.

@fvdbosch

fvdbosch/towerlight.ino Secret

Created Apr 18, 2016
Embed
What would you like to do?
IoT Tower Light
/*
* This is a minimal example, see extra-examples.cpp for a version
* with more explantory documentation, example routines, how to
* hook up your pixels and all of the pixel types that are supported.
*
*/
#include "application.h"
#include "neopixel/neopixel.h"
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int animation = 0;
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Particle.subscribe("animation", myHandler);
}
void loop()
{
if(animation == 1) {
siren(50);
}
else if(animation == 2) {
pulse(10);
}
else if(animation == 3) {
blink(500);
}
else if(animation == 4) {
fill();
}
else {
clear();
}
}
void myHandler(const char *event, const char *data) {
animation = atoi(data);
}
void siren(uint8_t wait) {
uint16_t i, j;
for(i=0; i<6; i++) {
for(j=0; j<12; j++) {
strip.setPixelColor(j, strip.Color(0, 0, 0));
}
strip.setPixelColor(i, strip.Color(255, 0, 0));
strip.setPixelColor(i+1, strip.Color(255, 0, 0));
strip.setPixelColor(i+2, strip.Color(255, 0, 0));
strip.setPixelColor(i+6, strip.Color(255, 0, 0));
strip.setPixelColor(i+7, strip.Color(255, 0, 0));
strip.setPixelColor(i+8, strip.Color(255, 0, 0));
strip.show();
delay(wait);
}
}
void fill() {
uint16_t i;
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
}
void clear() {
uint16_t i;
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
void pulse(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(j, 0, 0));
}
strip.show();
delay(wait);
}
for(j=255; j>0; j--) {
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(j, 0, 0));
}
strip.show();
delay(wait);
}
}
void blink(uint16_t wait) {
uint16_t i, j;
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
delay(wait);
for(i=0; i<12; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
delay(wait);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment