-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlanB_ArduinoMaster.ino
201 lines (170 loc) · 6.32 KB
/
PlanB_ArduinoMaster.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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SoftwareSerial.h>
#define SEALEVELPRESSURE_HPA (1013.25) // Typical sea level pressure in hPa (hectopascals)
#define THRESHOLD_ALTITUDE 600 // Altitude threshold for parachute deployment in meters
#define MOTOR_PIN 10 // Motor control pin
#define video 6
Adafruit_BME280 bme; // I2C
int previousAltitude = 0; // Previous altitude reading
int altitudeOffset = 0; // Offset for altitude calibration
unsigned long lastTime = 0; // Last time altitude was measured
bool firstReading = true; // Flag to indicate first altitude reading
bool ascending = false; // Flag to indicate ascending altitude
bool descending = false; // Flag to indicate descending altitude
bool parachuteDeployed = false; // Flag to indicate if parachute is deployed
unsigned long parachuteStartTime = 0; // Parachute deployment start time
String canstatus = "0";
int counter = 0;
bool printData = false;
SoftwareSerial mySerial(7,8); // RX, TX
SoftwareSerial xbee(2,3);
int readAverageAltitude() {
int sum = 0;
for (int i = 0; i < 10; i++) { // Take 10 altitude readings
sum += bme.readAltitude(SEALEVELPRESSURE_HPA); // Read altitude from BME280 sensor
delay(100); // Delay to stabilize sensor
}
return sum / 10; // Calculate and return average altitude
}
void activateBuzzer() {
Serial.println("activate");
}
void deployParachute() {
//--
unsigned long startTime = millis();
unsigned long duration = 1000; // 1 second in milliseconds
while (millis() - startTime < duration) {
digitalWrite(MOTOR_PIN, HIGH); // Turn the motor on
// You can add additional motor control code here if needed
}
digitalWrite(MOTOR_PIN, LOW); // Turn the motor off after 1 second
}
void readBMPValues(float bmpValues[]) {
// Read BMP sensor values (altitude, pressure, temperature, humidity)
int currentAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA) - altitudeOffset;
int pastAltitude = previousAltitude; // Assuming you have a global variable 'previousAltitude' storing past altitude
// Determine status based on altitude values
//String canstatus;
if (currentAltitude > pastAltitude) {
canstatus = "Ascending";
} else if (currentAltitude == pastAltitude) {
canstatus = "Stationary";
} else {
canstatus = "Descending";
}
// Check for special conditions
if (currentAltitude < 5) {
canstatus = "ReadyToLaunch";
} else if (currentAltitude > pastAltitude && currentAltitude < 20) {
activateBuzzer(); // Call function to activate buzzer
}
//Serial.println(altitudeOffset);
// Store sensor values and status in the BMP sensor values array
bmpValues[0] = currentAltitude;
bmpValues[1] = bme.readPressure() / 100.0F; // Convert pressure to hPa
bmpValues[2] = bme.readTemperature();
bmpValues[3] = bme.readHumidity();
// Update the previous altitude for next iteration
previousAltitude = currentAltitude;
// Print status
//Serial.println(canstatus);
}
void calibrateSensors() {
int currentAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
altitudeOffset = currentAltitude;
//Serial.println(altitudeOffset);
//Serial.println("Calibratting... & done");
}
void setup() {
pinMode(video,OUTPUT);
digitalWrite(video,HIGH);
Serial.begin(9600);
mySerial.begin(9600);
xbee.begin(9600);
pinMode(MOTOR_PIN, OUTPUT);
digitalWrite(MOTOR_PIN,0);// Initialize motor pin as output
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
String sensorData = "0,0,0,0,0,0,";
void loop() {
// Check if there is data available from the other Arduino
if (mySerial.available() > 0) {
// Read the data sent from the other Arduino
sensorData = mySerial.readStringUntil('\n');
sensorData.trim();
Serial.println(sensorData);
// Print the received sensor data to the Serial Monitor
//Serial.println(sensorData);
}
// Check if there is data available from the Serial Monitor
if (Serial.available() > 0) {
// Read the command from the Serial Monitor
char command = Serial.read();
// If the command is 'c', send 'c' to the other Arduino for calibration
if (command == 'c') {
calibrateSensors();
//mySerial.write('c');
}
if (command == 'v') {
digitalWrite(video,LOW);
}
if(command == 'd'){
deployParachute();
}
if (command == 's') {
printData = true; // Set flag to start printing
//Serial.println("Printing data...");
}
}
if(printData ){
unsigned long currentTime = millis(); // Current time
// Take the first altitude reading
if (firstReading) {
if (currentTime - lastTime >= 3000) {
previousAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA) - altitudeOffset; // Store the average altitude reading
//Serial.println("First reading taken.");
firstReading = false; // Set the flag to indicate first reading is done
lastTime = currentTime; // Update last time altitude was measured
}
} else {
// Take the second altitude reading after 3 seconds
if (currentTime - lastTime >= 3000) {
int currentAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA) - altitudeOffset; // Calculate average altitude
// Determine the trend
if (currentAltitude > previousAltitude) {
//Serial.println("Ascending");
ascending = true;
descending = false;
} else if (currentAltitude < previousAltitude) {
//Serial.println("Descending");
ascending = false;
descending = true;
} else {
//Serial.println("Stable");
ascending = false;
descending = false;
}
previousAltitude = currentAltitude; // Update previous altitude reading
lastTime = currentTime; // Update last time altitude was measured
firstReading = true; // Set the flag for the next iteration
}
}
// Read BMP sensor values and print data
float bmpValues[4];
readBMPValues(bmpValues);
printSensorData(bmpValues,canstatus);
//printSensorData
}
delay(1000);
}
void printSensorData(float bmpValues[], String canstatus) {
String data = "2022ASI027,"+(String)counter+",00:00:00,"+ sensorData + "0,0,0," + (String)bmpValues[0] + "," + (String)bmpValues[1] + "," + (String)bmpValues[3] + "," + (String)bmpValues[2] + ",0,0,0,80," + canstatus + "";
Serial.println(data);
xbee.println(data);
counter++;
}