1 #include <LightChrono.h>
4 const float APPARENT_SHUNT_RESISTANCE = 0.453;
5 const float VOLTAGE_REFERENCE = 2.494;
10 const int digits[] = {2, 3, 4, 5};
11 const int segments[] = {6, 7, 8, 9, 10, 11, 12, 13};
13 const bool one[7] = {false, true, true, false, false, false, false};
14 const bool two[7] = {true, true, false, true, true, false, true};
15 const bool three[7] = {true, true, true, true, false, false, true};
16 const bool four[7] = {false, true, true, false, false, true, true};
17 const bool five[7] = {true, false, true, true, false, true, true};
18 const bool six[7] = {true, false, true, true, true, true, true};
19 const bool seven[7] = {true, true, true, false, false, false, false};
20 const bool eight[7] = {true, true, true, true, true, true, true};
21 const bool nine[7] = {true, true, true, true, false, true, true};
22 const bool zero[7] = {true, true, true, true, true, true, false};
24 const bool U[7] = {false, true, true, true, true, true, false};
25 const bool I[7] = {false, false, true, false, false, false, false};
26 const bool P[7] = {true, true, false, false, true, true, true};
28 unsigned char display_state = 0;
34 void writeDigit(char d, char val, bool dec) {
68 for (int i = 0; i < 7; ++i) {
70 digitalWrite(segments[i], HIGH);
72 digitalWrite(segments[i], LOW);
76 digitalWrite(15, HIGH);
78 digitalWrite(digits[d], LOW);
80 digitalWrite(digits[d], HIGH);
81 digitalWrite(15, LOW);
84 void writeLetter(char d, const bool val[]) {
85 for (int i = 0; i < 7; ++i) {
87 digitalWrite(segments[i], HIGH);
89 digitalWrite(segments[i], LOW);
92 digitalWrite(digits[d], LOW);
94 digitalWrite(digits[d], HIGH);
97 void showNumber(float number) {
99 writeDigit(0, (n/1000U) % 10, false);
100 writeDigit(1, (n/100U) % 10, false);
101 writeDigit(2, (n/10U) % 10, false);
102 writeDigit(3, n % 10, false);
105 void showVoltage(float volts) {
106 int cvolts = volts * 100;
107 writeDigit(0, (cvolts/100U) % 10, true);
108 writeDigit(1, (cvolts/10U) % 10, false);
109 writeDigit(2, cvolts % 10, false);
112 void showCurrent(float current) {
113 int camp = current * 100;
114 writeDigit(0, (camp/100U) % 10, true);
115 writeDigit(1, (camp/10U) % 10, false);
116 writeDigit(2, camp % 10, false);
122 // put your setup code here, to run once:
124 analogReference(EXTERNAL);
126 for (int i = 0; i < 4; ++i) {
127 pinMode(digits[i], OUTPUT);
128 digitalWrite(digits[i], HIGH);
130 for (int i = 0; i < 8; ++i) {
131 pinMode(segments[i], OUTPUT);
132 digitalWrite(segments[i], LOW);
136 digitalWrite(15, LOW);
146 float battery_voltage;
147 float amps_in_period;
148 float watts_in_period;
152 if (timer.hasPassed(1000)) {
153 unsigned long passed = timer.elapsed();
155 battery_voltage = readVoltage(A0);
157 if (battery_voltage < 0.8) {
161 shunt_voltage = readVoltage(A1);
162 shunt_current = shunt_voltage/APPARENT_SHUNT_RESISTANCE;
165 amps_in_period = shunt_current * passed/1000;
166 watts_in_period = amps_in_period * battery_voltage;
168 ampseconds += amps_in_period;
169 wattseconds += watts_in_period;
170 mamphours = ampseconds/3.6;
171 mwatthours = wattseconds/3.6;
174 if (debounce.hasPassed(300) && !digitalRead(19)) {
175 display_state = (display_state + 1) % 4;
176 Serial.println(display_state);
179 switch (display_state) {
181 showNumber(mamphours);
184 showVoltage(battery_voltage);
187 showCurrent(shunt_current);
190 showNumber(mwatthours);
197 float readVoltage(int pin) {
200 for (int i = 0; i < 4; ++i) {
201 val = val + analogRead(pin);
205 return (val / 4) * VOLTAGE_REFERENCE/1023.0;