Adding a "start logging" button to the adafruit datalogger code

Hi,

I am looking a bit of guidance regarding how I might add a start/stop switch to my code. My code is based on this light & temperature logger from adafruit https://github.com/adafruit/Light-and-Temp-logger/blob/master/lighttemplogger.pde
I wish to have a start/stop button on digital pin 11 which will receive a high when required to log data and a low when not logging. However, I am looking for some forum advice on where to put the lines of code to control the logging function so as not to switch the thing down and lose information required for the file (or corrupt the file due to stopping comms incorrectly). Writing the code isn’t the problem (I hope), but instead where to put the extra lines and how to approach this problem.

I thought of using a while loop, so;
const int start_stop_button =11; //declare pin for button
pinMode(start_stop _button, INPUT); // button is an input
logging = (digitalRead(start_stop _button)) //check if start pressed
while(logging == HIGH)
{
log to SD card (((What part of the light&temp code goes in here?)))
}

Or maybe;

const int start_stop_button =11; //declare pin for button
pinMode(start_stop _button, INPUT); // button is an input
logging = (digitalRead(start_stop _button)) //check if start pressed
if (logging == HIGH)
{
log to SD card (((What part of the light&temp code goes in here?)))
}

Do I put these around the ‘main loop’, or the ‘setup’ and ‘main loop’?

Is there a more clever way to do this??

I notice the suggested code doesn’t have file.close(), is this an issue? Should it be in the code to make sure the file is properly closed?

Please share your thoughts with me.

Cheers,

Phil :slight_smile:

If you post some real code, we can help you.

It looks like you are on the right path. For proper opening and closing I would probably use the if instead of while and put that in the loop() function. This should be a good skeleton to put your logic into. There may be some errors as I didn't write this in the IDE (so it has never been compiled...)

const byte start_stop_button = 11;  // declare D11 pin for button, use byte datatype to save 1 byte of data (int is 2 bytes)
// declare your other pins for your sensors (if digital, analog doesn't need the declares, but it might not be a bad idea to at least reference them with comments here to make remembering what pins do what easier for wiring up your Arduino)

boolean debugFlag = FALSE;  // change this to true to get serial debugging information.

const char logFile[] = "loggingfile.txt";  // This is the filename to log things to.
boolean fileOpen = FALSE;  // a flag to keep track if the file is open. Initialize it as FALSE because the file isn't open at program start (obviously).

void setup() {  // This will run once right after power on, before the first iteration of loop().
  pinMode(start_stop _button, INPUT);  // button is an input.
  // set the pinMode() for any other digital pins.
  if (debugFlag) {
    Serial.begin(9600); // opens serial port, sets data rate to 9600 bps.
  }
}

void loop() {  // This will continue to loop until power is turned off.
  byte logging; // declare the logging status. 
  // declare and initialize the variables for your sensors. Both raw value variables and calculated value variables.

  // read from the sensors and store values to variables.

  // Do any calculations from raw sensor numbers to actual values that make sense (for example convert an ADC value to degrees F).

  logging = (digitalRead(start_stop _button));  //check if start pressed.

  if (debugFlag) {
    // Use Serial.print() and Serial.println() to output the values of your sensors (both raw and calculated) as well as the state of the logging switch to the serial monitor.
  }

  if (logging == HIGH) {  // The switch reads high so we want to output the data to the text file on the SD card.
    if (fileOpen == FALSE) {  // if the file isn't open, open it.
      fileOpen = SD.open(logFile, FILE_WRITE);  // Store the success of opening to the flag.
      if (debug) {  // if debugging, report this to the serial monitor
        Serial.print(F("The file was requested to open. The result is: "));
        Serial.println(fileOpen);
      }
    }
    // various SD.write(), SD.print(), SD.println() calls to output your data stored in the variables to the log file in the format you want.
  } else {  // This will execute if the logging variable is not HIGH (in other words, LOW).
    if (fileOpen == TRUE) {  // the file is open, we should close it.
      SD.close(file);  // flushes the write buffer to the file and closes it cleanly.
      fileOpen = FALSE;
      if (debug) {  // if debugging, report this to the serial monitor
        Serial.print(F("The file was requested to close. The result is: "));
        Serial.println(fileOpen);
      }
    }
  }
}

This should constantly check your sensors for data, but only output to the SD card based on your switch. This might overwrite the txt file every time you flip the switch to start writing again, or start writing at the end of the txt file. The SD library documentation at arduino.cc isn't clear about that. Something for you to experiment with though. :wink:

Oh, you probably want to also #include both the SD and Serial libraries... Hopefully that is obvious...