-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
170 lines (139 loc) · 5.01 KB
/
main.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright 2017 Stefan Daniel Homfeld, www.stedaho.de */
#include "ChannelHandler.h"
#include "TimeTables.h"
#include "Wire.h"
#include "uRTCLib.h"
/* Pin assignment */
const int pinWhite = 5;
const int pinRed = 9;
const int pinBlue = 6;
const int pinButton = 7;
/* Instances of channel handlers */
ChannelHandler whiteChannel(valueTableWhite);
ChannelHandler redChannel(valueTableRed);
ChannelHandler blueChannel(valueTableBlue);
/* Instance of the realtime clock */
uRTCLib rtc;
bool blinkerState = true;
long currentTime = 0;
volatile bool simulationRunning = false;
unsigned long previousMillis = 0;
/* Initial configuration */
void setup() {
/* Configuration of inputs and outputs */
pinMode(pinWhite, OUTPUT);
pinMode(pinRed, OUTPUT);
pinMode(pinBlue, OUTPUT);
pinMode(pinButton, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
/* Set initial values */
analogWrite(pinWhite, 0);
analogWrite(pinRed, 0);
analogWrite(pinBlue, 0);
/* Configure serial interface */
delay(1000);
Serial.begin(57600);
delay(500);
/* Eventually set time */
// rtc.set(0, 0, 22, 6, 25, 8, 17); // RTCLib::set(second, minute, hour, dayOfWeek, dayOfMonth, month, year)
Serial.println("TankControl started!");
}
/* The loop */
void loop() {
unsigned long currentMillis = millis();
/* Check if the loop has to run and determine current time - either simulated or from rtc */
if (simulationRunning) {
if (currentMillis - previousMillis < 5) {
return;
}
/* In simulating mode the time ist increased by 15 seconds per step */
currentTime += MINUTE / 4;
currentTime %= (24 * HOUR);
} else {
if (currentMillis - previousMillis < 1000) {
return;
}
/* Get the time from the attached real time clock */
rtc.refresh();
/* In case we did not get a valid time from the RTC we return and try again in the next cycle */
if (rtc.year() == 0) {
return;
}
currentTime = rtc.hour() * HOUR + rtc.minute() * MINUTE + rtc.second();
}
/* Save the time of the last run */
previousMillis = currentMillis;
/* Determine if we are in maintenance mode */
bool inMaintenance = (digitalRead(pinButton) == HIGH);
/* Blink status LED */
digitalWrite(LED_BUILTIN, blinkerState);
blinkerState = !blinkerState;
/* Override LED blinking and print out debug message if we are in maintenance mode */
if (inMaintenance) {
digitalWrite(LED_BUILTIN, true);
Serial.println("Maintenance mode active");
}
SetLedOutput(currentTime, inMaintenance);
}
/* Handles the incoming serial data event */
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == 's') { /* 's' activates simulation mode */
simulationRunning = true;
} else if (inChar == 'r') { /* 'r' enables real time oparation (default) */
simulationRunning = false;
}
}
}
/* Set the LED output for the given time */
void SetLedOutput(long timestamp, bool inMaintenance) {
/* Get LED values for the given time */
float percentageValueWhite = whiteChannel.GetPercentageAtTime(timestamp);
float percentageValueRed = redChannel.GetPercentageAtTime(timestamp);
float percentageValueBlue = blueChannel.GetPercentageAtTime(timestamp);
/* Override values if we are in maintenance mode */
if (inMaintenance) {
percentageValueWhite = maintenancePercentageValue;
percentageValueRed = maintenancePercentageValue;
percentageValueBlue = maintenancePercentageValue;
}
/* Convert percentage values to raw PWM values */
int rawValueWhite = GetRawFromPercentage(GammaCorrect(percentageValueWhite));
int rawValueRed = GetRawFromPercentage(GammaCorrect(percentageValueRed));
int rawValueBlue = GetRawFromPercentage(GammaCorrect(percentageValueBlue));
/* Set PWM values to the LED outputs */
analogWrite(pinWhite, rawValueWhite);
analogWrite(pinRed, rawValueRed);
analogWrite(pinBlue, rawValueBlue);
PrintSerialOutput(currentTime, percentageValueWhite, percentageValueRed, percentageValueBlue);
}
/* Performs a simple gamma correction of the given percentage value */
float GammaCorrect(float percentageValue) {
return percentageValue * percentageValue / 100;
// return pow(percentageValue, 2.7) / 2512;
}
/* Converts the percentage value into a raw PWM value */
int GetRawFromPercentage(float percentageValue) {
int rawValue = percentageValue * 2.55 + 0.5;
/* Ensure that we have a guaranteed output if the percentage value is greater zero */
if (percentageValue > 0 && rawValue == 0) {
rawValue = 1;
}
return rawValue;
}
/* Prints the current time and values */
void PrintSerialOutput(long timer, float vWhite, float vRed, float vBlue) {
char output[60];
sprintf(output, "[%02d:%02d:%02d] White: %02d.%01d%% Red: %02d.%01d%% Blue: %02d.%01d%%",
(int)(timer / HOUR),
(int)((timer % HOUR) / MINUTE),
(int)(timer % MINUTE),
(int)vWhite,
(int)(vWhite * 10) % 10,
(int)vRed,
(int)(vRed * 10) % 10,
(int)vBlue,
(int)(vBlue * 10) % 10);
Serial.println(output);
}