Guidance on SD logging program

Hi all,

I am an Automotive Engineering student with a mechatronics background so I'd say I'm quite on level with my Arduino but maybe not some really specific stuff.

I am working on a program to analyse driving behaviour which I want to log on an SD card and eventually log to Google Spreadsheets. But I have a question on my SD card logging, when I connect it through my car 12V adapter the setup works fine, but when the car turns off and on again the writing to the SD card won't continue anymore. I have to get the SD card out, reset the board and put the SD card back in for it to work.
Later on in my project I would like to connect a buzzer when certain values have been exceeded to control driving behaviour. Also I would like to know if it is possible to log data on the SD card and when the module gets the wifi connection it will send the SD card data to the google spreadsheet.

I know, quite a lot of questions but I have been dying to ask them :slight_smile:

To sum up the questions:
A. What makes the board not reboot the program when power is cut off?
B. Why does keeping the SD card in the module and resetting the board with the button not work?
C. Is it possible to let the buzzer go off while it keeps reading the accelerometer? (is that using interrupts?)
D. Is it possible, and if so how, to send the logged data from the SD card to the google spreadsheet when connected back to wifi and write to the SD card when wifi is lost?

What I am using:

  1. ESP8266 1.0 NodeMCU
  2. Adafruit MMA8451 Accelerometer
  3. Maker Factory SD Card module
    I Included my .ino file since I exceeded the max amount of characters and really don't know which part else to show.

Hope this is enough info to help out ^^

G-Force_Measurement_ESP8266_v3.ino (9.34 KB)

Please read the advicing topics telling "How to...", especially using code tags when posting code.
My unit, for the moment, can't open a. ino file so You get no help from here now.

Hi,

On the "how to" post I had read about what to do when exceeding the amount of characters (9000) so that's why I attached the .ino file. I also read and write the raw data from the sensor, but to keep the code simplified I removed it.

Below I have added the code

//Includes
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <SPI.h>                                                                                  // Include the Serial Peripheral Interface (SPI) library
#include <SD.h>                                                                                   // Include the SD card module library

//Defines
#define ledPin D4

//Variables
const int chipSelect = D8;                                                                        // used for ESP8266

//Other
Adafruit_MMA8451 mma = Adafruit_MMA8451();
File myFile;

void setup() {

  Serial.begin(9600);                                                                             //Start serial
  pinMode(ledPin, OUTPUT);
  Serial.println("Adafruit MMA8451 test!");

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");

  mma.setRange(MMA8451_RANGE_2_G);

  Serial.print("Range = "); Serial.print(2 << mma.getRange());
  Serial.println("G");

  Serial.println("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");                                                     // Used for debug if SD module isn't found
    while (1);
  }
  Serial.println("initialization done.");


  if (myFile) {                                                                                  // Open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    Serial.print("Writing to DATA.txt...");                                                      // Serial Print "writing to data file"
    myFile.println("DATA File SV 1 :)");                                                         // Write first line in the file
    myFile.close();                                                                              // Close the file
    Serial.println("done.");                                                                     // Serial Print "done" when file is closed
  }

  else {
    Serial.println("error opening DATA.txt");                                                    // If the file didn't open, print an error
  }
}



void loop() {

  mma.read();

  /* Get a new sensor event */
  sensors_event_t event;
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");

  String dataString = "";                                                                     // Set up data string 1 to use for the ","

  String dataStringX = "";                                                                    // Set up data string 2 to use for the X value
  String dataStringY = "";                                                                    // Set up data string 3 to use for the Y value
  String dataStringZ = "";                                                                    // Set up data string 3 to use for the Z value

  float x = event.acceleration.x;                                                             // Read the X value from accelerometer and put it into a float
  float y = event.acceleration.y;                                                             // Read the Y value from accelerometer and put it into a float
  float z = event.acceleration.z;                                                             // Read the Z value from accelerometer and put it into a float

  dataString += ",";                                                                          // Add the "," to datastring 1

  dataStringX += String(x);                                                                   // Add the data from the string read from X accelerometer to datastring 1 X value
  dataStringY += String(y);                                                                   // Add the data from the string read from Y accelerometer to datastring 2 Y value
  dataStringZ += String(z);                                                                   // Add the data from the string read from Z accelerometer to datastring 3 Z value

  myFile = SD.open("DATA.txt", FILE_WRITE);                                                   // Open the SD card file

  if (myFile) {
    Serial.println("File opened ok");                                                         // Serial Print "File opened ok" when file opens
  }

  if (myFile) {
    Serial.println("Writing to DATA.txt...");                                                 // Serial Print "writing to data file"
    myFile.print("  X value: ");                                                              // Print "X value: " into the file
    myFile.print(x);                                                                          // Print x value from accelerometer into the file
    myFile.print(",");                                                                        // Print "," into the file to separate the values. Add more with each new value
    myFile.print("  Y value: ");                                                              // Print "Y value: " into the file
    myFile.print(y);                                                                          // Print y value from accelerometer into the file
    myFile.print(",");                                                                        // Print "," into the file to separate the values. Add more with each new value
    myFile.print("  Z value: ");                                                              // Print "Z value: " into the file
    myFile.println(z);                                                                        // Print z value from accelerometer into the file
    digitalWrite (ledPin, HIGH);                                                              // Turn on LED while writing to SD card
  }
  myFile.close();                                                                             // Close the SD card file

}

Wel.
At the top of every forum ia a sticky post called how to use this forum.
In that item 7 shows how to post code with code tags.

As for the power issue.
Have you tried to reset the arduino with the on board reset button?

Does your sd card use resistors or a level shifter IC ?

When your code gets ththat large. You might want to use tabs.

You can easily move entire functions to a tatab.

Keep setup and loop on the main tab.

And that tab must keep the file name.

Okay so I wanted to upload my schematic file of how I did the wiring and I got banned for that? I had read the how to and it said it is a good idea to put in a schematic drawing of how it's wired.

So okay I won't do that again.

The setup works fine when the power is ona and the SD card is not inserted yet. Also reset button only works when the SD card is first removed and power is back on, then the reset button is pressed. If the power goes out and reset is pressed when the SD card is still inside it won't do anything. Also I am unable to upload a sketch when the SD card is still inside.

Is it possible to let it reset the program every time the power gets back on?

Edit: SD module works with resistors I believe since it has R1 up to R4 soldered on it

Wiring, or schematic is always wellcome. As matter of fact often requested by helpers.
Some misunderstanding took place I think.

I have send a message asking to lift the ban, because indeed I also think there is a misunderstanding.
But getting back to the subject, any tips or ideas on it?

uploaded your picture to a free photo host like FLICKR
copy the link to the picture and post the link.

if your picture is too large, that might have flagged the system.

getting banned just does not make sense, getting put into 'timeout' for trying to post too many posts too quickly is an inconvenience for new members.

IF the problem is resolved by re-cycling power to the SD module

you can add an FET to the power of the SD card,
when you cannot read or write, shut off power using the FET, then pause, then apply power again.

if you have to re-set the Arduino program, that is possible
and if you need to cut power to the Arduino and let it power back up, that too is possible
all done in software.

I did not go through the schematic to make sure that it is safe, but if it is,

Allright I got the power issue down thanks @dave-in-nj the FET solution works! So that leaves me my original post question C and D.

With C being:
How to let the buzzer go off for 10 or so seconds but I need it to perform in the background. I have read this post and tried it with switch cases but I don't want the code to be time based, it has to be value based. The Finite State Machine | Majenko's Hardware Hacking Blog

Would that need me to put all of the code into switches and make it relying on the accelerometer value?

And question D being if it is possible to read the data from the SD card when WiFi connection is made and send it to the Google Spreadsheet?