hello Arduiners,
Je viens vers vous car j'ai un souci que je ne comprend pas, je m'explique.
mon projet est d'écrire dans un fichier l'heure et avec du texte à la suite et de répeter tous ca tous les 20sec.
De plus je souhaite créer un fichier pour chaque jour.
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
#include <Wire.h>
#include "RTClib.h"
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
int jour;
int mois;
int annee;
int heure;
int minutes;
int secondes;
char car_secondes [2]="";
char car_minutes [2]="";
char car_jours [2]="";
char car_mois [2]="";
boolean flagwrite=0;
RTC_DS1307 RTC; // Variable RTC permettant de lire la date
String dataString = "";
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
pinMode(53, OUTPUT);
digitalWrite(53,HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
DateTime now = RTC.now();
annee= now.year()%100,DEC;
car_mois[0]=now.month()/10,DEC;
car_mois[1]=now.month()%10,DEC;
car_jours[0]=now.day()/10,DEC;
car_jours[1]=now.day()%10,DEC;
heure=now.hour(),DEC;
car_minutes[0]=now.minute()/10,DEC;
car_minutes[1]=now.minute()%10,DEC;
secondes=now.second(),DEC;
car_secondes[0]=now.second()/10,DEC;
car_secondes[1]=now.second()%10,DEC;
Serial.print(secondes);
Serial.print("\n");
Serial.print(minutes);
Serial.print("\n");
Serial.print(heure);
Serial.print("\n");
Serial.print(jour);
Serial.print("\n");
Write_SD();
delay(2000);
}
La partie du code en haut marche correctement j'ai bien l'heure affiché. La carte est bien initialisé et reconnu ensuite j’appelle la fonction writeSD que voici, dans cette fonction j'ai crée une variable filename qui prend la date du jour au format 11-04-14.txt
ensuite dans une variable datastring j'écrit le texte que j'ai besoin.
void Write_SD ()
{
char temp[8]="";
char namefile[12] ="";
sprintf(namefile,"%d%d-%d%d-%d",car_jours[0],car_jours[1],car_mois[0],car_mois[1],annee);//%d%d-%d%d-car_jours[0],car_jours[1],car_mois[0],car_mois[1]
Serial.print(namefile);
Serial.print("\n");
sprintf(temp,"%d:%d%d:%d%d",heure,car_minutes[0],car_minutes[1],car_secondes[0],car_secondes[1]);
Serial.print(temp);
Serial.print("\n");
// make a string for assembling the data to log:
dataString ="";
// Construction de la ligne a écrire
dataString = "String(temp)";
dataString += " ";//une tabulation
//Serial.println(dataString);
dataString += "String(LNA1)";
dataString += " ";//une tabulation
// Serial.println(LNA1);
dataString += "String(LNA2";
dataString += " ";//une tabulation
// Serial.println(dataString);
// open the file. note that only one file can be open at a time, Regarder ICI si on a changé de jour si oui créer un nouveau fichier.
// so you have to close this one before opening another.
File dataFile = SD.open(namefile , FILE_WRITE);
if (SD.exists(namefile)) {
Serial.println("its exists.");
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
//logfile_flag = LOW; //Efface le flag d'error
flagwrite=1;
}
// if the file isn't open, pop up an error:
}
else
{
Serial.println("doesn't exist.");
Serial.println("error opening .txt");
//logfile_flag = HIGH; //error
}
dataString ="";
}
Le point que je ne comprend pas c'est que le filename est bien écrit il contient 8.3 caractere en format date 11-04-14.txt
mais le fichier ne se crée pas.
J'ai donc tester de remplacer cette ligne
File dataFile = SD.open(namefile , FILE_WRITE);
if (SD.exists(namefile)) {
Serial.println("its exists.");
par celle ci
File dataFile = SD.open("11-05-14.txt", FILE_WRITE);
if (SD.exists("11-05-14.txt")) {
Serial.println("its exists.");
Et la ça marche le fichier
se crée et les lignes s'incrémente correctement en fonction du temps.
Le point est que je ne comprend pas pourquoi le fichier ne se crée pas avec la variable.
Si quelqu'un a une idée de piste a suivre je suis preneur.
En vous remerciant.
Maxime