myFirstLibrary, miles out?

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)

Don't cross-post.

If this is just something for your own use you may not need to create a library with the complications that entails . Just put your code in a separate .ino file and put a copy of that file in whichever project you want to use it with.

...R

Other thread removed.

[quote author=Coding Badly link=topic=255780.msg1809625#msg1809625 date=1405700286]

Other thread removed. [/quote]

Thanks, I'd created it in better suited place and somehow missed removing the first.

Robin2: If this is just something for your own use you may not need to create a library with the complications that entails . Just put your code in a separate .ino file and put a copy of that file in whichever project you want to use it with.

...R

It's a learning exercise, it doesn't need to be a library but thanks for the easy approach-it's often exactly what I'd be after.

mad_tunes:
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.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;
}

I guess I have not been on this forum for so long that I am not totally jaded.

I understand why you wrote the library if even as a learning experience as the title suggests it is your first library.

I downloaded it and some of the first things I have noticed is you are using the built in LED which is a digital pin not a analog pin at least on my Duemilanove I am testing this on. Second you have hard coded LED_BUILTIN for your analogWrites in your Breathe.cpp file. Nothing to hard for me to change but wanted to let you know.

I for one don’t think its too bad for a first library. Yea sure it might be a little over the top but hey if you have good examples then I guess its up to the end user to trim it down if they want or to not use it at all.

I think its cool anyway. Even though my vote counts for SQUAT here.

Thanks a lot and well spotted Tekati