-
Notifications
You must be signed in to change notification settings - Fork 4
/
LEDEffect.cpp
129 lines (119 loc) · 2.67 KB
/
LEDEffect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
LEDEffect.cpp - Library for LED Effecs.
Created by Harrison H. Jones, October 3, 2014.
*/
#if defined (SPARK)
#include "application.h"
#else
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#endif
#include "LEDEffect.h"
LEDEffect::LEDEffect(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
_time = millis();
_brightness = 125;
_fadeAmount = 5; // how many points to fade the LED by
_fadeDirection = _fadeAmount;
_ledState = 0; // 0 = off, 1 = on, 2 = breath, 3 = fade down, 4 = fade up, 5 = blink
_ledDelay = 30; // in ms
}
void LEDEffect::update()
{
if((millis() - _time) > _ledDelay)
{
_time = millis();
if(_ledState == 0)
_brightness = 0;
else if(_ledState == 1)
_brightness = 255;
else if (_ledState == 2)
{
// change the _brightness for next time through the loop:
_brightness = _brightness + _fadeDirection;
// reverse the direction of the fading at the ends of the fade:
if (_brightness == 0)
_fadeDirection = _fadeAmount;
else if (_brightness == 255)
_fadeDirection = -_fadeAmount;
}
else if (_ledState == 3) // Fade down
{
// change the _brightness for next time through the loop:
_brightness = _brightness - _fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (_brightness == 0)
_brightness = 255;
}
else if (_ledState == 4) // Fade up
{
// change the _brightness for next time through the loop:
_brightness = _brightness + _fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (_brightness == 255)
_brightness = 0;
}
else if(_ledState == 5)
{
if(_brightness == 255)
_brightness = 0;
else
_brightness = 255;
}
else if(_ledState == 6) // Dim
{
}
}
analogWrite(_pin, _brightness);
}
void LEDEffect::off()
{
_ledState = 0;
_ledDelay = 10;
}
void LEDEffect::on()
{
_ledState = 1;
_ledDelay = 10;
}
void LEDEffect::breath(int ledDelay)
{
if(_brightness == 0)
_fadeDirection = _fadeAmount;
else if(_brightness == 255)
_fadeDirection = -_fadeAmount;
_ledState = 2;
_ledDelay = ledDelay;
}
void LEDEffect::fadeDown(int ledDelay)
{
if(_brightness == 0)
_brightness = 255;
_fadeDirection = -_fadeAmount;
_ledState = 3;
_ledDelay = ledDelay;
}
void LEDEffect::fadeUp(int ledDelay)
{
if(_brightness == 255)
_brightness = 0;
_fadeDirection = _fadeAmount;
_ledState = 4;
_ledDelay = ledDelay;
}
void LEDEffect::blink(int ledDelay)
{
_ledState = 5;
_ledDelay = ledDelay;
}
void LEDEffect::dim(unsigned char brightness)
{
_ledState = 6;
_brightness = brightness;
_ledDelay = 1000; // Not really required.
}