Hello,
I am reading four analog amp probes and two digital temp probes. I am trying to save the data on a SD card. However, It just comes up with a weird number 343,343344… Any help would be great. I also would like to put a time associated with it but that can wait. I Know I need to implement the time.h and or RTC. I look forward to your response.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress probe1 = { 0x28, 0xCC, 0xA2, 0x97, 0x05, 0x00, 0x00, 0x1E };
DeviceAddress probe2 = { 0x28, 0x8A, 0xF8, 0x97, 0x05, 0x00, 0x00, 0xF1 };
DeviceAddress spare = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon11, emon12, emon13, emon14; // Create an instance
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(probe1, 10);
sensors.setResolution(probe2, 10);
sensors.setResolution(spare, 10);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
{
emon11.current(1, 111.1); // Current: input pin, calibration.
emon12.current(2, 111.1); // Current: input pin, calibration.
emon13.current(3, 111.1); // Current: input pin, calibration.
emon14.current(4, 111.1); // Current: input pin, calibration.
}
// SD Card Datalogger ;
{
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(53, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}}
void loop(void)
{
delay(2000);
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
Serial.print("Probe 1 is: ");
printTemperature(probe1);
Serial.print("\n\r");
Serial.print("probe 2 is: ");
printTemperature(probe2);
Serial.print("\n\r");
Serial.print("\n\r\n\r");
delay(2000);
double Irms1 = emon11.calcIrms(1480); // Calculate Irms only
double Irms2 = emon12.calcIrms(1480); // Calculate Irms only
double Irms3 = emon13.calcIrms(1480); // Calculate Irms only
double Irms4 = emon14.calcIrms(1480); // Calculate Irms only
Serial.print("CT 1: ");
Serial.print(Irms1*230.0); // Apparent power
Serial.print(" ");
Serial.println(Irms1); // Irms
Serial.print("CT 2: ");
Serial.print(Irms2*230.0); // Apparent power
Serial.print(" ");
Serial.println(Irms2); // Irms
Serial.print("CT 3: ");
Serial.print(Irms3*230.0); // Apparent power
Serial.print(" ");
Serial.println(Irms3); // Irms
Serial.print("CT 4: ");
Serial.print(Irms4*230.0); // Apparent power
Serial.print(" ");
Serial.println(Irms4); // Irms
// SD Card Datalogger ;
{
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 1; analogPin < 4; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
}