Good day everyone, I am new in arduino coding
Trying to logger co2 & temp data from mhz19b, with timestamp from rtc
hardware setup uno + sd shield with rtc ds1307, and co2 sensor
in serial monitor data normal on co2, temp & rtc but only rtc write in sd card
I need help on "get logvalue"
my code :
// Variable declaration: Lists all the variables used in the program
#include "SoftwareSerial.h"
#include "MHZCO2.h"
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
const int RX = 9;
const int TX = 8;
SoftwareSerial ss(TX, RX);
MHZ19B MHZ19B;
const int chipSelect = 10; //10 is default by shield, but normally on Pin 4
int interval = 30; //Log to SD Card every 30 seconds
long timer;
String timestring;
String mvalue;
RTC_DS1307 rtc;
// >>>>>>>>>>>>>>>>> Setup: Executes once when the Arduino is powered on
void setup()
{ Serial.begin(9600);
delay(2000);
Serial.println("Hello World");
Serial.println("Data Logger");
Serial.println("------------------");
MHZ19B.begin(&ss);
ss.begin(9600);
delay(1000);
Serial.println("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("SD Card error");
return;
}
Serial.println("card initialized");
if (! rtc.begin()) {
Serial.println("No RTC found");
} else {
Serial.println("RTC clock found");
}
if (! rtc.isrunning()) {
Serial.println("RTC is not configured");
}
}
// Loop: Executes repeatedly
void loop(){
MHZ19B.measure();
Serial.println("------------------");
delay(30000);
if ((timer + interval * 1000) < millis()) {
timer = millis();
get_logvalue(); //Get your value
get_time(); //Get time from RTC
write_data(); //Write value and Time to SD
}
}
void get_time(){ //Read Time from RTC
DateTime now = rtc.now();
timestring = now.day();
timestring += "-";
timestring += now.month();
timestring += "-";
timestring += now.year();
timestring += " ";
timestring += now.hour();
timestring += ":";
timestring += now.minute();
timestring += ":";
timestring += now.second();
Serial.println(timestring);
}
void get_logvalue() {
mvalue = "My Value"; //mvalue is your log parameter eg. Temperature
Serial.print("CO2: ");
Serial.println(MHZ19B.getCO2());
Serial.print("Temp: ");
Serial.println(MHZ19B.getTemperature());
}
void write_data() { //Write to SD card
String dataString = mvalue + "," + timestring;
File dataFile = SD.open("datalog1.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error writing datalog1.txt");
}
}