Skills Dev 3

Made by Tianyi He · HIDDEN

Created: December 18th, 2023

This project is unlisted and only folks with the link can see it. Be considerate and think twice before sharing.

0

Process

I didn't encounter much problems except for some basic issues. It also refreshed my memory on c++ and switch statements.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

#include "neopixel.h"

#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

long timerStartAt = -1;
int status = 0; 
std::array<int, 3> red = {255, 0, 0};
std::array<int, 3> white = {255, 255, 255};
long fadeDuration = 1000 * 15 * 60;

void setup() {
    Serial.begin(9600); 
    strip.begin();
    strip.show(); 
    Particle.function("changeStatus", handleStatue);
    Particle.variable("status", status);
}

uint32_t getCurrentRGB(int fadeDuration, int timeElapsed, std::array<int, 3> prevColor, std::array<int, 3> targetColor) {
    int prev_r = prevColor[0];
    int prev_g = prevColor[1];
    int prev_b = prevColor[2];

    int target_r = targetColor[0];
    int target_g = targetColor[1];
    int target_b = targetColor[2];

    int r = 0, g = 0, b = 0; 

    if (fadeDuration != 0) {
        r = map(timeElapsed, 0, fadeDuration, prev_r, target_r);
        g = map(timeElapsed, 0, fadeDuration, prev_g, target_g);
        b = map(timeElapsed, 0, fadeDuration, prev_b, target_b);
    }

    return strip.Color(r, g, b);
}

int handleStatue(String new_status) {
    if (new_status.toInt() >= 0 && new_status.toInt() <= 2) {
        status = new_status.toInt();
    } else {
        Particle.publish("error status");
    }
    return 1;
}

void loop() {
    long timeNow = millis();
    long timeElapsed = timeNow - timerStartAt;
    uint32_t c;

    switch (status) {
        case 0:
            c = strip.Color(255, 255, 255);
            timerStartAt = timeNow;
            break;
        case 1:
            c = (timeElapsed < fadeDuration) ? getCurrentRGB(fadeDuration, timeElapsed, white, red) : (status = 2, timerStartAt = timeNow, strip.Color(255, 255, 255));
            break;
        case 2:
            c = (timeElapsed < fadeDuration) ? getCurrentRGB(fadeDuration, timeElapsed, red, white) : (status = 0, timerStartAt = timeNow, strip.Color(255, 255, 255));
            break;
        default:
            Particle.publish("error status");
            return;
    }

    for(int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
    }
    strip.show();
}
Click to Expand
0

Product


0
0

Reflection

I found this lab particularly enjoyable due to the immense potential I see in LEDs for ambient display purposes. It's potential for integrating with other devices is exciting.

x
Share this Project

This project is unlisted and only folks with the link can see it. Be considerate and think twice before sharing.


Courses

48-675 Designing for the Internet of Things 24

· 11 members

A hands-on introductory course exploring the Internet of Things and connected product experiences.


About

~