Detecting double events - can bus

Hi all,

I’ve been having a hard time implementing a double click event. I currently have code that when a bus message is received, will send out a bus message.

I’m trying to add in a double press timed functionality. IE:

If bus message is received(2) within(time)

Do (X)

I’ve been reading up on and experimenting with millis but can’t seem to quite get there. Any help is appreciated!

Here is my original, working code

// CAN Receive Example
//

#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(10);                               // Set CS to pin 10


boolean cameraState = false;

void setup()
{
  Serial.begin(9600);

  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if (CAN0.begin(MCP_ANY, CAN_33K3BPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input

  Serial.println("MCP2515 Library Receive Example...");


}

void loop()
{
  byte on[3] = {04, 03, 00};
  byte off[3] = {04, 00, 00};

  if (!digitalRead(CAN0_INT))                        // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)

    if (((rxId & 0x10758040) == 0x10758040) && (rxBuf[0] == 140)) {  //If specific message ID and specific message data

      if (cameraState == false) {

        byte sndStat = CAN0.sendMsgBuf(0x10754040, 1, 3, on);
        cameraState = true;
        Serial.println(cameraState);
      }
      else
      { byte sndStat = CAN0.sendMsgBuf(0x10754040, 1, 3, off);
        cameraState = false;
        Serial.println(cameraState);
      }
    }
  }
}

Look for tutorials online using millis() to measure the time between presses.