pwm-respiration.ino
A PWM LED breathes gently toward a target brightness changed remotely via Particle.function. The exponential smoothing avoids hard jumps, and the sinusoidal beat gives a lively look.
Hardware
A Particle Photon module (2.4 GHz Wi-Fi, STM32 microcontroller, metal shielding) powered by USB or VIN. 1 LED + 330 Ω resistor (the module pins are 3.3 V).
Wiring
LED anode to D0 through 330 Ω, cathode to GND. D0 is a PWM output on the module.
How it works
analogWrite applies the duty cycle; the loop filters the setpoint (5 % smoothing per 10 ms step) and modulates amplitude with a sine. regler() updates the target from the cloud.
Running it
With the Particle CLI: particle flash --serial (DFU mode, double reset) or OTA flash once the board is online. Safe Mode (reset + WiFi) covers troubleshooting.
Variants
Chain several channels for ambient lighting, or close the loop on a light sensor (LDR on A0).
pwm-respiration.ino
// Respiration LED sur D0 (broche PWM) + reglage via Particle.function.
const int LED = D0; // sortie PWM (timer) sur le module
int niveauCible = 128; // 0..255, reglable a distance
float niveau = 0;
unsigned long derniereEtape = 0;
int regler(String args) {
int v = args.toInt();
if (v >= 0 && v <= 255) { niveauCible = v; return v; }
return -1;
}
void setup() {
pinMode(LED, OUTPUT);
Particle.function("luminosite", regler);
Serial.begin(9600);
}
void loop() {
if (millis() - derniereEtape < 10) return;
derniereEtape = millis();
// Rampe vers la consigne + leger battement sinusoidal.
static float phase = 0;
phase += 0.06;
float battement = 0.25 * sin(phase);
niveau += (niveauCible * (0.75 + battement) - niveau) * 0.05;
analogWrite(LED, constrain((int)niveau, 0, 255));
}
// Regler depuis le cloud : Particle.function "luminosite" avec "200".