Simple Double Click


//declare button PIN
const int buttonPIN = 13;

//vars to keep track of clicks and click time
const int buttonDelayTime = 500;
char buttonClicks = 0;
int buttonPressed = false;
bool buttonHeldDown = false;
long timePressed;


void setup() {
// setup the pins
  Serial.begin(9600);
  pinMode(buttonPIN, INPUT);
  
}

void loop() {
  
  // read the button state
  buttonPressed = digitalRead(buttonPIN);
/*
Detect a click when a button is pressed
Start timer when button is released
If another clicked is read within the timer, add another click
If not, time out and print out to the serial port
*/
  if(buttonPressed == HIGH && buttonHeldDown == false)
  {
    buttonHeldDown = true;
    if(buttonClicks == 0)
    {
      buttonClicks = 1;
      
    }
    if(buttonClicks == 1 && millis() < (timePressed + buttonDelayTime))
    {
      buttonClicks = 2;
      
    }
    
  } 
  if(buttonPressed == LOW && buttonHeldDown == true)
  {
    timePressed = millis();
    buttonHeldDown = false;
  }
  if(millis() > (timePressed+buttonDelayTime) &&  buttonHeldDown == false) {
  {
    if (buttonClicks == 1) Serial.println("1 Click");
    if (buttonClicks == 2) Serial.println("Double Click");
    buttonClicks = 0;
  }
  }
  
  
}

Documentation for the code would add values.

hi im very new to this, what do you mean by documentation?

Documentation = Human language to explain what you are doing. In this case, some simple C language comments using // and /* */ are probably what has been suggested. That would be called "internal documentation". Additional texts and diagrams to explain the code would be "external documentation". Such as user guides, manuals etc.

I suggest deleting some superfluous blank lines in your sketch, and applying auto formatting in the IDE using ctrl-T or the menu choice for it, prior to uploading.

ohhh, thank you!
Im sorry Ill get right on it

Did you not mean,
Serial.println("1 Click");

?

Thank you for catching that! I usually prefer bytes rather than ASCII characters but its roughly the same in the end

I'm surprised that it even works. But I guess you tested it.

Thanks for updating it, however in future, do not edit posts that have been commented on. It destroys the sense of the replies.

1 Like

Specify what the code is doing. You surely knows Windows and mouse klicks. You define/set the speed/rate for double clicks. What applies to Your code?
When is it a double click and when would it be two clicks?
Code is never accepted alone. As professional digging into quite large systems there were meters of shelves with binders telling what the code strategy was, timing diagrams.... You name it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.