Skip to content

Instantly share code, notes, and snippets.

@mpj
Created December 12, 2019 15:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mpj/209b3a80054d1778c3dd38844a1cb9a0 to your computer and use it in GitHub Desktop.
treeproject
<style>
.buttons div {
border: 2px solid black;
margin: 10px;
padding: 1rem;
font-size: 3rem;
font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
text-align: center;
}
.buttons div:hover {
background-color: yellow;
}
</style>
<div class="buttons">
<div onclick="switchToColor('blue')">Blue</div>
<div onclick="switchToColor('red')">Red</div>
<div onclick="switchToColor('green')">Green</div>
<div onclick="switchToColor('yellow')">Yellow</div>
<div onclick="switchToColor('rainbow')">Rainbow</div>
</div>
<script>
function switchToColor(color) {
fetch(
'https://api.particle.io/v1/devices/28003a000547373336323230/ourtree',
{
body: `args=${color}&access_token=xxxxxx`,
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
}
</script>
/**
* This is the more completely documented example! (see below)
*/
/* ======================= includes ================================= */
#include "Particle.h"
#include "neopixel.h"
/* ======================= prototypes =============================== */
void colorAll(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void rainbow(uint8_t wait);
void rainbowCycle(uint8_t wait);
int brewCoffee(String command);
uint32_t Wheel(byte WheelPos);
/* ======================= extra-examples.cpp ======================== */
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 60
#define PIXEL_PIN D2
#define PIXEL_TYPE SK6812RGBW
// Parameter 1 = number of pixels in strip
// note: for some stripes like those with the TM1829, you
// need to count the number of segments, i.e. the
// number of controllers in your stripe, not the number
// of individual LEDs!
//
// Parameter 2 = pin number (if not specified, D2 is selected for you)
//
// On Photon, Electron, P1, Core and Duo, any pin can be used for Neopixel.
//
// On the Argon, Boron and Xenon, only these pins can be used for Neopixel:
// - D2, D3, A4, A5
// - D4, D6, D7, D8
// - A0, A1, A2, A3
//
// In addition on the Argon/Boron/Xenon, only one pin per group can be used
// at a time. So it's OK to have one Adafruit_NeoPixel instance on pin D2 and
// another one on pin A2, but it's not possible to have one on pin A0 and
// another one on pin A1.
//
// Parameter 3 = pixel type [ WS2812, WS2812B, WS2812B2, WS2813, WS2811,
// TM1803, TM1829, SK6812RGBW, WS2812B_FAST,
// WS2812B2_FAST ]
// note: if not specified, WS2812B is selected for you which
// is the same as WS2812 or WS2813 in operation.
// note: RGB order is automatically applied to WS2811,
// WS2812/WS2812B/WS2812B2/WS2813/TM1803 is GRB order.
// note: For legacy 50us reset pulse timing on WS2812/WS2812B
// or WS2812B2, select WS2812B_FAST or WS2812B2_FAST
// respectively. Otherwise 300us timing will be used.
//
// 800 KHz bitstream 800 KHz bitstream (most NeoPixel products
// WS2812/WS2813 (6-pin part)/WS2812B (4-pin part)/SK6812RGBW (RGB+W) )
//
// 400 KHz bitstream (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// (Radio Shack Tri-Color LED Strip - TM1803 driver
// NOTE: RS Tri-Color LED's are grouped in sets of 3)
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// 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.
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Spark.function("ourtree", ourTree);
}
void loop() {
// Some example procedures showing how to display to the pixels:
// Do not run more than 15 seconds of these, or the b/g tasks
// will be blocked.
//--------------------------------------------------------------
//strip.setPixelColor(0, strip.Color(255, 0, 255));
//strip.show();
//colorWipe(strip.Color(255, 0, 0), 50); // Red
//colorWipe(strip.Color(0, 255, 0), 50); // FUCKING RED
//strip.setPixelColor(6, strip.Color(0, 255, 0));
//strip.show();
//colorWipe(strip.Color(122, 0, 0), 50); // Blue
// rainbow(20);
//rainbowCycle(20);
// colorAll(strip.Color(0, 0, 255, 0), 50); // Cyan
}
int ourTree(String color)
{
Particle.publish("color", color, PRIVATE);
if(color == "blue") {
colorWipe(strip.Color(0, 0, 255), 50);
} else if(color == "green") {
colorWipe(strip.Color(255, 0, 0), 50);
} else if(color == "red") {
colorWipe(strip.Color(0, 255, 0), 50);
} else if(color == "yellow") {
colorWipe(strip.Color(255, 255, 0), 50);
} else if (color == "rainbow") {
rainbow(20);
}
}
// Set all pixels in the strip to a solid color, then wait (ms)
void colorAll(uint32_t c, uint8_t wait) {
uint16_t i;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(wait);
}
// Fill the dots one after the other with a color, wait (ms) after each one
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout, then wait (ms)
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) { // 1 cycle of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// 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) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment