We all like blinky things, and I like a 'breathing' LED on projects when it fits in..
I've added it to a couple of different things now, so decided to use it to make a library.
I've got the functionality as I want it (i think), but I'm not sure I'm going about it in a great way. I may have over-complicated it a tad. Could anyone spare their time to share any thoughts or tips please?
Breathe.h
#include "Arduino.h" // let us use Arduino
#ifndef Breathe_h // guard against library being included twice
#define Breathe_h
class Breathe{
public:
Breathe(byte debugOn,int LEDPin,byte brMin,byte brMax,byte brUpdateTime, byte brStep);
void maintain(); // makes changes to LED brightness, maintains breathing
void stop(); // stops breathing
byte _debugOn; // if 1/true enables debugging, if 0/false disables debugging messages
private:
byte _breathingIn; // direction of led fade (breathing in=getting brighter)
int _LEDPin; // LED pin
byte _brMin; // minimum brightness
byte _brMax; // maximum brightness
byte _brUpdateTime; // ms between brightness updates
byte _brLvl; // current brightness level
byte _brStep; // amount to increment/decrement brightness for each update
byte _breathing; // currently breathing? (last breath out or stopped if not)
unsigned long _brCurTime; // current time(ms)
unsigned long _lstbrUpdateTime; // last time brightness changed
};
#endif
Breathe.cpp
#include "Breathe.h"
Breathe::Breathe(byte debugOn,int LEDPin, byte brMin,byte brMax,byte brUpdateTime, byte brStep){
_debugOn=debugOn;
_breathingIn=true; // direction of led fade (breathing in=getting brighter)
_brStep=brStep;
_brCurTime=millis();
_lstbrUpdateTime=0; // last time breath brightness changed
_LEDPin=LEDPin;
_brMin=brMin;
_brMax=brMax;
_brUpdateTime=brUpdateTime;
_brLvl=_brMin;
_breathing=true;
if(_debugOn){ // send message over serial
Serial.print("Breathe_starting: ");
Serial.print("LEDPin ");
Serial.print(_LEDPin);
Serial.print("brMin ");
Serial.print(_brMin);
Serial.print(" brMax ");
Serial.print(_brMax);
Serial.print(" brUpdateTime ");
Serial.println(_brUpdateTime);
}
pinMode(_LEDPin, OUTPUT); // make LED pin an output
analogWrite(_LEDPin, _brLvl); // write lowest brightness value
_lstbrUpdateTime=_brCurTime; // record time brightness was updated
}
void Breathe::maintain(){
_brCurTime=millis(); // record current time
if(_brCurTime>=_lstbrUpdateTime+_brUpdateTime){ // if it's time to breathe
if(_breathing){
if(_debugOn){ // send message over serial
Serial.print("Breathe_maintain: ");
}
if(_breathingIn){ // brightness increasing
if(_brLvl>=_brMax-_brStep){ // hit the end, stop and go back
_brLvl=_brMax;
_breathingIn=false;
} else { // keep going
_brLvl=_brLvl+_brStep;
}
} else { // brightness decreasing
if(_brLvl<=_brMin+_brStep){ // hit the end, stop and go back
_brLvl=_brMin;
_breathingIn=true;
} else { // keep going
_brLvl=_brLvl-_brStep;
}
}
} else if (_brLvl>=_brMin){ // last breath out, stopping
_brLvl=_brLvl-_brStep;
if(_debugOn){ // send message over serial
Serial.print("Breath_stop: ");
}
if(_brLvl<=_brMin){ // write 0 to LED
_brLvl=0;
analogWrite(LED_BUILTIN, _brLvl);
_breathingIn=true; // reset for new breath
}
}
if(_debugOn&&_brLvl>=_brMin){ // send message over serial
Serial.print("breathingIn ");
Serial.print(_breathingIn);
Serial.print(" brLvl ");
Serial.print(_brLvl);
Serial.print(" uptime(ms) ");
Serial.println(millis());
}
if(_brLvl>0){ // if not writing 0
analogWrite(LED_BUILTIN, _brLvl);
_lstbrUpdateTime=_brCurTime; // record last updated time
}
}
}
void Breathe::stop(){
_breathing=false;
}
Breathe.ino
#include <Breathe.h> // include the breathe library
Breathe breathing(true,LED_BUILTIN,1,155,50,5); // start breathing (byte debugging true/false 1/0, int LED pin, byte min brightness (use >1), byte max brightness, byte update every ms, byte move by 5)
void setup(){
Serial.begin(115200); // start serial monitor for USB debugging
}
void loop(){
breathing.maintain(); // keep breathing, must be called repeatedly
if(millis()==5000){
breathing._debugOn=false; // flip debugging on/off while breathing
}
if(millis()==10000){
breathing.stop(); // stop breathing
}
}
Breathe.zip (2.54 KB)