Dear,
I try to send a file to my sd card. But every time I reset I want another name with a higher number. Therefore, I must everytime make my char array is empty in my while loop.
But i fail in this. Do you know the answer or should I go in a different direction. Any suggestion is welcome.
ps. I use mega ADK with a Wireless shield
My regards, Tim Matthijs
#include <string.h>
#include <stdlib.h>
#include <SD.h>
#define aref_voltage 5.0 // De 5V word met de Aref verbonden en gecontroleerd met de multimeter.
//Sensor 1 pin Variabels
const int Sensor1Pin = A0; // De 1e sensor word aan de analoge pin 0 verbonden. (variabel: interger)
int Sensor1Reading; // the analog reading from the analog resistor divider (variabel: integer)
const int chipSelect = 4;
const int ledPin = 49;
unsigned long Tijd;
char SdBestand [20]="";
char SdBestandleeg [20]="";
void setup() {
// start de serieële communicatie op aan 9600bits per seconde;
Serial.begin(9600);
analogReference(EXTERNAL);
Serial.print("Initializing SD card...");
pinMode(10,OUTPUT);
pinMode(ledPin,OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println ("Card failed, or not present");
return;
}
Serial.println("card Initialized.");
// De voltage die op de AREF word aangesloten (0-5V) word gebruikt als referentie.
int Nummer = 0;
char Cijfer [4];
itoa(Nummer,Cijfer,4);
char Logging [8]= "Loggekn";
char txt [5]=".txt";
strcat (SdBestand, Logging);
strcat (SdBestand, Cijfer);
strcat (SdBestand, txt);
while (SD.exists(SdBestand)==true) {
Nummer++;
//has to empty the char SdBestand!!!
itoa(Nummer,Cijfer,4);
strcat (SdBestand, Logging);
strcat (SdBestand, Cijfer);
strcat (SdBestand, txt);
}
}
void loop()
{
Sensor1Reading = analogRead(Sensor1Pin);
String dataString = "";
String sensor = "Sensor1 ";
// Zet de analoge waarde om naar voltage. geberuik de referentie voltage.
float voltage = Sensor1Reading * aref_voltage / 1024;
//dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
char volt[8] = "";
dtostrf(voltage,6,2,volt);
String ledState = "";
if(voltage <= 2.5) {digitalWrite(ledPin,LOW);
ledState = "LOW";
}
else {digitalWrite(ledPin,HIGH);
ledState = "HIGH";
}
Tijd = millis();
dataString = sensor + " lezen = " + Sensor1Reading + " -" + volt + " Volt" + " - "+ ledState + " - " + Tijd + "ms" ;
File dataFile = SD.open(SdBestand,FILE_WRITE);
if (dataFile){
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
Serial.println(SdBestand);
delay(1000); //1000ms
}
else {Serial.println("error opening datalog.txt");
Serial.println(SdBestand);
delay (1000); //1000ms
}
}
This part is where i talk about
int Nummer = 0;
char Cijfer [4];
itoa(Nummer,Cijfer,4);
char Logging [8]= "Loggekn";
char txt [5]=".txt";
strcat (SdBestand, Logging);
strcat (SdBestand, Cijfer);
strcat (SdBestand, txt);
while (SD.exists(SdBestand)==true) {
Nummer++;
//has to empty the char SdBestand!!!
itoa(Nummer,Cijfer,4);
strcat (SdBestand, Logging);
strcat (SdBestand, Cijfer);
strcat (SdBestand, txt);
}