Shtuchka & Memo
Hey Memo, you ever coded a wardrobe that changes colors based on your mood? I’m thinking of a smart jacket that updates its look with a swipe of your phone—wearable tech meets runway. What’s the code‑behind for that?
Hey, it’s doable with a microcontroller like an ESP32, an LED matrix or flexible RGB strips, and a few sensors. Below’s a minimal sketch that shows the core flow – read a mood indicator, pick a color, and push it to the LEDs via Bluetooth from your phone. You’ll still need to tune the sensor thresholds and add the UI on the phone, but the logic stays the same.
```cpp
// 1. Include libraries
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_NeoPixel.h>
// 2. Pin definitions
#define LED_PIN 5 // GPIO for NeoPixel strip
#define LED_COUNT 30
#define HEART_PIN 34 // Analog pin for heart rate sensor
#define ACCEL_CS 15 // CS pin for an I2C accelerometer
// 3. Objects
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
WebServer server(80);
// 4. Mood thresholds
const int calmThreshold = 60; // bpm
const int energeticThreshold = 100;
// 5. Map moods to colors
struct MoodColor { const char* name; uint32_t color; };
MoodColor moodMap[] = {
{"calm", strip.Color(0, 0, 255)}, // blue
{"happy", strip.Color(255, 255, 0)}, // yellow
{"energetic", strip.Color(255, 0, 0)} // red
};
void setup() {
Serial.begin(115200);
strip.begin(); strip.show();
// Setup WiFi
WiFi.begin("ssid","pass");
while (WiFi.status() != WL_CONNECTED) delay(500);
server.on("/", HTTP_GET, [](){ server.send(200, "text/plain", "Mood Jacket"); });
server.begin();
// Init accelerometer (if using)
// Wire.begin();
// ... init code ...
}
int readHeartRate() {
int val = analogRead(HEART_PIN);
// Simple mapping, replace with proper algorithm
return map(val, 0, 4095, 40, 120);
}
int readAccelerometer() {
// Stub: return 0 if still, 1 if moving
// Replace with actual sensor read
return random(0,2);
}
void setMoodColor(const char* mood) {
for (auto &m : moodMap) {
if (strcmp(m.name, mood) == 0) {
for (int i=0; i<LED_COUNT; ++i) strip.setPixelColor(i, m.color);
strip.show();
break;
}
}
}
void loop() {
int bpm = readHeartRate();
int moving = readAccelerometer();
const char* mood;
if (bpm < calmThreshold && moving == 0) mood = "calm";
else if (bpm < energeticThreshold) mood = "happy";
else mood = "energetic";
setMoodColor(mood);
server.handleClient();
delay(2000); // sample every 2 seconds
}
```
**How it works**
1. **Sensor reading** – the heart‑rate sensor gives a rough BPM value, the accelerometer tells if you’re moving.
2. **Mood determination** – a very simple rule set maps BPM + motion to “calm”, “happy”, or “energetic”.
3. **Color assignment** – each mood has an RGB value; the strip lights up in that color.
4. **Phone control** – the ESP32 hosts a tiny web server; your phone can hit the root URL or add parameters to override the mood.
For example, a simple AJAX call to `http://<esp_ip>/set?mood=energetic` could force a change.
**Next steps**
- Replace the heart‑rate stub with a proper pulse sensor algorithm (moving average, peak detection).
- Use a real IMU (like an MPU‑6050) for accurate motion detection.
- Add a Bluetooth Low Energy (BLE) service instead of Wi‑Fi for lower power.
- Build a small Android/iOS UI that sends mood commands and maybe even displays a live mood graph.
That’s the core “code‑behind”. The details of the sensor integration and UI polish will turn it into a slick jacket. Happy hacking!