Pushbutton start/stop for SD logger

I have a voltage logger program here that I’m trying to control with a push button. Problem is that when the button is released, it logs a few times and then the switch goes back the the LOW status. I’ve searched around quite a bit and have seen some examples for on/off switches (http://forum.arduino.cc/index.php?topic=200933.0, http://forum.arduino.cc/index.php?topic=154547.0), always with an LED state. Here I want the high state to call my logging function and I’m having difficulty applying these examples. Any guidance for the code below would be much appreciated.

Wiring is for Adafruit SD shield and 4 analog voltage inputs (0-4) with pushbutton on D8.

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card

uint32_t syncTime = 0; // time of last sync()

const int chipSelect = 10;
//int switchState = 0; 
int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int reading;

//boolean currentState =LOW;//stroage for current button state
boolean previous = LOW;
boolean lastState = LOW;//storage for last button state
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers

//int analogPin4 = 4; PINS 4 and 5 needed by the shield!!!
//int analogPin5 = 5;

int buttonpin = 8;  /// declare D9 pin for button, use byte datatype to save 1 byte of data (int is 2 bytes)

//boolean state = false;

File logfile;
RTC_DS1307 RTC;

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
  while(1);
}

void setup()
{
  pinMode(10, OUTPUT);
  pinMode(buttonpin, INPUT);  // button is an input.
  //if (debugFlag) {
 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps.
 Serial.print("Initializing SD card...");
  
  if (!SD.begin(chipSelect)) {
  Serial.println("Card failed, or not present");
    // don't do anything more:
   while (1) ;
  }  
  Serial.println("card initialized.");
    char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 200; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break; // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin();
  if (!RTC.begin()) {
    logfile.println("RTC failed");
//#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
//#endif //ECHO_TO_SERIAL
  }
  
  logfile.println("millis,stamp,datetime,A0,A1,A2,A3");
  Serial.println("millis,stamp,datetime,A0,A1,A2,A3");
  Serial.println("Waiting for start button ...");
 }
 
 ////################################################################
void loop(){
  
  int currentState;
  reading =digitalRead(buttonpin);
  
 if (reading != previous){
   time=millis();
 }
 
 if ((millis() - time) > debounce){
   currentState = reading;
 
if (currentState == HIGH && lastState == LOW || currentState == HIGH &&  lastState == HIGH) {
  lastState == HIGH;
  logger();

 }
 
if (currentState == LOW && lastState == LOW || currentState == HIGH && lastState == HIGH){
    Serial.println("Waiting for start button ...");
    lastState == LOW;
  }

 }
  previous = reading;
 }
  

//lastState=currentState;

  
int logger() {   
    DateTime now;
   delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

   // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m); // milliseconds since start
  logfile.print(", ");

  Serial.print(m); // milliseconds since start
  Serial.print(", ");
  
  // fetch the time
  now = RTC.now();

  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  logfile.print(now.second(), DEC);
  logfile.print('"');

  Serial.print(now.unixtime()); // seconds since 1/1/1970
  Serial.print(", ");
  Serial.print('"');
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print('"');

int sensor0 = analogRead(analogPin0);
int sensor1 = analogRead(analogPin1);
int sensor2 = analogRead(analogPin2);
int sensor3 = analogRead(analogPin3);

int Vcc = readVcc()/1000.0;

float volt0=(sensor0/1023.00*Vcc); 
float volt1=(sensor1/1023.00*Vcc);
float volt2=(sensor2/1023.00*Vcc); 
float volt3=(sensor3/1023.00*Vcc);


logfile.print(", ");
logfile.print(volt0);
logfile.print(", ");
logfile.print(volt1);
logfile.print(", ");
logfile.print(volt2);
logfile.print(", ");
logfile.print(volt3);
logfile.print(", ");


Serial.print(", ");
Serial.print(volt0);
Serial.print(", ");
Serial.print(volt1); 
Serial.print(", ");
Serial.print(volt2);
Serial.print(", ");
Serial.print(volt3); 
Serial.print(", ");

  
logfile.println();

Serial.println();

logfile.flush();
}

Hi frostygoat

Your switch is connected to pin 8, is that right? Is it normally open until you press it? When pressed, does it connect pin 8 to +5V or GND? What pullup resistors do you have?

Can you clarify how you want the switch to control the logging? For example, "logging happens for as long as the switch is pressed"? Or "logging starts when the switch is pressed; when the switch is released and pressed again, logging stops"?

Regards

Ray

You can also implement a push-on/push-off control.

  1. create a global byte logMode variable set it to 0.

in loop(): 2. look for the switch to be pressed. 3. When pressed, wait for a debounce time. 4. Toggle the state of log Mode. 5 If logMode is 0 don't log, if 1 log.

Hi Ray,

I have a 10K between one pole and ground, and pin 8 is also connected here, then 5v on the other side similar to:

I'm going for press to start, press again to stop, press once more to start.

Thanks Keith I was going that road at one point I'll have to revisit.