#include "Particle.h"
#include <neopixel.h>
SYSTEM_MODE(AUTOMATIC);
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Function Declarations
void cyclePixels(uint8_t wait);
int newEvent(String command);
void fadeToRed();
void fadeToBluePurple();
void blueCycle(int wait);
uint32_t Wheel(byte WheelPos);
uint8_t lerp(uint8_t start, uint8_t end, float t);
// Global Variables
bool newEventApproaching = false;
bool keepRed = false;
void setup() {
strip.begin();
strip.show();
Particle.function("newEvent", newEvent);
}
void loop() {
if (!newEventApproaching && !keepRed) {
blueCycle(10); // Continue running blue-purple cycle
} else if (newEventApproaching) {
fadeToRed();
newEventApproaching = false;
keepRed = true;
}
}
int newEvent(String command) {
if (command == "1") {
newEventApproaching = true;
keepRed = false;
return 0;
}
return -1;
}
void fadeToRed() {
const unsigned long fadeDuration = 5000;
const unsigned long startTime = millis();
int pixelCount = strip.numPixels();
uint32_t startColors[pixelCount];
for (int i = 0; i < pixelCount; i++) {
startColors[i] = strip.getPixelColor(i);
}
while (millis() - startTime <= fadeDuration) {
float progress = (float)(millis() - startTime) / fadeDuration;
for (int i = 0; i < pixelCount; i++) {
uint8_t red = lerp((startColors[i] >> 16) & 0xFF, 255, progress);
uint8_t green = lerp((startColors[i] >> 8) & 0xFF, 0, progress);
uint8_t blue = lerp(startColors[i] & 0xFF, 0, progress);
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
}
for (int i = 0; i < pixelCount; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
}
void blueCycle(int wait) {
for (uint16_t j = 0; j < 256 * 5; j++) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos %= 256;
if (WheelPos < 128) {
return strip.Color(map(WheelPos, 0, 127, 0, 128), 0, 255);
} else {
return strip.Color(map(WheelPos, 128, 255, 128, 0), 0, 255);
}
}
uint8_t lerp(uint8_t start, uint8_t end, float t) {
return start + t * (end - start);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .