Created
January 15, 2018 17:59
-
-
Save serega6531/c865976e96aea2703647ce9b455426eb to your computer and use it in GitHub Desktop.
Библиотека для управления светодиодами в Arduino
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
#include "Arduino.h" | |
#include "LED.h" | |
LED::LED(uint8_t pin) { | |
pinMode(pin, OUTPUT); | |
ledPin = pin; | |
ledState = NONE; | |
} | |
void LED::tick() { | |
if (ledState == NONE) | |
return; | |
if (millis() > startTime + changeInterval) { | |
startTime = millis(); | |
switch (ledState) { | |
case ON_FOR_TIME: | |
currentBrightness = 0; | |
digitalWrite(ledPin, LOW); | |
ledState = NONE; | |
break; | |
case BLINK: | |
currentBrightness = 255 - currentBrightness; | |
analogWrite(ledPin, currentBrightness); | |
break; | |
case BREATH: | |
currentBrightness = constrain(currentBrightness + changeSpeed, 0, 255); | |
analogWrite(ledState, currentBrightness); | |
if (currentBrightness == 0 || currentBrightness == 255) { | |
changeSpeed = -changeSpeed; | |
} | |
break; | |
case FADE: | |
currentBrightness = constrain(currentBrightness + changeSpeed, 0, 255); | |
analogWrite(ledState, currentBrightness); | |
if (currentBrightness == 0 || currentBrightness == 255) { | |
ledState = NONE; | |
break; | |
} | |
break; | |
case NONE: | |
return; | |
} | |
} | |
} | |
void LED::toggle() { | |
ledState = NONE; | |
currentBrightness = 255 - currentBrightness; | |
analogWrite(ledPin, currentBrightness); | |
} | |
void LED::off() { | |
ledState = NONE; | |
currentBrightness = 0; | |
digitalWrite(ledPin, LOW); | |
} | |
void LED::on() { | |
ledState = NONE; | |
currentBrightness = 255; | |
digitalWrite(ledPin, HIGH); | |
} | |
void LED::dim(unsigned char brightness) { | |
ledState = NONE; | |
currentBrightness = brightness; | |
analogWrite(ledPin, currentBrightness); | |
} | |
void LED::on(int time) { | |
ledState = ON_FOR_TIME; | |
currentBrightness = 255; | |
digitalWrite(ledPin, HIGH); | |
changeInterval = time; | |
startTime = millis(); | |
} | |
void LED::blink(int interval) { | |
ledState = BLINK; | |
startTime = millis(); | |
changeInterval = interval; | |
currentBrightness = 255; | |
digitalWrite(ledPin, HIGH); | |
} | |
void LED::breath(int speed, int interval) { | |
ledState = BREATH; | |
changeSpeed = speed; | |
changeInterval = interval; | |
startTime = millis(); | |
} | |
void LED::fade(int speed, int interval) { | |
ledState = FADE; | |
changeSpeed = speed; | |
changeInterval = interval; | |
startTime = 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
#ifndef LED_h | |
#define LED_h | |
#include "Arduino.h" | |
enum LEDAction { | |
NONE, | |
ON_FOR_TIME, | |
BREATH, | |
FADE, | |
BLINK | |
}; | |
class LED | |
{ | |
public: | |
LED(uint8_t pin); | |
void tick(); | |
void toggle(); | |
void off(); | |
void on(); | |
void on(int time); | |
void breath(int speed, int interval); | |
void fade(int speed, int interval); // speed >0 for fade up, <0 for fade down | |
void blink(int interval); | |
void dim(unsigned char brightness); | |
private: | |
uint8_t ledPin; | |
int currentBrightness; | |
int changeSpeed; | |
int changeInterval; | |
LEDAction ledState; | |
unsigned long startTime; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment