Arduino: LED Library Code

Download LED_Lib.ino from DropBox or copy the code from the page below.

Sketch: LED_Library.ino

//include our header
#include "LED_Lib.h"

//declare instance and Pass Pin number
LEDLIB LED1 = LEDLIB(13); //pin 13 is not pwm will work for demonstration.

void setup() {

}

void loop() {
LED1.BLINKS(4, 2000); // blink led 4 times in 2000ms
delay(2000);
LED1.FADEON(1000); //led fades on 1000ms
delay(2000);
LED1.FADEOFF(2000); // led fades off 2000ms
delay(2000);
LED1.PULSE(4000); // pulse led 4000ms per pulse
delay(2000);
}

Header: LED_LIB.h


//required to include Arduino.h
#include "Arduino.h"

class LEDLIB
{
public:
LEDLIB(byte pin); //construct
BLINKS(int count, int delayms);
FADEON(int delayms);
FADEOFF(int delayms);
PULSE(int delayms);

private:
byte tmpPIN;
};

CPP: LED_Lib.cpp


// required to include Arduino.h
#include "Arduino.h"
// include our header file
#include "LED_Lib.h"

//function format
//class::function(variables)
LEDLIB::LEDLIB(byte pin)
{
tmpPIN = pin;
pinMode(tmpPIN, OUTPUT);
}

LEDLIB::BLINKS(int count, int delayms)
{
delayms = delayms / 2 / count; //delayms is ms until completion
for (int i = 0; i < count; i++){
digitalWrite(tmpPIN, HIGH);
delay(delayms);
digitalWrite(tmpPIN, LOW);
delay(delayms);
}
}

LEDLIB::FADEON(int delayms)
{
delayms = delayms / 255; //delayms is ms until completion
for (int i = 0; i 0; i--){
analogWrite(tmpPIN, i);
delay(delayms);
}
}

LEDLIB::PULSE(int delayms)
{
delayms = delayms / 2;

//fade on
FADEON(delayms);

//fade off
FADEOFF(delayms);
}