Hello there,
I am more or less a beginner in programming the Arduino (made several really easy codes up until now).
First of all the code:
/*
* Data Logger for digital dial gauges.
* Up to 4 gauge signals can be connected and interpreted.
*/
#include <SPI.h>
#include "SdFat.h"
// SD chip select pin
const uint8_t chipSelect = 4;
// Log file base name
#define FILE_BASE_NAME "LOG"
//=======================================================
// File system object
SdFat sd;
// Log file
SdFile file;
// Time in millis for next data record
uint32_t logTime;
//======================================================
// User functions.
//------------------------------------------------------
int i;
int sign;
long value;
float result;
float result1;
int clockpin1 = 5;
int datapin1 = 10;
int clockpin2 = 2;
int datapin2 = 3;
int clockpin3 = 6;
int datapin3 = 7;
int clockpin4 = 8;
int datapin4 = 9;
unsigned long tempmicros;
const uint8_t GAUGE_COUNT = 5;
const uint8_t DATA_COUNT = 4;
float RESULTS[DATA_COUNT];
// Write data header
void writeHeader() {
file.print(F("MICROS"));
for (uint8_t i = 1; i < GAUGE_COUNT; i++) {
file.print(F(",MESSUHR_"));
file.print(i, DEC);
}
file.println();
}
//-----------------------------------------------------
//Funktion zum Decodieren der Messuhr-Signale
float decode(int x, int y) {
sign=1;
value=0;
for (i=0;i<23;i++) {
while (digitalRead(x)==HIGH) {} //wait until clock returns to HIGH- the first bit is not needed
while (digitalRead(x)==LOW) {} //wait until clock returns to LOW
if (digitalRead(y)==LOW) {
if (i<20) {
value|= 1<<i;
}
if (i==20) {
sign=-1;
}
}
}
result=(value*sign)/100.00;
return result;
delay(300);
}
//-----------------------------------------------------
// Log a data record
void logData() {
uint16_t data[DATA_COUNT];
for (uint8_t i = 0; i < DATA_COUNT; i++) {
data[i] = RESULTS[i];
}
// Write data to file. Start with log time in millis.
file.print(logTime);
// Write GAUGE data to CSV record
for (uint8_t i = 0; i < DATA_COUNT; i++) {
file.write(',');
file.print(data[i]);
}
file.println();
}
//===================================================
// Error messages stored in flash.
#define error(msg) sd.errorHalt(F(msg))
//===================================================
void setup() {
//CLK und DATA-Pins für Messuhren belegen
pinMode(clockpin1, INPUT_PULLUP);
pinMode(datapin1, INPUT_PULLUP);
/*pinMode(clockpin2, INPUT_PULLUP);
pinMode(datapin2, INPUT_PULLUP);
pinMode(clockpin3, INPUT_PULLUP);
pinMode(datapin3, INPUT_PULLUP);
pinMode(clockpin4, INPUT_PULLUP);
pinMode(datapin4, INPUT_PULLUP);
*/
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";
Serial.begin(9600);
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(4))) {
sd.initErrorHalt();
}
// Find an unused file name.
/* if (BASE_NAME_SIZE > 6) {
error("FILE_BASE_NAME too long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("Can't create file name");
}
}
if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
error("file.open");
}
// Write data header.
writeHeader();*/
}
void loop() {
//Prozedur zur Auswertung der Messuhrsignale (MESSUHR1)
while (digitalRead(clockpin1)==HIGH) {} //if clock is LOW wait until it turns to HIGH
tempmicros=micros();
while (digitalRead(clockpin1)==LOW) {} //wait for the end of the HIGH pulse
if ((micros()-tempmicros)>500) { //if the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
result1 = decode(clockpin1,datapin1); //decode the bit sequence
}
logTime = millis();
Serial.print(logTime);
Serial.print("\t");
Serial.println(result1,2);
//logData();
// Force data to SD and update the directory entry to avoid data loss.
//if (!file.sync() || file.getWriteError()) {
// error("write error");
//}
}
As can be seen, I took a couple of snippest from the SDfat examples to build the code.
What I want to do is reading data from dial gauges, decoding the signals (which already works, code is also taken from another project) and writing the data to an SD Card.
My problem is that as soon as I uncomment these lines:
if (!sd.begin(chipSelect, SD_SCK_MHZ(4))) {
sd.initErrorHalt();
}
I am not able to get readings anymore. In the serial monitor I can still see printed values, but every value is 0.00.
If I comment these lines, the readings are correct.
So communication with the SD Card kind of destroys what I want to do and I have no idea why.
I am hoping that some of you might help me work the problem out.
Big thanks in advance.
Have a nice day.
Best regards,
Matthias