Skip to content

Commit b157b96

Browse files
Official Release
0 parents  commit b157b96

14 files changed

Lines changed: 898 additions & 0 deletions

File tree

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## LED Library for Arduino - ezLED
2+
This library is designed for Arduino, ESP32, ESP8266... to control LED: on, off, toggle, fade in/out, blink, blink the number of times, blink in a period of time. It is designed for not only beginners but also experienced users.
3+
4+
**ezLED** stands for **easy LED**, which mean that the library is easy to use.
5+
6+
Features
7+
----------------------------
8+
* Turn on/off
9+
* Toggle between on and off
10+
* Fade in/out
11+
* Blink
12+
* Blink with the number of times
13+
* Blink in a period of time
14+
* Cancel the blinking or fading anytime
15+
* Support both control modes: CTRL_ANODE and CTRL_CATHODE
16+
* Get the on/off LED's states: LED_OFF, LED_ON
17+
* Get the operation LED's state: LED_IDLE, LED_DELAY, LED_FADING, LED_BLINKING
18+
* All functions are non-blocking (without using delay() function)
19+
* Easy to use with multiple LEDs
20+
21+
Available Functions
22+
----------------------------
23+
* ezLED(int pin)
24+
* ezLED(int pin, int mode)
25+
* void turnON()
26+
* void turnON(unsigned long delayTime)
27+
* void turnOFF()
28+
* void turnOFF(unsigned long delayTime)
29+
* void toggle()
30+
* void toggle(unsigned long delayTime)
31+
* void fade(int fadeFrom, int fadeTo, unsigned long fadeTime)
32+
* void fade(int fadeFrom, int fadeTo, unsigned long fadeTime, unsigned long delayTime)
33+
* void blink(unsigned long onTime, unsigned long offTime)
34+
* void blink(unsigned long onTime, unsigned long offTime, unsigned long delayTime)
35+
* void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime)
36+
* void blinkInPeriod(unsigned long onTime, unsigned long offTime, unsigned long blinkTime, unsigned long delayTime)
37+
* void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes)
38+
* void blinkNumberOfTimes(unsigned long onTime, unsigned long offTime, unsigned int numberOfTimes, unsigned long delayTime)
39+
* void cancel(void)
40+
* int getOnOff(void)
41+
* int getState(void)
42+
* void loop(void)
43+
44+
45+
Available Examples
46+
----------------------------
47+
* [LED Blink](https://arduinogetstarted.com/library/led/example/arduino-led-blink)
48+
* [LED Blink In Period](https://arduinogetstarted.com/library/led/example/arduino-led-blink-in-period)
49+
* [LED Blink Number Of Times](https://arduinogetstarted.com/library/led/example/arduino-led-blink-number-of-times)
50+
* [LED Fade In Fade Out](https://arduinogetstarted.com/library/led/example/arduino-led-fade-in-fade-out)
51+
* [LED On Off](https://arduinogetstarted.com/library/led/example/arduino-led-on-off)
52+
* [LED Toggle](https://arduinogetstarted.com/library/led/example/arduino-led-toggle)
53+
* [Multiple LED](https://arduinogetstarted.com/library/led/example/arduino-multiple-led)
54+
* [LED Array](https://arduinogetstarted.com/library/led/example/arduino-led-array)
55+
56+
57+
58+
How To Install the Library
59+
----------------------------
60+
* [ezLED Library Installization Guide](https://arduinogetstarted.com/tutorials/arduino-led-library)
61+
62+
References
63+
----------------------------
64+
* [ezLED Library Reference](https://arduinogetstarted.com/tutorials/arduino-led-library)

examples/LEDArray/LEDArray.ino

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-array
7+
8+
This example blinks 3 LED:
9+
+ blink one LED forever
10+
+ blink one LED in 5 seconds
11+
+ blink one LED in 10 times
12+
+ without using delay() function. This is a non-blocking example
13+
*/
14+
15+
#include <ezLED.h> // ezLED library
16+
17+
#define NUM_LED 3 // three LEDs
18+
19+
#define PIN_LED_1 7
20+
#define PIN_LED_2 8
21+
#define PIN_LED_3 9
22+
23+
ezLED ledArray[NUM_LED] = {
24+
ezLED(PIN_LED_1), // create ezLED object that attach to pin PIN_LED_1
25+
ezLED(PIN_LED_2), // create ezLED object that attach to pin PIN_LED_2
26+
ezLED(PIN_LED_3) // create ezLED object that attach to pin PIN_LED_3
27+
};
28+
29+
void setup() {
30+
Serial.begin(9600);
31+
32+
ledArray[0].blink(500, 500); // 500ms ON, 500ms OFF, blink immediately
33+
ledArray[1].blinkInPeriod(100, 100, 5000); // 100ms ON, 100ms OFF, blink in 5 seconds, blink immediately
34+
ledArray[2].blinkNumberOfTimes(250, 750, 10); // 250ms ON, 750ms OFF, repeat 10 times, blink immediately
35+
}
36+
37+
void loop() {
38+
for (int i = 0; i < NUM_LED; i++)
39+
ledArray[i].loop(); // MUST call the led.loop() function in loop()
40+
41+
// print the operation state
42+
for (int i = 0; i < NUM_LED; i++) {
43+
Serial.print("LED ");
44+
Serial.print(i + 1);
45+
46+
if (ledArray[i].getState() == LED_DELAY)
47+
Serial.println(" DELAYING");
48+
else if (ledArray[i].getState() == LED_BLINKING)
49+
Serial.println(" BLINKING");
50+
else if (ledArray[i].getState() == LED_IDLE)
51+
Serial.println(" BLINK ENDED");
52+
}
53+
54+
// DO SOMETHING HERE
55+
}

examples/LEDBlink/LEDBlink.ino

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-blink
7+
8+
This example blinks LED:
9+
+ blink led
10+
+ without using delay() function. This is a non-blocking example
11+
*/
12+
13+
#include <ezLED.h> // ezLED library
14+
15+
ezLED led(9); // create a LED object that attach to pin 9
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
led.blink(250, 750); // 250ms ON, 750ms OFF, blink immediately
20+
//led.blink(250, 750, 1000); // 250ms ON, 750ms OFF, blink after 1 second
21+
}
22+
23+
void loop() {
24+
led.loop(); // MUST call the led.loop() function in loop()
25+
26+
if (led.getState() == LED_BLINKING)
27+
Serial.println("BLINKING");
28+
else if (led.getState() == LED_IDLE)
29+
Serial.println("BLINK ENDED");
30+
31+
// To stop blinking immediately, call led.cancel() function
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-blink-in-period
7+
8+
This example blinks LED:
9+
+ blink 10 seconds
10+
+ without using delay() function. This is a non-blocking example
11+
*/
12+
13+
#include <ezLED.h> // ezLED library
14+
15+
ezLED led(9); // create a LED object that attach to pin 9
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
led.blinkInPeriod(250, 750, 10000); // 250ms ON, 750ms OFF, blink in 10 seconds, blink immediately
20+
//led.blinkInPeriod(250, 750, 10000, 1000); // 250ms ON, 750ms OFF, blink in 10 seconds, blink after 1 second
21+
}
22+
23+
void loop() {
24+
led.loop(); // MUST call the led.loop() function in loop()
25+
26+
if (led.getState() == LED_BLINKING)
27+
Serial.println("BLINKING");
28+
else if (led.getState() == LED_IDLE)
29+
Serial.println("BLINK ENDED");
30+
31+
32+
// To stop blinking immediately, call led.cancel() function
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-blink-number-of-times
7+
8+
This example blinks an LED:
9+
+ blink LED 10 times and then off
10+
+ without using delay() function. This is a non-blocking example
11+
*/
12+
13+
#include <ezLED.h> // ezLED library
14+
15+
ezLED led(9); // create a LED object that attach to pin 9
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
led.blinkNumberOfTimes(250, 750, 10); // 250ms ON, 750ms OFF, repeat 10 times, blink immediately
20+
//led.blinkNumberOfTimes(250, 750, 10, 1000); // 250ms ON, 750ms OFF, repeat 10 times, blink after 1 second
21+
}
22+
23+
void loop() {
24+
led.loop(); // MUST call the led.loop() function in loop()
25+
26+
if (led.getState() == LED_BLINKING)
27+
Serial.println("BLINKING");
28+
else if (led.getState() == LED_IDLE)
29+
Serial.println("BLINK ENDED");
30+
31+
// To stop blinking immediately, call led.cancel() function
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-fade-in-fade-out
7+
8+
This example fade LED:
9+
+ fade in LED in 3 seconds
10+
+ fade out LED in 3 seconds
11+
+ without using delay() function. This is a non-blocking example
12+
*/
13+
14+
#include <ezLED.h> // ezLED library
15+
16+
ezLED led(9); // create a LED object that attach to pin 9
17+
bool isFadedIn = false;
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
}
22+
23+
void loop() {
24+
led.loop(); // MUST call the led1.loop() function in loop()
25+
26+
if (led.getState() == LED_IDLE) {
27+
if (isFadedIn == false) {
28+
Serial.println("FADING IN");
29+
led.fade(0, 255, 3000); // fade in from 0 to 255 in 3000ms, fade immediately
30+
//led.fade(0, 255, 3000, 1000); // fade in from 0 to 255 in 3000ms, fade after 1 second
31+
isFadedIn = true;
32+
} else {
33+
Serial.println("FADING OUT");
34+
led.fade(255, 0, 3000); // fade out from 255 to 0 in 3000ms, fade immediately
35+
//led.fade(255, 0, 3000, 1000); // fade out from 255 to 0 in 3000ms, fade after 1 second
36+
isFadedIn = false;
37+
}
38+
}
39+
40+
// To stop fading immediately, call led.cancel() function
41+
}

examples/LEDOnOff/LEDOnOff.ino

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-on-off
7+
8+
This example turn ON/OFF led according to state of button.
9+
*/
10+
11+
#include <ezLED.h> // ezLED library
12+
13+
#define BUTTON_PIN 7
14+
15+
ezLED led(3); // create a LED object that attach to pin 3
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
pinMode(BUTTON_PIN, INPUT_PULLUP);
20+
}
21+
22+
void loop() {
23+
led.loop(); // MUST call the led.loop() function in loop()
24+
25+
int buttonState = digitalRead(BUTTON_PIN);
26+
27+
if (buttonState == LOW) {
28+
led.turnON(); // turn on immediately
29+
//led.turnON(1000); // turn on after 1 second
30+
} else {
31+
led.turnOFF(); // turn off immediately
32+
//led.turnOFF(1000); // turn off after 1 second
33+
}
34+
35+
if (led.getOnOff() == LED_ON)
36+
Serial.println("LED is ON");
37+
else
38+
Serial.println("LED is OFF");
39+
}

examples/LEDToggle/LEDToggle.ino

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Created by ArduinoGetStarted.com
3+
4+
This example code is in the public domain
5+
6+
Tutorial page: https://arduinogetstarted.com/library/led/example/arduino-led-toggle
7+
8+
This example turn ON/OFF led according to state of button.
9+
*/
10+
11+
#include <ezButton.h> // ezButton library https://arduinogetstarted.com/tutorials/arduino-button-library
12+
#include <ezLED.h> // ezLED library
13+
14+
ezButton button(7); // create a button object that attach to pin 7
15+
ezLED led(3); // create a LED object that attach to pin 3
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
button.setDebounceTime(50); // set debounce time to 50ms
20+
}
21+
22+
void loop() {
23+
button.loop(); // MUST call the loop() function first
24+
led.loop(); // MUST call the loop() function first
25+
26+
if (button.isPressed()) {
27+
led.toggle(); // toggle immediately
28+
//led.toggle(1000); // toggle after 1 second
29+
}
30+
31+
if (led.getOnOff() == LED_ON)
32+
Serial.println("LED is ON");
33+
else
34+
Serial.println("LED is OFF");
35+
}

0 commit comments

Comments
 (0)