Trying to use plx-daq to read when a button is pressed

Hello,

We are trying to write a program where every time a button is pressed, we want the time to be recorded as well as other details like which button it was.
However, when we use the plx-daq extension to transfer all this data to an .csv file, it records constantly. (ex. makes nonstop rows until we disconnect it).

Our main problem is that we cannot have any large delays in between button pressings, because we want to be able to record every button press (maybe that is once every one minute... maybe that is 10 times in 30 seconds?)

Is this a problem that others have encountered before? We are at a bit of a loss now.

My understanding of plx-daq, and faint memory of using it some months ago, is that you just do a serial print from Arduino when you want to capture a record.

So since you are capturing button presses, should you not simply do a print when a button of interest becomes pressed, ala the state change detect tutorial.

Always share your code.

Adapting the tutorial here, I got this output:

Computer Time Date
16:46:38 43770.00
16:46:39 43770.00
16:46:41 43770.00
16:46:43 43770.00
16:46:48 43770.00
16:46:50 43770.00
16:46:55 43770.00
16:46:56 43770.00

... with each line caused by a button press, after putting this line:

Serial.println("DATA,TIME,DATE");

... in the appropriate place in the state change detect example. (So the serial print only happens when the button is newly pressed, rather than just spewing say every time through loop. The state change detect example is easy to adapt for many buttons, and if you have them in an array you can print the index as a field to identify the button.)

Needs the appropriate lines in setup() of course:

Serial.println("CLEARDATA");     
Serial.println("LABEL,Computer Time,Date");
Serial.println("MSG, Hello World...");
}

Here’s a sketch doing state change detection on an array of pins with buttons to ground. You just need to add or subtract pin numbers from the array; it uses sizeof() to figure out how many pins are used.

Here’s the output (arrays are indexed from zero, so 3 pins are 0, 1 and 2).

Computer Time Date Pin
17:25:16 43770.00 0
17:25:17 43770.00 1
17:25:17 43770.00 1
17:25:18 43770.00 2
17:25:19 43770.00 0
17:25:20 43770.00 0
17:25:20 43770.00 1
17:25:21 43770.00 2
17:25:21 43770.00 2
17:25:22 43770.00 2
17:25:22 43770.00 1
// state change detect on a button array and reported to plx-daq
// 1 nov 2019

/*
  State change detection (edge detection) changed for INPUT PULLUP
*/

// the buttons
byte buttonPins[] = {3, 4, 5}; //the buttons must be wired from pin to ground, pinmodes are input_pullup
const byte howManyButtons(sizeof(buttonPins) / sizeof(byte));
bool buttonStates[howManyButtons];         // current state of the button
bool lastButtonStates[howManyButtons];     // previous state of the button

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("setup() ... ");
  Serial.println("report state change detect on a button array to plx-daq");

  // initialize the button pins as input with pullup so active low
  //    make sure the button is from pin to ground
  for (int i = 0; i < howManyButtons; i++)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
    //initialize button states
    buttonStates[i] = digitalRead(buttonPins[i]);
    lastButtonStates[i] = buttonStates[i];
  }
  //plx daq stuff
  Serial.println("CLEARDATA");       
  Serial.println("LABEL,Computer Time,Date,Pin");
  Serial.println("MSG, Hello World...");

  Serial.println("setup() done");
  Serial.println("Press a button....");
  Serial.println(" ");
}

void loop()
{
  checkForButtonStateChange();
} //loop

void checkForButtonStateChange()
{
  for (int i = 0; i < howManyButtons; i++)
  {
    buttonStates[i] = digitalRead(buttonPins[i]);
    // compare the buttonState to its previous state
    if (buttonStates[i] != lastButtonStates[i]) // means it changed... but which way?
    {
      if (buttonStates[i] == LOW)  // changed to pressed
      {
        Serial.print("DATA,TIME,DATE,");
        Serial.println(i);
      }
      // poor man's de-bounce
      delay(50);
    }
    // save the current state as the last state, for next time through the loop
    lastButtonStates[i] = buttonStates[i];
  }
} // checkForButtonStateChange()