-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD Interface Code.ino
264 lines (224 loc) · 6.82 KB
/
LCD Interface Code.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <LiquidCrystal.h>
#include <Servo.h>
// Initialize the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Servo objects for azimuth and altitude
Servo servoAzimuth;
Servo servoAltitude;
// Celestial bodies
String celestialBodies[] = {
"Mercury", "Venus", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune",
"Bennu", "Ceres", "2024 PT5"
};
int currentIndex = 0;
const int numBodies = sizeof(celestialBodies) / sizeof(celestialBodies[0]);
const int buttonUp = 8;
const int buttonDown = 9;
const int buttonSelect = 10;
const int buttonReset = 13; // Reset button
const unsigned long debounceDelay = 50;
unsigned long lastDebounceTimeUp = 0;
unsigned long lastDebounceTimeDown = 0;
unsigned long lastDebounceTimeSelect = 0;
unsigned long lastDebounceTimeReset = 0; // Reset button debounce
bool lastButtonStateUp = HIGH;
bool lastButtonStateDown = HIGH;
bool lastButtonStateSelect = HIGH;
bool lastButtonStateReset = HIGH; // Reset button state
String azimuthAltitude = "";
bool dataRequested = false;
const int REAL_TIME = 0;
const int PREDICTION = 1;
int modeIndex = REAL_TIME;
bool modeSelected = false;
bool celestialObjectSelected = false;
bool displayingAzAlt = false;
void setup() {
lcd.begin(16, 2);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonSelect, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP); // Set reset button as input with pull-up
servoAzimuth.attach(6);
servoAltitude.attach(7);
Serial.begin(9600);
displayModeSelection();
}
void loop() {
unsigned long currentTime = millis();
int readingUp = digitalRead(buttonUp);
int readingDown = digitalRead(buttonDown);
int readingSelect = digitalRead(buttonSelect);
int readingReset = digitalRead(buttonReset); // Read reset button
// Handle reset button press to reset and go back to mode selection
if (readingReset != lastButtonStateReset) {
lastDebounceTimeReset = currentTime;
lastButtonStateReset = readingReset;
}
if ((currentTime - lastDebounceTimeReset) > debounceDelay && readingReset == LOW) {
// Reset the whole system and go back to mode selection
modeSelected = false;
celestialObjectSelected = false;
displayingAzAlt = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Reset...");
delay(1000); // Display reset message for 1 second
displayModeSelection();
return; // Exit early after handling the reset
}
// Up button handling
if (readingUp != lastButtonStateUp) {
lastDebounceTimeUp = currentTime;
lastButtonStateUp = readingUp;
}
if ((currentTime - lastDebounceTimeUp) > debounceDelay && readingUp == LOW) {
if (!modeSelected) {
modeIndex = REAL_TIME;
displayModeSelection();
} else if (modeSelected && !celestialObjectSelected && !displayingAzAlt) {
incrementIndex();
displayCelestialBody();
}
delay(200);
}
// Down button handling
if (readingDown != lastButtonStateDown) {
lastDebounceTimeDown = currentTime;
lastButtonStateDown = readingDown;
}
if ((currentTime - lastDebounceTimeDown) > debounceDelay && readingDown == LOW) {
if (!modeSelected) {
modeIndex = PREDICTION;
displayModeSelection();
} else if (modeSelected && !celestialObjectSelected && !displayingAzAlt) {
decrementIndex();
displayCelestialBody();
}
delay(200);
}
// Select button handling
if (readingSelect != lastButtonStateSelect) {
lastDebounceTimeSelect = currentTime;
lastButtonStateSelect = readingSelect;
}
if ((currentTime - lastDebounceTimeSelect) > debounceDelay && readingSelect == LOW) {
if (!modeSelected) {
modeSelected = true;
if (modeIndex == REAL_TIME) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Real-Time Mode");
delay(1000);
displayCelestialBody();
} else if (modeIndex == PREDICTION) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Prediction Mode");
delay(1000);
Serial.println("Prediction");
lcd.setCursor(0, 1);
lcd.print("Triggering...");
delay(2000);
displayModeSelection();
}
} else if (modeSelected && !celestialObjectSelected) {
celestialObjectSelected = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selected: ");
lcd.print(celestialBodies[currentIndex]);
Serial.println(celestialBodies[currentIndex]);
long timeout = millis() + 20000;
azimuthAltitude = "";
while (millis() < timeout) {
if (Serial.available() > 0) {
azimuthAltitude = Serial.readStringUntil('\n');
azimuthAltitude.trim();
if (azimuthAltitude.indexOf(',') != -1) {
displayingAzAlt = true;
displayAzimuthAltitude();
controlServos(azimuthAltitude);
// Delay for 10 seconds after displaying azimuth and altitude
delay(10000);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error/Timeout");
}
break;
}
}
if (azimuthAltitude == "") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timeout/Error");
}
delay(2000);
celestialObjectSelected = false;
displayingAzAlt = false;
displayCelestialBody();
} else if (displayingAzAlt) {
celestialObjectSelected = false;
displayingAzAlt = false;
displayCelestialBody();
}
}
lastButtonStateUp = readingUp;
lastButtonStateDown = readingDown;
lastButtonStateSelect = readingSelect;
lastButtonStateReset = readingReset; // Update reset button state
}
void incrementIndex() {
currentIndex++;
if (currentIndex >= numBodies) {
currentIndex = 0;
}
}
void decrementIndex() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = numBodies - 1;
}
}
void displayCelestialBody() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentIndex + 1);
lcd.print(": ");
lcd.print(celestialBodies[currentIndex]);
}
void displayAzimuthAltitude() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Az/Alt:");
lcd.setCursor(0, 1);
lcd.print(azimuthAltitude);
}
void controlServos(String azAltData) {
int commaIndex = azAltData.indexOf(',');
if (commaIndex != -1) {
String azimuthStr = azAltData.substring(0, commaIndex);
String altitudeStr = azAltData.substring(commaIndex + 1);
float azimuth = azimuthStr.toFloat();
float altitude = altitudeStr.toFloat();
moveServo(servoAzimuth, azimuth);
moveServo(servoAltitude, altitude);
}
}
void moveServo(Servo &servo, float angle) {
angle = constrain(angle, 0, 180);
servo.write(angle);
}
void displayModeSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Mode:");
lcd.setCursor(0, 1);
if (modeIndex == REAL_TIME) {
lcd.print("1: Real-Time");
} else {
lcd.print("2: Prediction");
}
}