forked from clearwater/gaugette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED.cpp
executable file
·43 lines (40 loc) · 974 Bytes
/
LED.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
#include <Arduino.h>
#include "LED.h"
LED::LED(unsigned char pin)
{
this->pin = pin;
this->time0 = 0;
this->target = 0;
this->value = 0;
this->period = 0; // instant
pinMode(pin, OUTPUT);
analogWrite(pin, 0);
}
void LED::set(unsigned char value)
{
if (this->period==0) {
this->value = this->target = value;
analogWrite(pin, this->value);
} else {
this->time0 = millis();
this->target = value;
}
}
void LED::update()
{
if (this->value != this->target) {
unsigned long delta = millis() - time0;
unsigned char newValue;
if (delta>=period) {
Serial.print(delta);
Serial.println(" done");
newValue = this->value = this->target; // done
} else {
int dv = (int)target - (int)value;
// cast to long because dv * d will exceed 16-bit signed int.
int dvt = (long)dv * (long)delta / (long)period;
newValue = (int)value + dvt;
}
analogWrite(pin, newValue);
}
}