Im using an ADS1231 chip to read a bridge circuit, and I'm having trouble storing the data in a SD card. My professor gave us a library to use, and it works for our other sensors, but for the ADS1231 it just prints:
Logging to: LOGGER03.CSV
millis (mS), strain gauge 1(V)strain gauge 2 (V),
2192
writing to buffer
4196
writing to sd card
1
6213
writing to buffer
8217
writing to sd card
2
0
It continues on like this with the number increasing by about 2000 or so each time.
Heres the code
// External Libraries for writting to the SD Card
#include <SdFat.h>
#include <SdFatUtil.h>
// Defining the usage of the SD card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// Controls for the data logging system
#define LOG_INTERVAL 2000 // milli seconds between entries
#define SYNC_INTERVAL 4000 // mills between writes to the the SD card.
// Time keeper
uint32_t syncTime = 0; // time of last sync()
// Use this LED for your writing indicator
int led = 6;
int ledState=0;
//ADC pins
int data = 9;
int sclk = 11;
int power = 12;
int data2 = 3;
int sclk2 = 4;
int power2 = 5;
// Defining number of items to log and where the sensors are plugged into the Arduino
const unsigned int numberOfSensors = 2;
// Titles for column headings
String analogSensorNames[numberOfSensors] = {
"strain gauge 1(V)" "strain gauge 2 (V)"};
void setup()
{
// put your setup code here, to run once:
//data and sclk will shift bytes in
pinMode(led, OUTPUT);
pinMode(data, INPUT);
pinMode(sclk, OUTPUT);
pinMode(power, OUTPUT);
digitalWrite(data, HIGH);
digitalWrite(sclk, LOW);
pinMode(data2, INPUT);
pinMode(sclk2, OUTPUT);
pinMode(power2, OUTPUT);
digitalWrite(data2, HIGH);
digitalWrite(sclk2, LOW);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// SD Card setup stuff
pinMode(10, OUTPUT); // To make the SPI (microSD card) interface work Set pin 10 to an Output.
pinMode(8, OUTPUT); // Define the Chip Select pin for the SparkFun SD card
// Initialize the SD Card and insure it is readable
initializeSDCard();
initialWriteSDCard();
}
void loop()
{
delay(LOG_INTERVAL);
logTheTime(); // Wait for a log interval to take a new batch of data samples
//initialize variables and conversion factors
int32_t value = 0;
int32_t value2 = 0;
double vref = 5;
double voltage;
double voltage2;
//wait for data to go low and read 1st ADS
if (digitalRead(data) == LOW){
//shifts in three bytes and writes value in twos compliment
value = shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);
// extends the sign bit to 32 bit register
if (value & 0x00800000) {
value |= 0xFF000000;
}
//shifts data high (polling pulse)
digitalWrite(sclk, HIGH);
delay(10);
digitalWrite(sclk, LOW);
//coverts value to volts
uint32_t norm = 0x7FFFFF00;
double dnorm = (double) norm;
double scale = vref/dnorm;
voltage = (double)(value*scale);
//writes to SD card?
file.print(voltage,6);
Serial.println(voltage,6);
}
//End this current record by starting a new line
file.println();
Serial.println();
//Write to the SD card and if a write to SD is necessary then change the LEDwrittingStatus
if(writeDataToSD()){
//Change the State of the writtingStatus LED from 0->1, or 1->0
++ledState;
Serial.println(ledState);
if (ledState > 1) {
ledState = 0;
Serial.println(ledState);
}
}
//Update the activity LED to show a successful write cycle
digitalWrite(led, ledState); // Change the LED state
}
Heres the write to sd card code
//Write the Data to SD card
boolean writeDataToSD()
{
//Verify that can write to sd Card
if (file.writeError) error("write data");
//check to see if it is time to write to sd Card
//don't sync too often - requires 2048 bytes of I/O to SD card
if ((millis() - syncTime) < SYNC_INTERVAL)
{
Serial.println("writing to buffer");
return 0;
}
syncTime = millis();
//if millis - syncTime is > than SYNC_INTERVAL, write buffer to SD Card
//check first that there is no sync errors with card
if (!file.sync()) error("sync");
Serial.println("writing to sd card");
return 1;
}
This code works with writing to the SD card
// External Libraries for writting to the SD Card
#include <SdFat.h>
#include <SdFatUtil.h>
// Defining the usage of the SD card
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// Controls for the data logging system
#define LOG_INTERVAL 500 // milli seconds between entries
#define SYNC_INTERVAL 2000 // mills between writes to the the SD card.
// Time keeper
uint32_t syncTime = 0; // time of last sync()
// Use this LED for your writing indicator
int led = 6;
int ledState=0;
// Analog pressure sensor variables
int pressureSensor;
float pressureSensorV;
float psi;
// Defining number of items to log and where the sensors are plugged into the Arduino
// 1 Pressure
const unsigned int numberOfSensors = 1;
// Titles for column headings
String analogSensorNames[numberOfSensors] = {
"Pressure (psi)"};
void setup()
{
// put your setup code here, to run once:
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// SD Card setup stuff
pinMode(10, OUTPUT); // To make the SPI (microSD card) interface work Set pin 10 to an Output.
pinMode(8, OUTPUT); // Define the Chip Select pin for the SparkFun SD card
// Initialize the SD Card and insure it is readable
initializeSDCard();
initialWriteSDCard();
}
void loop()
{
delay(LOG_INTERVAL);
logTheTime(); // Wait for a log interval to take a new batch of data samples
//Read 1st sensors
pressureSensor = analogRead(A0);
pressureSensorV = (pressureSensor)*(5.0/1024);
psi = (pressureSensorV-(0.1*5.0))/(4.0/15.0);
file.print(',');
file.print(psi);
Serial.print(',');
Serial.print(psi,2);
//End this current record by starting a new line
file.println();
Serial.println();
//Write to the SD card and if a write to SD is necessary then change the LEDwrittingStatus
if(writeDataToSD()){
//Change the State of the writtingStatus LED from 0->1, or 1->0
++ledState;
Serial.println(ledState);
if (ledState > 1) {
ledState = 0;
Serial.println(ledState);
}
}
//Update the activity LED to show a successful write cycle
digitalWrite(led, ledState); // Change the LED state
}
Sorry for the long post, but if anyone can see why the first program doesn't work and the second one does I'd greatly appreciate it.