├── .DS_Store ├── library.properties ├── keywords.txt ├── .github └── FUNDING.yml ├── LICENSE.txt ├── examples └── statusLED │ └── statusLED.ino ├── README.md └── src ├── statusLED.h └── statusLED.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TheDIYGuy999/statusLED/HEAD/.DS_Store -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=statusLED 2 | version=1.8.1 3 | author=TheDIYGuy999 <> 4 | maintainer=TheDIYGuy999 <> 5 | sentence=Allows Arduino boards to generate LED flash sequences without the use of delay() and much more 6 | paragraph=This is an Arduino library for easy LED control: dimming, flash sequences etc. 7 | category=Signal Input/Output 8 | url=https://github.com/TheDIYGuy999/statusLED 9 | architectures=* 10 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For statusLED 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | statusLED KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | begin KEYWORD2 16 | flash KEYWORD2 17 | on KEYWORD2 18 | off KEYWORD2 19 | pwm KEYWORD2 -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: TheDIYGuy999 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: "https://paypal.me/thediyguy999" 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 TheDIYGuy999 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /examples/statusLED/statusLED.ino: -------------------------------------------------------------------------------- 1 | /* 2 | statusLED.h - Library for status LED control (blinking, flash sequences, on, off, pwm) 3 | Created by TheDIYGuy999 May 2016 4 | Released into the public domain. 5 | */ 6 | 7 | #include // TheDIYGuy999 library: https://github.com/TheDIYGuy999/statusLED 8 | 9 | // Status LED objects 10 | statusLED greenLED(false); // False = not inversed 11 | statusLED redLED(false); 12 | 13 | void setup() { 14 | // put your setup code here, to run once: 15 | 16 | 17 | // LED setup 18 | 19 | // AVR platform: 20 | #if defined __AVR_ATmega32U4__ || __AVR_ATmega328P__ 21 | greenLED.begin(5); // Green LED on pin 5 22 | redLED.begin(6); // Red LED on pin 6 23 | 24 | // ESP32 platform: use a separate timer for each LED, but not timer 0, because of millis() 25 | #else 26 | greenLED.begin(33, 2, 31500); // Green LED on pin 33, Timer 2, 31500Hz 27 | redLED.begin(32, 3, 31500); // Green LED on pin 32, Timer 3, 31500Hz 28 | #endif 29 | 30 | // LED demo (1x executed) 31 | redLED.on(); 32 | delay(2000); 33 | greenLED.on(); 34 | delay(1000); 35 | redLED.pwm(10); // PWM capable pins required! 36 | delay(1000); 37 | greenLED.pwm(50); 38 | delay(1000); 39 | redLED.off(); 40 | delay(1000); 41 | greenLED.off(); 42 | delay(1000); 43 | } 44 | 45 | void loop() { 46 | // put your main code here, to run repeatedly: 47 | 48 | // Flashing sequences 49 | redLED.flash(140, 150, 700, 5, 70); // ON, OFF, PAUSE, PULSES, (OPTIONAL DELAY FOR FIRST PASS) 50 | greenLED.flash(40, 350, 0, 0); 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # statusLED Arduino library 2 | 3 | This is an Arduino library for easy LED control: dimming, flash sequences etc. 4 | 5 | ## Features: 6 | - Switching the LED on and off 7 | - Adjusting the brightness, using PWM (PWM capable pin required) 8 | - Blinking the LED 9 | - Flashing sequences (e.g. for visualization of numbers) 10 | - Ideal for light control in RC vehicles 11 | - Inverse mode, if your LED is wired between output pin and vcc 12 | - Works on ATmega328P, ATmega32U4, ESP32 13 | 14 | ## Changes: 15 | - v1.0: Initial commit 16 | - v1.1: ESP32 support added 17 | - v1.2: ESP32 now with 16 PWM channels instead of 8 18 | - v1.3: The .flash() function is now a boolean and returning "true", if the sequence is in start position. Allows to synchronize the indicator sound with the LED. Used in: https://github.com/TheDIYGuy999/Rc_Engine_Sound_ESP32 19 | - v1.4: Experimental support for optional 16bit timer support on ESP32 added. Required for v2.0 of: https://github.com/TheDIYGuy999/Rc_Engine_Sound_ESP32 20 | - v1.5: Optional delay for first flash() pass added. Allows to program offsets between different LED. Required for v2.6 of: https://github.com/TheDIYGuy999/Rc_Engine_Sound_ESP32 21 | - v1.6: Optional ramp (ESP32 only) for flash() and off() added. Allows to switch indicators on and off softly. Required for v5.7 of: https://github.com/TheDIYGuy999/Rc_Engine_Sound_ESP32 22 | - v1.7: Adjustable off brightness for flash() 23 | - v1.8: Arduino 1.5 structure, so it's not detected as "legacy" library anymore 24 | - v1.8.1 potential compiler error solved 25 | 26 | ## Usage 27 | 28 | See [example](https://github.com/TheDIYGuy999/statusLED/blob/master/examples/statusLED/statusLED.ino). 29 | 30 | (c) 2016 - 2021 TheDIYGuy999 31 | -------------------------------------------------------------------------------- /src/statusLED.h: -------------------------------------------------------------------------------- 1 | /* 2 | statusLED.cpp - Library for status LED control (blinking, flash sequences, on, off, pwm) 3 | Created by TheDIYGuy999 May 2016 - April 2021 4 | Released into the public domain. 5 | 6 | Dez. 2019: added ESP32 support 7 | */ 8 | 9 | #ifndef statusLED_h 10 | #define statusLED_h 11 | 12 | #include "Arduino.h" 13 | 14 | // Class definition (header) ======================================================================== 15 | class statusLED { 16 | public: 17 | statusLED(bool inverse); 18 | #if defined __AVR_ATmega32U4__ || __AVR_ATmega328P__ // AVR platform 19 | void begin(int pin1); 20 | #else // ESP32 platform 21 | void begin(int pin1, int channel, int frequency, int resolution = 8); 22 | #endif 23 | 24 | bool flash(unsigned long onDuration, unsigned long offDuration, unsigned long pauseDuration, int pulses, int delay = 0, int bulbSimRamp = 0, int flashOffBrightness = 0); 25 | void on(); 26 | void off(int bulbSimRamp = 0, int offOffBrightness = 0); 27 | void pwm(int brightness); 28 | 29 | private: 30 | int _pin1; 31 | int _frequency; 32 | unsigned long _onDuration; 33 | unsigned long _offDuration; 34 | unsigned long _pauseDuration; 35 | unsigned long _delay; 36 | int _pulses; 37 | int _pulseCnt = 0; 38 | int _channel = 0; 39 | int _brightness; 40 | int _flashBrightness = 0; 41 | int _flashOffBrightness = 0; 42 | int _offBrightness = 0; 43 | int _offOffBrightness = 0; 44 | int _bulbSimRamp; 45 | int _offBulbSimRamp; 46 | unsigned long _previousMillis = 0; 47 | unsigned long _previousFlashRampMillis = 0; 48 | unsigned long _previousOffRampMillis = 0; 49 | byte _state = 0; 50 | bool _inverse; 51 | bool _start; 52 | bool _up; 53 | bool _down; 54 | }; 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /src/statusLED.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | statusLED.cpp - Library for status LED control (blinking, flash sequences, on, off, pwm) 3 | Created by TheDIYGuy999 May 2016 - April 2021 4 | Released into the public domain. 5 | 6 | Dez. 2019: added ESP32 support 7 | Dez. 2019: replaced sigmaDelta function with ledc funvtion, so we can use more 16 instead of 8 timers 8 | */ 9 | 10 | #include "Arduino.h" 11 | #include "statusLED.h" 12 | 13 | 14 | // Member definition (code) ======================================================================== 15 | statusLED::statusLED(bool inverse) { // Constructor (called, when new ojects of that class are created) 16 | _state = 0; 17 | _previousMillis = 0; 18 | _inverse = inverse; 19 | } 20 | 21 | // Begin function ************************************************************ 22 | 23 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ // Atmega Platform 24 | void statusLED::begin(int pin1) { 25 | _pin1 = pin1; 26 | pinMode(_pin1, OUTPUT); 27 | if (_inverse) digitalWrite(_pin1, HIGH); 28 | else digitalWrite(_pin1, LOW); 29 | } 30 | 31 | #else // ESP32 platform (using channel 0 will affect millis()! ) 32 | void statusLED::begin(int pin1, int channel, int frequency, int resolution) { 33 | _pin1 = pin1; 34 | _channel = channel; 35 | _frequency = frequency; 36 | ledcSetup(_channel, _frequency, resolution); // 8 bit resolution, if no input argument 37 | ledcAttachPin(_pin1, _channel); 38 | if (_inverse) ledcWrite(_channel, 255); 39 | else ledcWrite(_channel, 0); 40 | } 41 | #endif 42 | 43 | 44 | // Flash function ************************************************************ 45 | bool statusLED::flash(unsigned long onDuration, unsigned long offDuration, unsigned long pauseDuration, int pulses, int delay, int bulbSimRamp, int flashOffBrightness) { 46 | _onDuration = onDuration; 47 | _offDuration = offDuration; 48 | _pauseDuration = pauseDuration; 49 | _bulbSimRamp = bulbSimRamp; 50 | _pulses = pulses; 51 | _delay = delay; 52 | _flashOffBrightness = flashOffBrightness; 53 | 54 | unsigned long currentMillis = millis(); 55 | 56 | // State machine 57 | switch (_state) { 58 | case 0: //---- Step 0 (do nothing) 59 | _previousMillis = currentMillis; 60 | _state = 1; 61 | break; 62 | 63 | case 1: //---- Step 1 (Start delay for first pass) 64 | if (currentMillis - _previousMillis >= _delay) { 65 | _state = 2; 66 | } 67 | break; 68 | 69 | case 2: //---- Step 2 (LED on) 70 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 71 | if (_inverse) digitalWrite(_pin1, LOW); 72 | else digitalWrite(_pin1, HIGH); 73 | #else 74 | //if (_inverse) ledcWrite(_channel, 0); 75 | //else ledcWrite(_channel, 255); 76 | _up = true; 77 | _down = false; 78 | #endif 79 | _pulseCnt ++; // Increase loop counter 80 | _previousMillis = currentMillis; 81 | _state = 3; 82 | _start = true; 83 | break; 84 | 85 | case 3: //---- Step 3 (ON duration) 86 | if (currentMillis - _previousMillis >= _onDuration) { 87 | _state = 4; 88 | _start = false; 89 | } 90 | break; 91 | 92 | case 4: //---- Step 4 (LED off) 93 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 94 | if (_inverse) digitalWrite(_pin1, HIGH); 95 | else digitalWrite(_pin1, LOW); 96 | #else 97 | //if (_inverse) ledcWrite(_channel, 255); 98 | //else ledcWrite(_channel, 0); 99 | _down = true; 100 | _up = false; 101 | #endif 102 | _previousMillis = currentMillis; 103 | _state = 5; 104 | break; 105 | 106 | case 5: //---- Step 5 (OFF duration) 107 | if (currentMillis - _previousMillis >= _offDuration) { 108 | _state = 6; 109 | } 110 | break; 111 | 112 | case 6: //---- Step 6 (Flash sequence finished?) 113 | if (_pulseCnt >= _pulses) { 114 | _pulseCnt = 0; 115 | _previousMillis = currentMillis; 116 | _state = 7; // Sequence finished 117 | } else { 118 | _state = 2; 119 | } 120 | break; 121 | 122 | case 7: //---- Step 7 (Pause duration) 123 | if (currentMillis - _previousMillis >= _pauseDuration) { 124 | _state = 2; 125 | } 126 | break; 127 | } // End of state machine 128 | 129 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 130 | // No actions 131 | #else 132 | if (bulbSimRamp > 0) { 133 | // Ramp brightness 134 | if (micros() - _previousFlashRampMillis >= _bulbSimRamp) { 135 | _previousFlashRampMillis = micros(); 136 | if (_up && _flashBrightness < 255) { 137 | _flashBrightness ++; 138 | } 139 | if (_flashBrightness == 255 || _down) _up = false; 140 | 141 | if (_down && _flashBrightness > _flashOffBrightness) { 142 | _flashBrightness --; 143 | } 144 | if (_flashBrightness == _flashOffBrightness) _down = false; 145 | } 146 | } 147 | else { 148 | // Change brightness immediately 149 | if (_up) { 150 | _flashBrightness = 255; 151 | _up = false; 152 | } 153 | if (_down) { 154 | _flashBrightness = _flashOffBrightness; 155 | _down = false; 156 | } 157 | } 158 | 159 | // Write brightness 160 | if (_inverse) ledcWrite(_channel, 255 - _flashBrightness); 161 | else ledcWrite(_channel, _flashBrightness); 162 | #endif 163 | 164 | return _start; // Report back, if we are back @ step 0 (added 2020 01 03) 165 | } 166 | 167 | // On function ************************************************************ 168 | void statusLED::on() { 169 | _state = 0; 170 | _pulseCnt = 0; 171 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 172 | if (_inverse) digitalWrite(_pin1, LOW); 173 | else digitalWrite(_pin1, HIGH); 174 | #else 175 | if (_inverse) ledcWrite(_channel, 0); 176 | else ledcWrite(_channel, 255); 177 | #endif 178 | } 179 | 180 | // Off function ************************************************************ 181 | void statusLED::off(int bulbSimRamp, int _offOffBrightness) { 182 | _state = 0; 183 | _pulseCnt = 0; 184 | _offBulbSimRamp = bulbSimRamp; 185 | _offOffBrightness = _offOffBrightness; 186 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 187 | if (_inverse) digitalWrite(_pin1, HIGH); 188 | else digitalWrite(_pin1, LOW); 189 | #else 190 | //if (_inverse) ledcWrite(_channel, 255); 191 | //else ledcWrite(_channel, 0); 192 | if (_offBulbSimRamp > 0) { 193 | // Ramp brightness 194 | if (micros() - _previousOffRampMillis >= _offBulbSimRamp) { 195 | _previousOffRampMillis = micros(); 196 | if (_offBrightness > _offOffBrightness) _offBrightness --; 197 | } 198 | } 199 | else _offBrightness = _offOffBrightness; // Change brightness immediately 200 | 201 | // Write brightness 202 | if (_inverse) ledcWrite(_channel, 255 - _offBrightness); 203 | else ledcWrite(_channel, _offBrightness); 204 | 205 | 206 | #endif 207 | } 208 | 209 | // PWM function ************************************************************ 210 | void statusLED::pwm(int brightness) { 211 | _state = 0; 212 | _pulseCnt = 0; 213 | //_channel = 1; 214 | _brightness = brightness; 215 | 216 | #if defined __AVR_ATmega32U4__ || defined __AVR_ATmega328P__ 217 | if (_inverse) analogWrite(_pin1, 255 - _brightness); 218 | else analogWrite(_pin1, _brightness); 219 | 220 | #else // ESP32 platform (analogWrite not supported) 221 | if (_inverse) ledcWrite(_channel, 255 - _brightness); 222 | else ledcWrite(_channel, _brightness); 223 | 224 | #endif 225 | } 226 | --------------------------------------------------------------------------------