SkillDev_AmbientLamp

Made by Ziru Wei

make an ambient lamp that can respond to the event start or end in a calendar

Created: November 13th, 2024

0

πŸ“… πŸ”΄ πŸ”΅ : [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

0
#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
0

2/ test with some light effect functions

0
#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
0

3/ test with Particle console

use input "1" and "2" as inputs

0
#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
0

A meimaid diagram to make the logic clearπŸ‘‡

0
  • Setup: Initializes the system, registers the Particle function, and starts the LEDs.
  • Loop: Continuously checks the status of the event flags and decides whether to run a normal cycle (blueCycle) or respond to an event (fadeToRed).
  • newEvent: Triggered by an external command, this function updates flags that influence the behavior in the loop.
  • fadeToRed: This function is called to smoothly transition the LED colors from their current state to red over a specified duration.
  • blueCycle: Regularly cycles through a blue-purple color spectrum unless interrupted by an event.
0

When the meeting event starts, it turns red gradually while when the event ends, it jumps to blue and purple cycle. 

It is because when it ends, both me and my lamp cannot wait to return to the peaceful and calm state...

x
Share this Project


About

make an ambient lamp that can respond to the event start or end in a calendar