écrire les données sur une carte SD, en "autonomie"

Bonjour,

Ci-dessous, un extrait “simplifié” de ce qui sera un code bien plus long dont le but est d’écrire, sur une carte SD, les données de différents capteurs (GPS, accéleromètre etc).
Important : le système définitif sera autonome (alimenté par batterie), donc, pas d’écran de contrôle.

Ce code fonctionne parfaitement dès lors qu’on l’upload dans la carte (Adafruit Flora, pour le moment, Arduino Mega à venir) et que l’on ouvre le moniteur de l’IDE sans quoi, il n’écrit rien dans la carte…

Comment puis-je faire pour que l’écriture sur la carte SD s’active dès lors que la Flora/Mega est alimentée (ou, idéalement, dès lors qu’un interrupteur a été activé) ?

Merci de vos conseils

/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
 		Pin 4 used here for consistency with other Arduino examples
 
 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <SPI.h>
#include <SD.h>
#include <Adafruit_GPS.h> // libairie du GPS, pour la suite.
#include <SoftwareSerial.h>  
#include <Wire.h>
#include <Adafruit_LSM303.h> // libairie de l'accélémromètre, pour la suite.

Adafruit_GPS GPS(&Serial1);
Adafruit_LSM303 lsm;


// 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 = 10; // Flora : pin 10, Mega, 53

File dataFile;
long randNumber0;
long randNumber1;

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
  }


  Serial.print(F("Initializing SD card..."));
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    while (1) ;
  }
  Serial.println(F("card initialized."));
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println(F("error opening datalog.txt"));
    // Wait forever since we cant write data
    while (1) ;
  }
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
  String dataString1 = "";
 // print a random number from 0 to...
    randNumber0 = random(10000);
    dataString += String(randNumber0);
    dataString += " _my_Log_A_ "; 
    randNumber1 = random(10);
    dataString1 += String(randNumber1);
    dataString1 += " my_Log_B_ "; 
    

  dataFile.println(dataString + " " + dataString1); //dataFile.println(dataString + " " + dataString1); ??? à tester...

  // print to the serial port too:
  Serial.println(dataString + " " + dataString1);
  
  // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer! 
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the 
  // SD card is filled with data.
  dataFile.flush();
  
  // Take 1 measurement every 250 milliseconds
  delay(250);
}

Salut,

Suffit de viré ça :

   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

Pour le bouton (en supposant branché comme ceci):

void setup() {

pinMode(bouton,INPUT_PULLUP);

// + le reste de ton setup()

}

void loop() {

if(digitalRead(bouton)==LOW) {

// ici tout le code qui est dans ta loop()

while(digitalRead(bouton)==LOW);
delay(50);

}

}

Ooooops !!! Mais c'est bien sûr !

Pour la question de l'interrupteur, je vais attendre de recevoir la Mega (d'ici 2 jours) car je dois refaire le montage complet, le code "réel" étant trop gros pour la Flora... Je vous tiens au courant.

Merci bien !

Bonjour,

Le code avance et fonctionne. Il écrit les données du GPS et de l’accéléromètre/compas sur la carte SD mais je retrouve le même problème : il ne fonctionne que lorsque je le charge dans la carte, pas en “autonomie” (alimenté, par exemple, depuis un port USB, sans chargement préalable du code).
J’ai bien essayé de “commenté” la ligne // while (!Serial); (ligne 46 du code ci-joint) mais ça ne semble pas vouloir fonctionner…
Le problème ne viendrait-il pas du fait que le GPS est connecté aux TX1 et RX1 de la Mega ?
Je cherche et cherche mais ne vois pas où intervenir…

// Test code for Adafruit Flora GPS modules
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Flora GPS module
// ------> http://adafruit.com/products/1059
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada
     
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>  
#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <SD.h>
#include <SPI.h>

Adafruit_GPS GPS(&Serial1);
Adafruit_LSM303 lsm;
char c = (' ');

// 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 = 10; //déclaration de l'entrée (sortie ?) pour la carte SD

File dataFile; // nom du fichier carte SD


 // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;

void setup()

{
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
 // while (!Serial);     // wait for Serial monitor to connect. j'ai "commenté" cette ligne dans l'idée de rendre le système autonome
 Serial.begin(115200);
 //Serial.println(F("Adafruit GPS library basic test!"));
  
     
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz
     
  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  // Ask for firmware version
  Serial.println(F(PMTK_Q_RELEASE));
  
  // initialize the LSM303 ?
  if (!lsm.begin())
  {
   Serial.println(F("Oops ... unable to initialize the LSM303. Check your wiring!"));
   while (1);
  }
  
  //================================================
  {
    // initialisation de la carte SD
  Serial.print(F("Initializing SD card..."));
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    while (1) ;
  }
  Serial.println(F("card initialized."));
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println(F("error opening datalog.txt"));
    // Wait forever since we cant write data
    while (1) ;
  }
  // fin init carte SD
  
}
}

//============================================
     
uint32_t timer = millis();
void loop() // run over and over again
{

  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if (GPSECHO)
    if (c) Serial.print(c);
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    // Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false :j'ai changé cette ligne qui doit
    // imprimer les données brutes du GPS
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return; // we can fail to parse a sentence in which case we should just wait for another
  
  }
  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis()) timer = millis();
     
  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 1000) {
    timer = millis(); // reset the timer
   
    if (GPS.fix) {
 
    lsm.read();
    
    // a partir d'ici, écriture dans la carte SD des data accel/compas/GPS
  
   dataFile.print(GPS.hour, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.minute, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.seconds, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.milliseconds); 
   dataFile.print(GPS.day, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.month, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.year, DEC); 
   dataFile.print(", "); 
   dataFile.print(GPS.latitude, 4); 
   dataFile.print(", "); 
   dataFile.print(GPS.longitude, 4); 
   dataFile.print(", "); 
   dataFile.print(GPS.speed); 
   dataFile.print(", "); 
   dataFile.print(GPS.angle); 
   dataFile.print(", "); 
   dataFile.print(GPS.altitude); 
   dataFile.print(", "); 
   dataFile.print((int)GPS.satellites); 
   dataFile.print(", "); 
   dataFile.print((int)lsm.accelData.x); 
   dataFile.print(", "); 
   dataFile.print((int)lsm.accelData.y);  
   dataFile.print(", "); 
   dataFile.print((int)lsm.accelData.z); 
   dataFile.print(", "); 
   dataFile.print((int)lsm.magData.x); 
   dataFile.print(", "); 
   dataFile.print((int)lsm.magData.y); 
   dataFile.print(", "); 
   dataFile.println((int)lsm.magData.z);
  
   // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer! 
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the 
  // SD card is filled with data.
  dataFile.flush();

  // Take 1 measurement every 500 milliseconds
  delay(500);
  
      }
    }
  }

Voila que ça fonctionne ! Je continue donc pour ajouter une LED témoin et l'interrupteur...