Hello all,
I am trying to make it so that data on captured from sensors gets saved to SD card everything two minutes. I want a time elapsed.
h:m:s
The first save is at 0:0:0, the second save is at 0:1:0 ect
// LCD setup file
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Sensors setup
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS_1 2
#define ONE_WIRE_BUS_2 3
OneWire oneWire_in(ONE_WIRE_BUS_1);
OneWire oneWire_out(ONE_WIRE_BUS_2);
DallasTemperature sensor_inhouse(&oneWire_in);
DallasTemperature sensor_outhouse(&oneWire_out);
// Configuration SD card module
// SD card attached to SPI bus as follows:
// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 10
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Chip select pin for selecting SD card module
// Delay for every two mins to save
const unsigned long interval = 60000 ; // 2 minutes is 120000 milli seconds
unsigned long previous = 0 ;
// Increment
unsigned long previousTime = 0;
byte seconds ;
byte minutes ;
byte hours ;
void setup()
{
Serial.begin(9600); // Baud rate of 9600
pinMode(chipSelect, OUTPUT); // Set chip select as output
sensor_inhouse.begin();
sensor_outhouse.begin();
// Time elapsed
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Radiator");
lcd.setCursor(2,1);
lcd.print("RIOU GLASS!");
delay(1000);
lcd.setCursor(0,0);
lcd.setCursor(0,1);
Serial.println(F("Radiator"));
Serial.println();
while (!Serial) {
}
Serial.print("Starting SD card...");
if (!SD.begin(chipSelect)) { // If SD module not working
Serial.println("Card failed to start or not present");
while (1);
}
Serial.println(" Card is now operational!"); // If SD module working
Serial.println();
File dataFile = SD.open("radiator.txt", FILE_WRITE); // Being to write in beehive.txt
// Printing column name for date, time, Temp_1, Temp_2, Temp_DHT and Humidity on serial terminal
delay(100);
Serial.print("Time elapsed "); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Radiator 1 in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
Serial.print("Radiator 2 in °C"); // Print header name for column
Serial.print(" \t"); // Create tab/space
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("Timer"); // Print header name for column
// dataFile.print("\t"); // Create tab/space
dataFile.print(",");
dataFile.print("Radiator 1 in °C"); // Print header name for column
// dataFile.print("\t"); // Create tab/space
dataFile.print(",");
dataFile.print("Radiator 2 in °C"); // Print header name for column
// dataFile.print("\t"); // Create tab/space
dataFile.print(",");
dataFile.println(); // Insert a new line
dataFile.close();
delay(1000);
}
void loop()
{
unsigned long current = millis(); // Setting delay for saving data on SD card every two minutes
previousTime = previousTime + 1000; // use 100000 for uS
seconds = seconds +1;
if (seconds == 60)
{
seconds = 0;
minutes = minutes +1;
}
if (minutes == 60)
{
minutes = 0;
hours = hours +1;
}
if (hours == 13)
{
hours = 1;
}
if(current - previous >= interval) {
File dataFile = SD.open("radiator.txt", FILE_WRITE);
if (dataFile){ // If data is now ready to be written
// Save to SD card
// dataFile.print("");
dataFile.print(hours, DEC);
dataFile.print(":");
dataFile.print(minutes, DEC);
dataFile.print(":");
dataFile.print(seconds, DEC);
dataFile.print(",");
// dataFile.print("\t"); // Create tab/space
dataFile.print(sensor_inhouse.getTempCByIndex(0)); // Print temperature from temperature sensor 1
// dataFile.print("\t"); // Create tab/space
dataFile.print(",");
dataFile.print(sensor_outhouse.getTempCByIndex(0)); // Print temperature from temperature sensor 2
// dataFile.print("\t"); // Create tab/space
dataFile.println();
dataFile.close();
// Print to serial
Serial.print(hours, DEC);
Serial.print(":");
Serial.print(minutes, DEC);
Serial.print(":");
Serial.print(seconds, DEC);
Serial.print(" \t"); // Create tab/space
Serial.print(sensor_inhouse.getTempCByIndex(0)); // Print temperature from temperature sensor 1
Serial.print(" \t"); // Create tab/space
Serial.print(sensor_outhouse.getTempCByIndex(0)); // Print temperature from temperature sensor 2
Serial.print(" \t"); // Create tab/space
Serial.println();
}
else {
Serial.println("There has been an error saving data"); // If the has been a problem
}
previous = current ; // Delay
}
sensor_inhouse.requestTemperatures();
sensor_outhouse.requestTemperatures();
lcd.setCursor(0, 0);
lcd.print("Sens 1: ");
lcd.print(sensor_inhouse.getTempCByIndex(0));
lcd.setCursor(0, 1);
lcd.print("Sens 2: ");
lcd.print(sensor_outhouse.getTempCByIndex(0));
// delay(5000);
}
I get this result in serial monitor
Radiator
Starting SD card... Card is now operational!
Time elapsed Radiator 1 in °C Radiator 2 in °C
0:0:37 23.50 23.62
0:1:15 23.44 23.56
0:1:53 23.50 23.56
0:2:31 23.37 23.56
0:3:9 23.44 23.56
0:3:47 23.50 23.62
0:4:25 23.44 23.62