Hi.
I want to save the data on CSV in two rows on SD card using data logger. So far I figured out how to save one data on CSV, but I'm having difficulties to save two data on two rows on CSV file, for example time in the first row and temperature in the second row. The code looks like as follow.
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
int buttonState;
int switchPin = 2;
int recordState = 0;
File dataFile;
void setup() {
buttonState = digitalRead(switchPin);
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect.
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) { // See if the SD card is present and can be initialized.
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void loop() {
int val = digitalRead(switchPin);
delay(10); // Debouncing
int val2 = digitalRead(switchPin);
if (val == val2) {
if (val != buttonState) { // Make sure that the pushbutton is pushed down (but not be released yet).
if (val == LOW) { // Make sure that the pushbutton is released.
if (recordState == 0) {
recordState = 1;
dataFile = SD.open("datalog.csv", FILE_WRITE); // If the pushbutton is pressed and released, create/open a new datalog.csv file in the SD card and start recording.
Serial.println("Data Recording Start");
Serial.println("Voltage (V)");
dataFile.println("Data Recording Start");
String dataString1 = "Voltage";
dataFile.println(dataString1);
}
else {
recordState = 0; // If the pushbutton is pressed and released again, close the datalog.csv file and stop recording.
Serial.println("Data Recording Completed");
dataFile.println("Data Recording Completed");
dataFile.close();
}
}
}
}
if (recordState == 1) {
String dataString2 = ""; // Make a string for assembling the data to log.
int analogPin = 0;
float value = analogRead(analogPin); // Read sensor.
float voltage = value*5/1023; // Convert the read value to actual voltage.
dataString2 += String(voltage);
dataFile.println(dataString2); // Print the string to the created datalog.csv file.
Serial.println(dataString2); // Also print the string to the Serial Monitor.
}
buttonState = val;
}
What is basically does is that when a pushbutton is pressed the recording starts, and when the button is pressed again the recording stops. In this case, I can record only input voltage, but I also record time using millis() function. I already have an idea of how to use millis() function, so I just want to know how to record the time in the first row and voltage in the second row.
Thanks.