1

I am making a cansat which will be launched on a sugar rocket. The cansat's Arduino nano will be operating sensors which will read the data and store it in an sd card and also a servo to deploy the parachute.

The problem is that I am not able to make code where I want all the sensors to be logging data in the sd card in a loop and a mini servo should perform a sweep angle of 90 degrees after 2 minutes of power supply to the Arduino.

I have made the code for sensors inputting data into sd card but the servo part is confusing me due to no possibility of putting servo code in void setup as the setup will first do the servo sweep after 2 minutes and then record data, neither can I put it in the loop as that also records the data after 2 minutes and I tried to put that into a new function but that's also not working.

Please give me a solution for this. The code that I have made is below : ( just for sensors logging data in sd card )

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#include <SD.h>

#define DHTPIN 10
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

const int MQ2 = A0;

File dataFile;

void setup() {
  Serial.begin(9600);
  dht.begin();
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
  if (!SD.begin(4)) {
    Serial.println("SD card initialization failed!");
    while (1) {}
  }
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println("Temperature, Humidity, Pressure, Gas");
    dataFile.close();
  } else {
    Serial.println("Error opening data file!");
  }
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  sensors_event_t event;
  bmp.getEvent(&event);
  float pressure = event.pressure;
  float gas = analogRead(MQ2);
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print(temperature);
    dataFile.print(",");
    dataFile.print(humidity);
    dataFile.print(",");
    dataFile.print(pressure);
    dataFile.print(",");
    dataFile.println(gas);
    dataFile.close();
  } else {
    Serial.println("Error opening data file!");
  }
  delay(1000);
}
2
  • 2
    For a parachute I wouldn't code a sweep, I'd just write the servo to 90 so it moves as quickly as possible. Releasing a parachute is not usually a gradual process. As far as how, just test millis() in your program to see how long it has been since launch and at 120 seconds write the servo to 90 degrees.
    – Delta_G
    Jun 9, 2023 at 17:00
  • 1
    examine the blinkWithoutDelay example sketch in the Arduino IDE
    – jsotola
    Jun 9, 2023 at 17:35

1 Answer 1

1

I assumed you would want to collect the data after you opened the parachute. And as Delta_G suggested, you would want to open the parachute as fast as possible and not gradually.

I also assumed you want to collect the data every second, based on your provided code, because it has a 1000-millisecond delay. With all that here is a code that might work: (I haven't tested it, but it should give an idea of how to get going)

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#include <SD.h>

#define DHTPIN 10
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

const int MQ2 = A0;
const long parachute_timeout_ms = 2 * 60 * 1000;   // Open parachute after two minutes.
bool parachute_opened = false;

const long data_collection_interval_ms = 1 * 1000; // Collect data every second.
bool start_collecting_data = false;
unsigned long previous_ms = 0;

File dataFile;

void setup() 
{
  Serial.begin(9600);
  dht.begin();
  if (!bmp.begin()) 
  {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
  if (!SD.begin(4)) 
  {
    Serial.println("SD card initialization failed!");
    while (1) {}
  }
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (dataFile) 
  {
    dataFile.println("Temperature, Humidity, Pressure, Gas");
    dataFile.close();
  } 
  else 
  {
    Serial.println("Error opening data file!");
  }

  previous_ms = millis();
}

void loop() 
{

  // Check to see if it's time to open the parachute.
  unsigned long current_ms = millis();

  if ((parachute_opened == false) && (current_ms - previous_ms >= parachute_timeout_ms)) 
  {
    // Your code to open the parachute
    // ... ... ..

    // Make sure this code doesn't execute again.
    parachute_opened = true;

    //...and then set the flag that tells the other part of the code
    // that it should start collecting data.
    start_collecting_data = true;
  }

  if ((start_collecting_data) && (current_ms - previous_ms >= parachute_timeout_ms))
  {
    collect_data();
    previous_ms = current_ms;
  }
}

void collect_data()
{
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  sensors_event_t event;
  bmp.getEvent(&event);
  float pressure = event.pressure;
  float gas = analogRead(MQ2);
  dataFile = SD.open("data.txt", FILE_WRITE);
  if (dataFile) 
  {
    dataFile.print(temperature);
    dataFile.print(",");
    dataFile.print(humidity);
    dataFile.print(",");
    dataFile.print(pressure);
    dataFile.print(",");
    dataFile.println(gas);
    dataFile.close();
  } 
  else 
  {
    Serial.println("Error opening data file!");
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.