-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDManager.cpp
More file actions
62 lines (53 loc) · 1.42 KB
/
LEDManager.cpp
File metadata and controls
62 lines (53 loc) · 1.42 KB
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
#include "LEDManager.h"
LEDManager::LEDManager(int pin):pin(pin){
pinMode(pin, OUTPUT);
}
void LEDManager::setMode(LEDMode mode, int maxBrightness, unsigned long cycleTime){
analogWrite(pin, 0);
startedCycle = false;
this->cycleTime = cycleTime;
this->mode = mode;
this->maxBrightness = maxBrightness;
}
void LEDManager::update(unsigned long currentTime){
if(!startedCycle){
startedCycle = true;
startTime = currentTime;
}
unsigned long timeElapsed = currentTime - startTime;
if(timeElapsed >= cycleTime){
startedCycle = false;
}
float currentBrightness;
switch(mode)
{
case SOLID:
analogWrite(pin, maxBrightness);
break;
//Blink mode spends half its time on, and half off.
case BLINK:
if(timeElapsed <= (cycleTime/2))
analogWrite(pin, maxBrightness);
else
analogWrite(pin, 0);
break;
case FADE_IN:
//brightness is determined by a linear function of time
currentBrightness = maxBrightness * (float(timeElapsed)/float(cycleTime));
// Serial.println(currentBrightness);
analogWrite(pin, int(currentBrightness));
break;
case FADE:
//could use functions for this
if(timeElapsed <= (cycleTime/2)){
currentBrightness = maxBrightness * (float(timeElapsed)/float(cycleTime));
}
else
{
currentBrightness = maxBrightness * (1.0 - (float(timeElapsed)/float(cycleTime)));
}
analogWrite(pin, int(currentBrightness));
break;
}
previousTime = currentTime;
}