-
Notifications
You must be signed in to change notification settings - Fork 1
/
chargesafe.ino
200 lines (177 loc) · 4.19 KB
/
chargesafe.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// include the library code:
#include <LiquidCrystal.h>
#include "Battery.h"
#include "getVoltages.h"
#include "SortBatteries.h"
// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int donePin = 2;
int nextPin = 3;
bool done;
bool next;
const int ARRAYMAX = 10;
int lastIndex;
int displayIndex;
int chargingIndex;
String chargingMessage[2];
bool charging;
Battery Batteries[ARRAYMAX];
// Use one char and letter++?
char letters[26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y', 'Z'};
int state;
double cellArr[4];
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(donePin, INPUT_PULLUP);
pinMode(nextPin, INPUT_PULLUP);
lastIndex = 0;
displayIndex = 0;
chargingIndex = 0;
state = 0;
charging = false;
}
void loop() {
// Buttons
buttonHandler();
// Display
view();
}
void view(){
if(state == 0){
lcd.setCursor(1, 0);
lcd.print("Welcome!" );
lcd.setCursor(1, 1);
lcd.print("Click Done..." );
}
else if(state == 1){
lcd.setCursor(0, 0);
lcd.print("Name = " + String(letters[lastIndex]) );
double voltage = getVoltages(cellArr);
////////////////////////////////////////////////////Serial.println(analogRead(A0));
lcd.setCursor(0,1);
lcd.print(String(voltage) + " V");
lcd.setCursor(11,1);
lcd.print("Save?");
}
else if(state == 2){
// Display current battery
Battery thisBattery = Batteries[displayIndex];
lcd.setCursor(0, 0);
lcd.print("Name: " + thisBattery.getName() );
lcd.setCursor(0,1);
lcd.print(String(thisBattery.getTotal()) + "V");
lcd.setCursor(9,1);
lcd.print("Next ->");
}
else if(state == 3){
lcd.setCursor(0, 0);
if(!charging)
{
lcd.print("Insert Battery " + Batteries[chargingIndex].getName());
lcd.setCursor(9,1);
lcd.print("Next ->");
}
else
{
lcd.print("Charging...");
chargerCheck();
}
}
else if(state == 4){
lcd.setCursor(1, 0);
lcd.print("Finished!" );
lcd.setCursor(1, 1);
lcd.print("Restart?" );
}
}
void buttonHandler(){
// Done Button
if ( digitalRead(donePin) == LOW && digitalRead(donePin) != done )
{
done = digitalRead(donePin);
lcd.clear();
doneHandler();
delay(10);
}
else if (digitalRead(donePin) == HIGH && digitalRead(donePin) != done)
{
done = digitalRead(donePin);
lcd.clear();
}
// Next Button
if ( digitalRead(nextPin) == LOW && digitalRead(nextPin) != next )
{
next = digitalRead(nextPin);
lcd.clear();
nextHandler();
delay(10);
}
else if (digitalRead(nextPin) == HIGH && digitalRead(nextPin) != next)
{
next = digitalRead(nextPin);
lcd.clear();
}
}
void doneHandler(){
displayIndex = 0;
if(state + 1 <= 4){ state++; }
else{
state = 0;
lastIndex = 0;
displayIndex = 0;
}
if(state == 3) {
sortBatteries(Batteries, lastIndex);
popupMessage("Connect Charger!", 5000);
}
}
void nextHandler(){
// Measure
if (state == 1){
if(lastIndex < ARRAYMAX){
double cells[4];
getVoltages(cells);
addBattery(lastIndex, cells[0], cells[1], cells[2], cells[3]);
popupMessage("Saved!", 1000);
}
else{
popupMessage("Not Saved, Full!", 1000);
}
}
// View
else if(state == 2){
if(displayIndex < lastIndex-1){displayIndex++;}
else { displayIndex = 0; }
}
// Charge
else if(state == 3){
if(charging){
//charging = false;
}else if (!charging){
charging = true;
chargingIndex++;
}
}
}
void addBattery(int letterIndex, double Cell_1, double Cell_2 , double Cell_3, double Cell_4)
{
Batteries[lastIndex] = Battery(letters[letterIndex], Cell_1, Cell_2, Cell_3, Cell_4);
lastIndex++;
}
void popupMessage(String message, int delayMs){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
delay(delayMs);
lcd.clear();
}
void chargerCheck()
{
double arr[4];
double total = Batteries[chargingIndex].getTotal();
if(getVoltages(arr) > total - 0.2 && getVoltages(arr) < total + 0.2){
charging = false;
}
}