Mouse clicks more than once

Hi all,

I have two buttons, if one of these buttons pressed, I want a left mouse click. The problem is, while button is pressed, mouse clicks more than once. (like hell)

What I need is, only one click if one of the buttons is clicked…

PS: My buttons are in reversed state and I have a led indicating if the buttons are pressed.

Here is my code, thanks for helping…

#include <Mouse.h>

// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int button2Pin = 3; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int button2State = 0;

void setup() {

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(button2Pin, INPUT);
Mouse.begin();
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
button2State = digitalRead(button2Pin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Mouse.click();
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

if (button2State == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Mouse.click();
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

}

Try using INPUT_PULLUP instead of INPUT on your buttons in void setup() and then switch

if (buttonState == HIGH) {

to

if (buttonState == LOW) {

for button state 1 and 2.

You need to sense when the button changes from high to low or low to high. The keyword here is change. The transition is important not the level. See the state change detection example in the IDE examples (File, Examples, 02.Digital).

chummer1010:
Try using INPUT_PULLUP instead of INPUT on your buttons in void setup() and then switch

if (buttonState == HIGH) {

to

if (buttonState == LOW) {

for button state 1 and 2.

Good advice except that the button will also need to be wired differently.
Here is a good tutorial.
Gammon Switches

I cannot change how the buttons work. If it is pressed, it cuts the circuit. So when not pressed circuit is closed and electricity flows...

Yes you can. read the tutorial I posted.
It just depends on whether the button is connected to power or ground.

Hutkikz:
Good advice except that the button will also need to be wired differently.
Here is a good tutorial.
Gammon Switches

Ah, that's right, I left that part out. Just connect the button to the Arduino pin on one end, and then the other end to ground. Read the tutorial, OP, you will need to know this principle in many other projects.

Thank you guys. I have solved the problem. I have figured out that my problem is not related to wiring…

Actually the code I have written at the beginning of the post was incorrect. I have written the button states wrong. Since my buttons are reversed, the buttonstates had to be LOW instead of HIGH.

By the way I am already using 10K pulldown resistors for the switches, connected to ground…

My problem was something different and I believe many more will face the same issue later…

So my problem was, I have push buttons and if I keep the buttons pressed, the circuit will be close, electrons will flow and the function will work. Even if I press the buttons so instantly, the function was working more than once until I remove my finger from the button…

The function was a simple mouse click. And this was not possible because of time passes and code runs more than once until I release my finger. Every time I tried, the function worked as a double mouse click. (or maybe more than double) So the code gave me an output of double click instead of one click…

So here is my solution (I am writing because maybe someone else will have the same problem and it is good to learn from previous experiences.)

I have ended the mouse and gave a delay to the loop. Here is the final code:

#include <Mouse.h>

void setup() {
// put your setup code here, to run once:
pinMode(12, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

if (digitalRead(2) == LOW)
{
Mouse.begin();
digitalWrite(12, HIGH);
Mouse.click();
Mouse.end();
digitalWrite(12, LOW);
delay(1000);
}

if (digitalRead(3) == LOW)
{
Mouse.begin();
digitalWrite(12, HIGH);
Mouse.press();
Mouse.release();
Mouse.end();
digitalWrite(12, LOW);
delay(1000);
}

}

Delay will make the program unresponsive. Using state change detection will allow you to accomplish the same thing without using delay.

emre:
Actually the code I have written at the beginning of the post was incorrect. I have written the button states wrong. Since my buttons are reversed, the buttonstates had to be LOW instead of HIGH.

Well that explains it. Glad you got it worked out. However, delay() will make this slightly, well delayed, or unresponsive.

Edit: groundfungus beat me to it.

emre:
So here is my solution (I am writing because maybe someone else will have the same problem and it is good to learn from previous experiences.)

Your code could be used as an example in the categories 'what should be avoided' or
'how I thought I would get around state change detection by using delays'.

Not to mention the need to de-bounce.

Here are some final photos of the project and my final code.

Simply, there are two buttons and both of them do a left mouse click. (Than the mouse ends…)
There is also one second delay after one of the buttons is pressed (not to click two times accidentally, or the code not to loop and click more than once)

#include <Mouse.h>

void setup() {

pinMode(12, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);

}

void loop() {

if (digitalRead(2) == LOW)
{
Mouse.begin();
digitalWrite(12, HIGH);
Mouse.click();
Mouse.end();
delay(1000);
digitalWrite(12, LOW);
}

if (digitalRead(3) == LOW)
{
Mouse.begin();
digitalWrite(12, HIGH);
Mouse.press();
Mouse.release();
Mouse.end();
delay(1000);
digitalWrite(12, LOW);
}

}

Change your incorrect “quote” tags to “code”.

I like the buttons!