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);
}