Last active
May 21, 2017 06:33
-
-
Save s-celles/ffd3085238b95b21df2dccfeaba5892a to your computer and use it in GitHub Desktop.
Arduino Led blink without delay (with millis)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define led_pin 11 | |
#define but_pin 2 | |
class Led { | |
public: | |
Led(int pin, int state); | |
void settings(); | |
void on(); | |
void off(); | |
void toggle(); | |
void blink(unsigned long delay_on, unsigned long delay_off, int times); | |
void stop(); | |
void update(); | |
void update(unsigned long time); | |
private: | |
bool m_state; | |
bool m_state_init; | |
int m_pin; | |
int m_blinking; | |
unsigned long m_time_next; | |
unsigned long m_delay_on; | |
unsigned long m_delay_off; | |
}; | |
Led::Led(int pin, int state=0) | |
{ | |
this->m_state = state; | |
this->m_state_init = state; | |
this->m_state = !this->m_state; | |
this->m_pin = pin; | |
this->m_blinking = 0; // 0=no blinking -1:blinking foreever 1:blinking one time ... | |
} | |
void Led::settings() | |
{ | |
pinMode(this->m_pin, OUTPUT); | |
} | |
void Led::on() | |
{ | |
this->m_state = true; | |
} | |
void Led::off() | |
{ | |
this->m_state = false; | |
} | |
void Led::toggle() | |
{ | |
this->m_state = !this->m_state; | |
} | |
void Led::blink(unsigned long delay_on = 1000, unsigned long delay_off = 1000, int times=-1) | |
{ | |
if (times>0) { | |
this->m_blinking = 2*times+1; | |
} else { // -1=foreever | |
this->m_blinking = times; | |
} | |
this->m_delay_on = delay_on; | |
this->m_delay_off = delay_off; | |
this->m_state_init = this->m_state; | |
} | |
void Led::stop() | |
{ | |
this->m_blinking = 0; | |
this->m_state = this->m_state_init; | |
} | |
void Led::update() | |
{ | |
unsigned long time = millis(); | |
this->update(time); | |
} | |
void Led::update(unsigned long time) | |
{ | |
if (this->m_blinking != 0) { | |
if (time >= this->m_time_next) { | |
this->m_state = !this->m_state; | |
if (this->m_state) { | |
this->m_time_next = time + this->m_delay_on; | |
} else { | |
this->m_time_next = time + this->m_delay_off; | |
} | |
if (this->m_blinking > 0) { | |
this->m_blinking = this->m_blinking - 1; | |
} | |
} | |
} | |
digitalWrite(this->m_pin, (this->m_state) ? HIGH : LOW); | |
} | |
Led myLed(led_pin); | |
unsigned long time; | |
volatile bool recording = LOW; | |
void setup() | |
{ | |
Serial.begin(9600); | |
myLed.settings(); | |
//myLed.blink(200, 200, 3); | |
//pinMode(but_pin, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(but_pin), on_start_stop, CHANGE); | |
recording = false; | |
} | |
void on_start_stop() | |
{ | |
recording = !recording; | |
if (recording) { | |
Serial.println("Start recording"); | |
myLed.blink(200, 200); | |
} else { | |
Serial.println("Stop recording"); | |
myLed.stop(); | |
} | |
} | |
void loop() | |
{ | |
time = millis(); | |
//Serial.println("in the loop!"); | |
myLed.update(); | |
//myLed.update(time); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related schematics and simulation
https://circuits.io/circuits/5006415-blink-led-without-delay/
ToDo
lit_state
(state to light on) - default should be true->