myFirstLibrary, miles out?
we blinky things, , 'breathing' led on projects when fits in..
i've added couple of different things now, decided use make library.
i've got functionality want (i think), i'm not sure i'm going in great way. may have over-complicated tad. spare time share thoughts or tips please?
breathe.h
breathe.cpp
breathe.ino
i've added couple of different things now, decided use make library.
i've got functionality want (i think), i'm not sure i'm going in great way. may have over-complicated tad. spare time share thoughts or tips please?
breathe.h
code: [select]
#include "arduino.h" // let 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 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 increment/decrement brightness each update
byte _breathing; // breathing? (last breath out or stopped if not)
unsigned long _brcurtime; // current time(ms)
unsigned long _lstbrupdatetime; // last time brightness changed
};
#endif
breathe.cpp
code: [select]
#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 on 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 output
analogwrite(_ledpin, _brlvl); // write lowest brightness value
_lstbrupdatetime=_brcurtime; // record time brightness updated
}
void breathe::maintain(){
_brcurtime=millis(); // record current time
if(_brcurtime>=_lstbrupdatetime+_brupdatetime){ // if it's time breathe
if(_breathing){
if(_debugon){ // send message on serial
serial.print("breathe_maintain: ");
}
if(_breathingin){ // brightness increasing
if(_brlvl>=_brmax-_brstep){ // hit end, stop , go back
_brlvl=_brmax;
_breathingin=false;
} else { // keep going
_brlvl=_brlvl+_brstep;
}
} else { // brightness decreasing
if(_brlvl<=_brmin+_brstep){ // hit end, stop , 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 on serial
serial.print("breath_stop: ");
}
if(_brlvl<=_brmin){ // write 0 led
_brlvl=0;
analogwrite(led_builtin, _brlvl);
_breathingin=true; // reset new breath
}
}
if(_debugon&&_brlvl>=_brmin){ // send message on 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
code: [select]
#include <breathe.h> // include 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 5)
void setup(){
serial.begin(115200); // start serial monitor usb debugging
}
void loop(){
breathing.maintain(); // keep breathing, must called repeatedly
if(millis()==5000){
breathing._debugon=false; // flip debugging on/off while breathing
}
if(millis()==10000){
breathing.stop(); // stop breathing
}
}
don't cross-post.
Arduino Forum > Using Arduino > Programming Questions > myFirstLibrary, miles out?
arduino
Comments
Post a Comment