I am having problems logging data from the Ocean Controls thermocouple multiplexer shield to the Adafruit data logger using the Arduino uno. I have resolved all SPI pin conflicts, as per the Adafruit forum... http://forums.adafruit.com/viewtopic.php?f=31&t=39699 Both shields work together, however whenever I try to write sensor data to the SD card the data is written as 0.00 rather than say 25.50. I have tried to convert the float value from the sensor to a string using sprintf, however the same problem occurs. All outputs are printed to the serial monitor correctly.
The partial code is as follows. I have cut the end out as it was too long for this post.
#include <string.h> //Use the string Library
#include <ctype.h>
#include <EEPROM.h>
#include <SD.h> // Libraries for SD shield
#include <SPI.h>
// Libraries for RTC
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc; //RTC command
// SD pins
int CS_pin = 10;
//#define SHOWMEYOURBITS // Display the raw 32bit binary data from the MAX31855
#define PINEN 7 //Mux Enable pin
#define PINA0 4 //Mux Address 0 pin
#define PINA1 5 //Mux Address 1 pin
#define PINA2 6 //Mux Address 2 pin
#define PINSO 12 //TCAmp Slave Out pin (MISO)
#define PINSC 13 //TCAmp Serial Clock (SCK)
#define PINCS 9 //TCAmp Chip Select Change this to match the position of the Chip Select Link
int Temp[8], SensorFail[8];
float floatTemp, floatInternalTemp;
char failMode[8];
int internalTemp, intTempFrac;
unsigned int Mask;
//char data[16];
char i, j, NumSensors =1, UpdateDelay;
char Rxchar, Rxenable, Rxptr, Cmdcomplete, R;
char Rxbuf[32];
char adrbuf[3], cmdbuf[3], valbuf[12];
int val = 0, Param;
unsigned long time;
void setup()
{
Serial.begin(9600);
Serial.println("TCMUXV3");
// SD set up and test
pinMode(CS_pin, OUTPUT); // CS pin is an output
// If CS_pin reads false the program will terminate
if(!SD.begin(CS_pin, 11, 12, 13))
{
Serial.println("Card Failure");
return;
}
// RTC set up and test
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
return;
}
if (EEPROM.read(511)==1)
{
NumSensors = EEPROM.read(0);
UpdateDelay = EEPROM.read(1);
}
pinMode(PINEN, OUTPUT);
pinMode(PINA0, OUTPUT);
pinMode(PINA1, OUTPUT);
pinMode(PINA2, OUTPUT);
pinMode(PINSO, INPUT);
pinMode(PINCS, OUTPUT);
pinMode(PINSC, OUTPUT);
digitalWrite(PINEN, HIGH); // enable on
digitalWrite(PINA0, LOW); // low, low, low = channel 1
digitalWrite(PINA1, LOW);
digitalWrite(PINA2, LOW);
digitalWrite(PINSC, LOW); //put clock in low
}
void loop()
{
if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
{
time = millis();
//for(j=0;j<NumSensors;j++)
//{
if (j<(NumSensors-1)) j++;
else j=0;
switch (j) //select channel
{
case 0:
digitalWrite(PINA0, LOW);
digitalWrite(PINA1, LOW);
digitalWrite(PINA2, LOW);
break;
case 1:
digitalWrite(PINA0, HIGH);
digitalWrite(PINA1, LOW);
digitalWrite(PINA2, LOW);
break;
case 2:
digitalWrite(PINA0, LOW);
digitalWrite(PINA1, HIGH);
digitalWrite(PINA2, LOW);
break;
case 3:
digitalWrite(PINA0, HIGH);
digitalWrite(PINA1, HIGH);
digitalWrite(PINA2, LOW);
break;
case 4:
digitalWrite(PINA0, LOW);
digitalWrite(PINA1, LOW);
digitalWrite(PINA2, HIGH);
break;
case 5:
digitalWrite(PINA0, HIGH);
digitalWrite(PINA1, LOW);
digitalWrite(PINA2, HIGH);
break;
case 6:
digitalWrite(PINA0, LOW);
digitalWrite(PINA1, HIGH);
digitalWrite(PINA2, HIGH);
break;
case 7:
digitalWrite(PINA0, HIGH);
digitalWrite(PINA1, HIGH);
digitalWrite(PINA2, HIGH);
break;
}
delay(5);
digitalWrite(PINCS, LOW); //stop conversion
delay(5);
digitalWrite(PINCS, HIGH); //begin conversion
delay(100); //wait 100 ms for conversion to complete
digitalWrite(PINCS, LOW); //stop conversion, start serial interface
delay(1);
Temp[j] = 0;
failMode[j] = 0;
SensorFail[j] = 0;
internalTemp = 0;
for (i=31;i>=0;i--)
{
digitalWrite(PINSC, HIGH);
delay(1);
//print out bits
#ifdef SHOWMEYOURBITS
if (digitalRead(PINSO)==1)
{
Serial.print("1");
}
else
{
Serial.print("0");
}
#endif
if ((i<=31) && (i>=18))
{
// these 14 bits are the thermocouple temperature data
// bit 31 sign
// bit 30 MSB = 2^10
// bit 18 LSB = 2^-2 (0.25 degC)
Mask = 1<<(i-18);
if (digitalRead(PINSO)==1)
{
if (i == 31)
{
Temp[j] += (0b11<<14);//pad the temp with the bit 31 value so we can read negative values correctly
}
Temp[j] += Mask;
//Serial.print("1");
}
else
{
// Serial.print("0");
}
}
//bit 17 is reserved
//bit 16 is sensor fault
if (i==16)
{
SensorFail[j] = digitalRead(PINSO);
}
if ((i<=15) && (i>=4))
{
//these 12 bits are the internal temp of the chip
//bit 15 sign
//bit 14 MSB = 2^6
//bit 4 LSB = 2^-4 (0.0625 degC)
Mask = 1<<(i-4);
if (digitalRead(PINSO)==1)
{
if (i == 15)
{
internalTemp += (0b1111<<12);//pad the temp with the bit 31 value so we can read negative values correctly
}
internalTemp += Mask;//should probably pad the temp with the bit 15 value so we can read negative values correctly
//Serial.print("1");
}
else
{
// Serial.print("0");
}
}
//bit 3 is reserved
if (i==2)
{
failMode[j] += digitalRead(PINSO)<<2;//bit 2 is set if shorted to VCC
}
if (i==1)
{
failMode[j] += digitalRead(PINSO)<<1;//bit 1 is set if shorted to GND
}
if (i==0)
{
failMode[j] += digitalRead(PINSO)<<0;//bit 0 is set if open circuit
}
digitalWrite(PINSC, LOW);
delay(1);
//delay(1);
}
//Serial.println();
//Serial.println(Temp,BIN);
Serial.print("#");
Serial.print(j+1,DEC);
Serial.print(": ");
if (SensorFail[j] == 1)
{
Serial.print("FAIL");
if ((failMode[j] & 0b0100) == 0b0100)
{
Serial.print(" SHORT TO VCC");
}
if ((failMode[j] & 0b0010) == 0b0010)
{
Serial.print(" SHORT TO GND");
}
if ((failMode[j] & 0b0001) == 0b0001)
{
Serial.print(" OPEN CIRCUIT");
}
}
else
{
floatTemp = (float)Temp[j] * 0.25;
Serial.print(floatTemp,2);
Serial.print(" degC");
}
Serial.print(" Int: ");
floatInternalTemp = (float)internalTemp * 0.0625;
Serial.print(floatInternalTemp,4);
Serial.print(" degC");
Serial.println("");
//***********************************************
//**** LETS TRY TO LOG DATA TO SD CARD HERE *****
SD.begin(CS_pin, 11, 12, 13);
File dataFile = SD.open("test.txt", FILE_WRITE);
dataFile.println(floatTemp); // write the temp output to file
dataFile.close();
I also have problems logging data from the RTC, to say name a file by todays date, or to write the time next to the sensor data. The SD card is formatted to fat16, and the data logger and RTC work fine with the single Adafruit thermocouple amplifiler.
Any insight into this problem would be greatly appreciated. I am a novice with arduino/C++ code. I have spent the last couple of days trying to resolve this without getting anywhere apart from learning a bit more about coding :P. The end result is to use the unit as a data acquisition module to process monitor a solar water heater.