SkillDev_AmbientLamp
Made by Ziru Wei
Made by Ziru Wei
make an ambient lamp that can respond to the event start or end in a calendar
Created: November 13th, 2024
π π΄ π΅ οΌ [https://vimeo.com/1029310724]
Since I constantly failed to embedded the video in the document, Here is the link to this prototype π
1/ test the neopixel
#include "Particle.h"
#include <neopixel.h>
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
strip.begin();
strip.setPixelColor(0, strip.Color(255, 0, 0));
strip.show();
}
void loop() {
}
Click to Expand
#include <neopixel.h>
#include "Particle.h"
#define PIXEL_PIN SPI
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
strip.begin();
strip.setPixelColor(0, strip.Color(255, 0, 0));
strip.show();
}
void loop() {
rainbowCycle(20);
theaterChase(strip.Color(127, 127, 127), 50);
}
void theaterChase(uint32_t color, int wait) {
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 3; b++) {
strip.clear();
for (int c = b; c < strip.numPixels(); c += 3) {
strip.setPixelColor(c, color);
}
strip.show();
delay(wait);
}
}
}
void rainbowCycle(int wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) {
for (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 = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
Click to Expand
#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
blueCycle
) or respond to an event (fadeToRed
).
make an ambient lamp that can respond to the event start or end in a calendar