-
Notifications
You must be signed in to change notification settings - Fork 1
/
PitkinController.ino
70 lines (58 loc) · 1.27 KB
/
PitkinController.ino
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
#define switchPin 0
#define pwmPin 1
int powerLevel = 0;
unsigned long lastTime = 0;
bool isFirstActivation = true;
//Output levels for the PWM: 100%, 80%, 60% and 30%
const int output[] = { 255, 204, 153, 77 };
const int lowestLevel = sizeof(output) / sizeof(output[0]) - 1;
void setup()
{
//Use input pullup resistors to filter noise, and switch ground for the signal to the switchPin
pinMode(switchPin, INPUT_PULLUP);
pinMode(pwmPin, OUTPUT);
isFirstActivation = !isSwitchOn();
}
void loop()
{
while (isSwitchOn()) {
unsigned long offTime = millis() - lastTime;
if (offTime > 10000) {
reset();
}
else if (offTime > 100) {
reducePower();
}
outputPowerLevel(powerLevel);
lastTime = millis();
}
//when the switchPin goes high and the while loop exits, immediately turn the PWM off
turnOff();
}
bool isSwitchOn() {
return digitalRead(switchPin) == LOW;
}
bool isLowestLevel(int level) {
return level >= lowestLevel;
}
void reset() {
powerLevel = 0;
}
void outputPowerLevel(int level) {
if (level < 0) {
analogWrite(pwmPin, 0);
return;
}
else {
analogWrite(pwmPin, output[level]);
}
}
void turnOff() {
outputPowerLevel(-1);
}
void reducePower(){
if (!isLowestLevel(powerLevel) && !isFirstActivation) {
powerLevel++;
}
isFirstActivation = false;
}