Ohhh, I get it now. Anyways, if we ignore the sloppy mess due to the lack of functions, how would you solve my main problem? I still would have the loop running every 2 seconds and after that it checks to deploy.
OK - With your current code, explain in detail what you would like to happen!
E.G :
every 2000mS, run the GPS read & mySerial output
every time through the loop check servoAltitude >= 1 and mhact == 1 -> set maxheight to true and mhact to 0
every time through the loop check maxheight == true and servoAltitude <= 0.5 -> do servo stuff (Takes 1 second appx.)
What I want is:
Every 2000 ms it reads and prints out the BME680 details (temperature and stuff)
Every 2000 ms it reads and prints out the GPS Location
Every 1 ms (or as close as possible to that) It changes the servoAltitude to the current height and then checks to see if the servo needs to deploy the parachute.
The problem is that that last one also takes 2000 ms
every 2000mS, run the GPS read & mySerial output
How long does this servicing take? Place a millis() start time and end time in the block and see how long it takes.
Can you give me the exact code? I don't know how to use that
It's 01:31 am here, i'mma go to bed, cya tomorrow
Can you give me the exact code? I don't know how to use that
![]()
if (millis() - timer > 2000) {
timer = millis();
// GPS
if (GPS.fix) {
mySerial.print("\n");
mySerial.print("Location:");
mySerial.print(GPS.latitudeDegrees, 4);
mySerial.print(", ");
mySerial.println(GPS.longitudeDegrees, 4);
}
// BME680
mySerial.print("Temperature = ");
mySerial.print(bme.temperature);
mySerial.println(" *C");
mySerial.print("Pressure = ");
mySerial.print(bme.pressure / 100.0);
mySerial.println(" hPa");
mySerial.print("Humidity = ");
mySerial.print(bme.humidity);
mySerial.println(" %");
mySerial.print("Gas = ");
mySerial.print(bme.gas_resistance / 1000.0);
mySerial.println(" KOhms");
mySerial.print("Approx. Altitude = ");
mySerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
mySerial.println(" m");
unsigned long elapsedTime = millis() - timer;
Serial.println(elapsedTime);
}
Ran your code.
elapsedTime = 875 ms
What I want is:
Every 1 ms (or as close as possible to that) It changes the servoAltitude to the current height and then checks to see if the servo needs to deploy the parachute.
Ran your code.
elapsedTime = 875 ms
Do you see the problem?
You may need to only report data until you are some distance (or estimated time) from the ground and then focus your attention completely on the altitude measurement and parachute deployment.
but wait, can't I get close to 875 ms then?
I have an idea, but i'm not quite sure how the statement works...
Can I put a while (altitude <= 100 and maxheight == true and deploy == true)
{ and then run my code here continuously until it's been deployed? }
Okay guys, check this code out. Should this work?
#include <Servo.h>
#include <Adafruit_GPS.h>
#include <Wire.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
bool maxheight = false;
int mhact = 1;
bool deploy = false;
float servoAltitude = 0;
Servo servo;
#define BME_SCK 15
#define BME_MISO 14
#define BME_MOSI 16
#define BME_CS 3
#define SEALEVELPRESSURE_HPA (1024.94) // Meet de druk op de grond!
Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
// BME680
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150);
delay(5000);
// GPS
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
mySerial.println(PMTK_Q_RELEASE);
// Servo Motor
delay(200);
servo.attach(6);
delay(200);
servo.write(180);
delay(200);
servo.detach();
delay(200);
}
uint32_t timer = millis();
void loop() {
// Parsing
char c = GPS.read();
if ((c))
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
if (millis() - timer > 2000) {
timer = millis();
// GPS
if (GPS.fix) {
Serial.print("\n");
Serial.print("Location:");
Serial.print(GPS.latitudeDegrees, 4);
Serial.print(", ");
Serial.println(GPS.longitudeDegrees, 4);
mySerial.print("\n");
mySerial.print("Location:");
mySerial.print(GPS.latitudeDegrees, 4);
mySerial.print(", ");
mySerial.println(GPS.longitudeDegrees, 4);
}
// BME680
Serial.print("\n");
Serial.print("Temperature = ");
Serial.print(bme.temperature);
Serial.println(" *C");
mySerial.print("\n");
mySerial.print("Temperature = ");
mySerial.print(bme.temperature);
mySerial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.pressure / 100.0);
Serial.println(" hPa");
mySerial.print("Pressure = ");
mySerial.print(bme.pressure / 100.0);
mySerial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.humidity);
Serial.println(" %");
mySerial.print("Humidity = ");
mySerial.print(bme.humidity);
mySerial.println(" %");
Serial.print("Gas = ");
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(" KOhms");
mySerial.print("Gas = ");
mySerial.print(bme.gas_resistance / 1000.0);
mySerial.println(" KOhms");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
mySerial.print("Approx. Altitude = ");
mySerial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
mySerial.println(" m");
}
// Servo Motor
servoAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
// Servo Motor: Max Height Initialization
if (servoAltitude >= 100 and mhact == 1)
{ mySerial.println("Above 150 m"); Serial.println("Above 1 m");
maxheight = true; mhact = mhact - 1;
}
// Servo Motor: Deployment
while (altitude <= 72 and maxheight == true and deploy == true) // 32 meters above deployment (max elapsed time = 2 sec. -> 16 m/s * 2 s = 32 m)
{
if (servoAltitude <= 40)
{ mySerial.print("Deploying at height: "); mySerial.println(servoAltitude);
Serial.print("Deploying at height: "); Serial.println(servoAltitude);
delay(100);
servo.attach(6);
delay(100);
servo.write(90);
delay(700);
servo.detach();
delay(100);
maxheight = !maxheight;
}
}
}
while (altitude <= 72 and maxheight == true and deploy == true) // 32 meters above deployment (max elapsed time = 2 sec. -> 16 m/s * 2 s = 32 m)
{
if (servoAltitude <= 40)
{
mySerial.print("Deploying at height: ");
mySerial.println(servoAltitude);
Serial.print("Deploying at height: ");
Serial.println(servoAltitude);
delay(100);
servo.attach(6);
delay(100);
servo.write(90);
delay(700);
servo.detach();
delay(100);
maxheight = !maxheight;
}
}
Do you need all of those tests in the while ?
Only one of them (maxheight) is changed in the code dependant on the while, and altitude is not even declared in the sketch as posted
What tests?
and yes, you are right it should be servoAltitude
These tests
altitude <= 72 and maxheight == true and deploy == true
while (altitude <= 72 and maxheight == true and deploy == true) // 32 meters above deployment (max elapsed time = 2 sec. -> 16 m/s * 2 s = 32 m)
{
if (servoAltitude <= 40)
{
You enter the while() loop when servoAltitude is <72.
How will you know when the servoAltitude is <40?
Do you expect the bme sensor to read itself and magically pass a value to servoAltitude? ![]()
That's a good point... what's a good alternative? reading the bme in the servo loop?
would it work if I stick this line in the servo while () ?
servoAltitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
what's a good alternative?
Don't use a while loop. Use an if statement instead
Read the altitude at the start of loop() and make decisions based on its value
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.