How can I stop a program from logging data into an SD card?

Hello everyone,

This is my first post here, but I have been reading the forums for quite some time.
I’m trying to figure how to create a routine via software or hardware to stop my program from loading analog readings into my sd card.

I was thinking something like I press “A” key and stop or hardware I press a button and that stops it.
I haven’t been able to find something close to what I want in my search, I don’t know if I’m using the wrong words or what.
I can post the code if needed, but it’s not much different from a datalogger from the SD library example.

Thanks in advance!

_G

If you wrote a sketch to log data to a SD card, why can you not code it to stop the logging?

Because, I'm logging emg data and I'm not sure how long is the time that a loop should run, (if I where to do it that way). I haven't worked much with arduino IDE, I come from programming python and not very familiar with C++.

I have a vague idea that I should set up some interrupts when I press a key or a flag but not sure how to proceed. I'm not asking for a code just directions.

thanks

Use if (digitalRead(somePin == HIGH)) then add a section of code to stop the logging.

Oh I see.. if I do if (digitalRead(somePin == HIGH)) I can then call SD.close(myFile) and stop reading or writing

I will give it a try, thanks!

gbernal: This is my first post here, but I have been reading the forums for quite some time.

Then you know that you should post some code.

I'm trying to figure how to create a routine via software or hardware to stop my program from loading analog readings into my sd card.

I was thinking something like I press "A" key and stop or hardware I press a button and that stops it. I haven't been able to find something close to what I want in my search, I don't know if I'm using the wrong words or what. I can post the code if needed, but it's not much different from a datalogger from the SD library example.

You know enough to know how to read and debounce a button, right? If not, have a look at http://gammon.com.au/switches

You know what a if statement does, right? If not, have a look at http://www.learncpp.com/

If there is some reason you cannot implement these things in your code, feel free to come back with some code you've tried, and we might be able to help.

:sweat_smile: Thanks for the suggestions I’m going to try to implement them.

Here it is what I have so far for the logging and reading 
#include <SPI.h>
#include <SD.h>

File SensorDataout;
File SensorDatain;
// change this to match your SD shield or module;
const int chipselect = 10;

 void setup()
{
	Serial.begin (9600);
	  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(SS, OUTPUT);

/*  Let's check is the file exists, if it doesn't 
					we will exit
*/
  
if (!SD.begin(chipselect)){

	Serial.println("Initialization failed!");
	return;
}

	Serial.println("Initialization done!");


	if (SD.exists("EMGdata3.txt")){
		Serial.println("EMGdata3.txt exists.");
	}

	else{ 
		Serial.println("EMGdata3.txt doesn't exists.");
		makeFile();
	}
  // cals the write function
	writeData();
//////////******************************************///////////////
////////// This part begins the sd reader part ////////////////
/////////            for the setup part           /////////////////
//////////******************************************///////////////
	SensorDatain = SD.open("EMGdata3.txt");
  	if (SensorDatain) {
    Serial.println("EMGdata3.txt:");
    
    // read from the file until there's nothing else in it:
    while (SensorDatain.available()) {
    	char byteIn = SensorDatain.read();
      int data1;
      int data2;
      int data3;
      while (byteIn != '\n') {
        data1 = SensorDatain.parseInt();
        data2 = SensorDatain.parseInt();
        data3 = SensorDatain.parseInt();

    //ln = Serial.write(datatest.read());
    //Serial.print("this is data1 = ");
    Serial.println(data1);
    

     

    }
  }
    // close the file:
    SensorDatain.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening EMGdata3.txt");
  }
  		

}

void loop()
{
//////////******************************************///////////////
////////// This part begins the sd write  part ////////////////
/////////                      /////////////////
//////////******************************************///////////////
	  	// make a string for assembling the data to log:
  	String dataString = "";

	  // read three sensors and append to the string:
	  for (int analogPin = 0; analogPin < 3; analogPin++) {
	    int sensor = analogRead(analogPin);
	    dataString += String(sensor);
	    if (analogPin < 2) {
	      dataString += ","; 
	    }
	  }

  		SensorDataout.println(dataString);
  		//Serial.println(dataString);
  		SensorDataout.flush();

      
}

void makeFile()

{
	/*  Let's make a file in case we don't have one already */
	Serial.println("Let's make that file");
	SensorDataout = SD.open("EMGdata3.txt",  FILE_WRITE); //FILE_WRITE is the flag that allow us to creat a new fil
	SensorDataout.close();
	// let's make sure the file was created
	  // Check to see if the file exists: 
  if (SD.exists("EMGdata3.txt")) {
    Serial.println("EMGdata3.txt exists.");
  }
  else {
    Serial.println("EMGdata3.txt doesn't exist.");  
  }

}

void writeData()
{
//////////******************************************///////////////
////////// This part begins the sd write part ////////////////
/////////            for the setup part           /////////////////
//////////******************************************///////////////
	SensorDataout = SD.open("EMGdata3.txt", FILE_WRITE);
    if (! SensorDataout) {
       Serial.println("error opening EMGdata3.txt");
        // Wait forever since we cant write data
        while (1) ;
      }
      

}

I've done a few logging programs. I would typically have a function call to write a dataset to the card. At the beginning of that function I would check a flag (controlled by somewhere else in the code, usually watching a pin state) to see if I wanted to log a data line. If the flag isn't set, return immediately. Then (or concurrently with the previous check) I would check the "if the file was actually able to be opened" flag (that I tested back in startup()). If that flag isn't set, return immediately. Then open the file for writing, write out my dataset, then close the file. Finally, return from the function.

That way, if power is interrupted between write attempts (when I'm collecting and manipulating data), I don't corrupt the SD card, and nothing is cached in a write buffer. This is particularly important if you are only writing to the SD card at 1 dataset per second or less often. Also, if I flip the switch (or send the command) while a dataset is being written, the dataset finishes writing and the file is closed before I update the flag, so I don't have to worry about a partial dataset in my file.

Also, when I'm checking to see if I want to continue logging (either by looking at a pin state or by command from one of many I/O streams) I only have to set or clear a flag, I don't have to remember to open/close a file at that point, and any future (in operation) commands to write to a file aren't trying to write to a closed file potentially causing errors. File operations for writing datasets is contained to one small section of my code instead of being scattered throughout.

Wow, this is great stuff!! thank you so much. I will report back once I have implemented your inputs.

Very much appreciated!