Hello All,
I am working on a humidity sensor unit using three humidity sensors and the SDFat Library. The data is going into a .csv file but its not in any usable order. The program pulling each reading every couple of seconds and recording it in one column. I want to be able to group the readings from each sensor in there own individual columns (as shown below in my example)
#include <dht11.h>
#include <SdFat.h>
#include <Time.h>
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
SdFat sd;
SdFile myFile;
dht11 DHT1; // define the sensor 1 type
dht11 DHT2; // define the sensor 2 type
dht11 DHT3; // define the sensor 3 type
const int chipSelect = 8; // sets CS for SD reader to be on pin 8
void setup()
{
DHT1.attach(14); // set pin of sensor 1 to analog 0
DHT2.attach(15); // set pin of sensor 2 to analog 1
DHT3.attach(16); // set pin of sensor 2 to analog 2
Serial.begin(9600); // start clock
while (!Serial); // wait for arduino to start up
Serial.println("Temperature and Humidity Readings ");
Serial.print("3 Sensors: ");
setSyncProvider( requestSync); //set function to call when sync for clock required
Serial.println("Waiting for sync message");
delay(400);
Serial.println("\n");
int chk1 = DHT1.read(); // declare chk1 variable as status of sensor 1
int chk2 = DHT2.read(); // declare chk2 variable as status of sensor 2
Serial.print("Read sensor 1: "); // show status of sensor 1
switch (chk1)
{
case 0: Serial.println("OK \n"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
Serial.print("Read sensor 2: "); // // show status of sensor 2
switch (chk2)
{
case 0: Serial.println("OK \n"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
}
void digitalClockDisplay()// digital clock display of the time
{
Serial.print(hour());
printDigits(minute());
printDigits(second());
myFile.print(hour());
myFileDigits(minute());
myFileDigits(second());
}
void printDigits(int digits) // utility function for digital clock display: prints preceding colon and leading 0
{
Serial.print(":");
if(digits < 10)
{
Serial.print('0');
}
Serial.print(digits);
}
void myFileDigits(int myFileDigits)
{
myFile.print(":");
if (myFileDigits < 10)
{
myFile.print('0');
}
myFile.print(myFileDigits);
}
void processSyncMessage()
{
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER))
{
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) // check the integer is a valid time (greater than Jan 1 2013)
{
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
void loop()
{
if (Serial.available())
{
processSyncMessage();
}
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt(); // Initialize SdFat or print a detailed error message and halt, Use half speed like the native library.change to SPI_FULL_SPEED for more performance.
if (!myFile.open("Readings.csv", O_RDWR | O_CREAT | O_AT_END)) // open file to write to
{
sd.errorHalt("opening Readings.csv for write failed"); // error message if failed
}
float h1 = DHT1.humidity;
h1 = h1; // calibration for sensor 1
digitalClockDisplay();
myFile.print(',');
Serial.println();
Serial.println("Sensor 1");
Serial.print("Humidity (%): ");
Serial.println(h1, DEC);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT1.temperature, DEC);
Serial.print("Temperature (°F): ");
Serial.println(DHT1.fahrenheit(), DEC);
Serial.print("Dew Point (°C): ");
Serial.println(DHT1.dewPoint(), DEC);
Serial.print("Dew PointFast (°C): ");
Serial.println(DHT1.dewPointFast(), DEC);
Serial.println();
Serial.println();
myFile.println(",");
myFile.println("Sensor 1");
myFile.print("Humidity (%), ");
myFile.print(h1, DEC);
myFile.println(",");
myFile.print("Temperature (°C), ");
myFile.print((float)DHT1.temperature, DEC);
myFile.println(",");
myFile.print("Temperature (°F), ");
myFile.print(DHT1.fahrenheit(), DEC);
myFile.println(",");
myFile.print("Dew Point (°C), ");
myFile.print(DHT1.dewPoint(), DEC);
myFile.println(",");
myFile.print("Dew PointFast (°C), ");
myFile.print(DHT1.dewPointFast(), DEC);
myFile.println(",");
myFile.println();
myFile.println();
float h2 = DHT2.humidity;
h2 = h2 + 0; // calibration for sensor 2
digitalClockDisplay();
myFile.print(',');
Serial.println();
Serial.println("Sensor 2");
Serial.print("Humidity (%): ");
Serial.println(h2, DEC);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT2.temperature, DEC);
Serial.print("Temperature (°F): ");
Serial.println(DHT2.fahrenheit(), DEC);
Serial.print("Dew Point (°C): ");
Serial.println(DHT2.dewPoint(), DEC);
Serial.print("Dew PointFast (°C): ");
Serial.println(DHT2.dewPointFast(), DEC);
Serial.println();
Serial.println();
myFile.println("Sensor 2");
myFile.print("Humidity (%),");
myFile.print(h2, DEC);
myFile.println(",");
myFile.print("Temperature (°C), ");
myFile.print((float)DHT2.temperature, DEC);
myFile.println(",");
myFile.print("Temperature (°F), ");
myFile.print(DHT2.fahrenheit(), DEC);
myFile.println(",");
myFile.print("Dew Point (°C), ");
myFile.print(DHT2.dewPoint(), DEC);
myFile.println(",");
myFile.print("Dew PointFast (°C), ");
myFile.print(DHT2.dewPointFast(), DEC);
myFile.println(",");
myFile.println('\n');
myFile.println('\n');
float h3 = DHT3.humidity;
h3 = h3 + 0; // calibration for sensor 3
digitalClockDisplay();
myFile.print(',');
Serial.println();
Serial.println("Sensor 3");
Serial.print("Humidity (%): ");
Serial.println(h3, DEC);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT3.temperature, DEC);
Serial.print("Temperature (°F): ");
Serial.println(DHT3.fahrenheit(), DEC);
Serial.print("Dew Point (°C): ");
Serial.println(DHT3.dewPoint(), DEC);
Serial.print("Dew PointFast (°C): ");
Serial.println(DHT3.dewPointFast(), DEC);
Serial.println();
Serial.println();
myFile.println("Sensor 3");
myFile.print("Humidity (%),");
myFile.print(h3, DEC);
myFile.println(",");
myFile.print("Temperature (°C), ");
myFile.print((float)DHT3.temperature, DEC);
myFile.println(",");
myFile.print("Temperature (°F), ");
myFile.print(DHT3.fahrenheit(), DEC);
myFile.println(",");
myFile.print("Dew Point (°C), ");
myFile.print(DHT3.dewPoint(), DEC);
myFile.println(",");
myFile.print("Dew PointFast (°C), ");
myFile.print(DHT3.dewPointFast(), DEC);
myFile.println(",");
myFile.println('\n');
myFile.println('\n');
delay(1500);
myFile.close();
}
The format the code is currently printing is shown here

And how the end product should look like

Any help or suggestions would be appreciated!