wizpic
Advanced Member level 3
- Joined
- May 23, 2004
- Messages
- 792
- Helped
- 108
- Reputation
- 216
- Reaction score
- 72
- Trophy points
- 1,308
- Location
- London
- Activity points
- 6,405
I've decided to take the plunge and switch to the Arduino after using PDS for many years, I want to build a data logger but stuck on a little problem
I've got this test code working and every time the arduino starts it creates a new file and logs every five seconds.
But I want to be able to add an header but get stuck it will not compile I get an error this is the code I'm trying to add the header as I only need it once at the top of the file
The error I get is
How can I add header bearing in mind I'm new to this and still learning
Thanks
I've got this test code working and every time the arduino starts it creates a new file and logs every five seconds.
Code:
#include <SPI.h>
#include <SD.h>
//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
int pow_pin = 8;
char filename[16];
int n = 0;
float refresh_rate = 0.0;
void setup()
{
Serial.begin(9600); // Open serial communications and wait for port to open:
Serial.print("Initializing SD card...");
pinMode(CS_pin, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(CS_pin)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
//char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
while (SD.exists(filename)) {
n++;
snprintf(filename, sizeof(filename), "data%03d.txt", n);
}
File dataFile = SD.open(filename, FILE_READ);
Serial.println(n);
Serial.println(filename);
//now filename[] contains the name of a file that doesn't exist
//Write Log File Header
//Read the Configuration information (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if (commandFile)
{
Serial.println("Reading Command File");
float decade = pow(10, (commandFile.available() - 1));
while (commandFile.available())
{
float temp = (commandFile.read() - '0');
refresh_rate = temp * decade + refresh_rate;
decade = decade / 10;
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop()
{
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
dataFile.println("teste");
dataFile.println(voltage);
dataFile.close();
}
else {
Serial.println("error");
}
delay(refresh_rate);
}
But I want to be able to add an header but get stuck it will not compile I get an error this is the code I'm trying to add the header as I only need it once at the top of the file
Code:
#include <SPI.h>
#include <SD.h>
//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
int pow_pin = 8;
char filename[16];
int n = 0;
float refresh_rate = 0.0;
void setup()
{
Serial.begin(9600); // Open serial communications and wait for port to open:
Serial.print("Initializing SD card...");
pinMode(CS_pin, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(CS_pin)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
//char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
while (SD.exists(filename)) {
n++;
snprintf(filename, sizeof(filename), "data%03d.txt", n);
}
File dataFile = SD.open(filename, FILE_READ);
Serial.println(n);
Serial.println(filename);
//now filename[] contains the name of a file that doesn't exist
//Write Log File Header
//Write Log File Header
File dataFile = SD.open("LOG.csv", FILE_WRITE);
if (dataFile)
{
dataFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
String header = "ID, Light, Temp, IR1, IR2";
dataFile.println(header);
dataFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open file");
}
//Read the Configuration information (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if (commandFile)
{
Serial.println("Reading Command File");
float decade = pow(10, (commandFile.available() - 1));
while (commandFile.available())
{
float temp = (commandFile.read() - '0');
refresh_rate = temp * decade + refresh_rate;
decade = decade / 10;
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
}
else
{
Serial.println("Could not read command file.");
return;
}
}
void loop()
{
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
dataFile.println("teste");
dataFile.println(voltage);
dataFile.close();
}
else {
Serial.println("error");
}
delay(refresh_rate);
}
The error I get is
Code:
sd_read_write.ino: In function 'void setup()':
sd_read_write:38: error: redeclaration of 'File dataFile'
sd_read_write:32: error: 'File dataFile' previously declared here
redeclaration of 'File dataFile'
Thanks