How would it be possible to save information to SD every 15 minutes but at the same time regulate temperature and humidity every second?
I believe the 15 mins would block this function to regulate every second.
Here is the code:
/*
* File: main.c
* Company: RIOU Glass
* Author: Mateusz WINTER
* Project: This project is to monitor temperature and humidity
* in a beehive by logging and saving data on SD card
*
*/
// Configuration for DHT Sensor
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Configuration for RTC Module
// DS1302 RST -> Arduino Digital 7
// DS1302 DATA -> Arduino Digital 6
// DS1302 CLK -> Arduino Digital 5
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(6, 7, 8);
// Configuration SD card module
// SD card attached to SPI bus as follows:
// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 4
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
// Temperature sensors
float Temp_1;
float Temp_2;
// Delay
unsigned long seconds = 1000L; // 1000ms to 1s
unsigned long minutes = seconds * 900; // 900s in 15 minutes
// Fan and heater
const int HEATER = 10; //Red LED turn on heater
const int FAN = 9; //Blue LED turn on fan
void setup () {
Serial.begin(9600); // Baud rate of 9600
pinMode(chipSelect, OUTPUT);
// Heater and fan
pinMode(HEATER,OUTPUT);
pinMode(FAN,OUTPUT);
digitalWrite(HEATER,LOW);
digitalWrite(FAN,LOW);
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
myRTC.setDS1302Time(00, 17, 11, 6, 25, 06, 2019);
Serial.println(F("Beehive data logger"));
Serial.println();
while (!Serial) {
}
Serial.print("Starting SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed to start or not present");
while (1);
}
Serial.println(" Card is now operational!");
Serial.println();
dht.begin();
File dataFile = SD.open("testing.txt", FILE_WRITE);
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
delay(100);
Serial.print("Date "); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Time "); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Humidity in %"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_DHT in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_1 in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Temp_2 in °C"); // Print header name for column
Serial.println(); // Insert a new line
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on SD card
dataFile.print("Date "); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print("Time "); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print("Humidity in %"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print("Temp_DHT in °C"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print("Temp_1 in °C"); // Print header name for column
dataFile.print(" \t"); // Create tab/space
dataFile.print("Temp_2 in °C"); // Print header name for column
dataFile.println(); // Insert a new line
dataFile.close();
}
//}
void loop() {
File dataFile = SD.open("testing.txt", FILE_WRITE);
// This allows for the update of variables for time or accessing the individual elements. //
myRTC.updateTime();
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Temp_1 = analogRead(A0)*5/1023.0; // Read value from analog port A0 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
Temp_1 = Temp_1 - 0.5;
Temp_1 = Temp_1 / 0.01;
Temp_1 = Temp_1 - 5.0;
Temp_2 = analogRead(A1)*5/1023.0; // Read value from analog port A1 voltage range 0V - 5V 1023 is to convert the 10 bit number to a voltage reading.
Temp_2 = Temp_2 - 0.5;
Temp_2 = Temp_2 / 0.01;
Temp_2 = Temp_2 - 5.0;
if (dataFile){
// Save to SD card
dataFile.print("");
dataFile.print(myRTC.dayofmonth);
dataFile.print("/");
dataFile.print(myRTC.month);
dataFile.print("/");
dataFile.print(myRTC.year);
dataFile.print(" ");
dataFile.print(myRTC.hours);
dataFile.print(":");
dataFile.print(myRTC.minutes);
dataFile.print(":");
dataFile.print(myRTC.seconds);
dataFile.print(" \t");
dataFile.print(F(""));
dataFile.print(h);
dataFile.print(F("% "));
dataFile.print(t);
dataFile.print(" \t"); // Create tab/space
dataFile.print(Temp_1); // Print temperature from temperature sensor 1
dataFile.print(" \t"); // Create tab/space
dataFile.print(Temp_2); // Print temperature from temperature sensor 2
dataFile.print(" \t"); // Create tab/space
dataFile.println();
dataFile.close();
// Print to serial
Serial.print("");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.print(myRTC.seconds);
Serial.print(" \t");
Serial.print(F(""));
Serial.print(h);
Serial.print(F("% \t"));
Serial.print(t);
Serial.print(" \t"); // Create tab/space
Serial.print(Temp_1); // Print temperature from temperature sensor 1
Serial.print(" \t"); // Create tab/space
Serial.print(Temp_2); // Print temperature from temperature sensor 2
Serial.print(" \t"); // Create tab/space
Serial.println();
}
else {
Serial.println("There has been an error saving data");
}
// Temperature and humidity control
if(Temp_2 > 25) // If the temperature is above 25 then turn on fan
{
digitalWrite(HEATER,LOW);
digitalWrite(FAN,HIGH);
}
else
{
digitalWrite(HEATER,HIGH);
digitalWrite(FAN,LOW);
}
//if ( h > 60 ) // If the humidity if greater than 60%
//{
// digitalWrite(HEATER, LOW);
// digitalWrite(FAN, HIGH);
//}
//else
//{
// digitalWrite(HEATER, LOW);
// digitalWrite(FAN, LOW);
//}
//delay(minutes);
delay(2000);
}