Push Button Keeps Repeating Mouse Move But I Only Want Mouse Move To Run Once

Hello everyone,

I want to use a Arduino leonardo to emulate the mouse movements when a button is pressed but the problem is that the button press keeps repeating and the mouse movement keeps repeating over and over again. I only want the mouse movement to run only once per button press.

Do you have any suggestion ?

Code below:

#include "Mouse.h"
#include <Keyboard.h>

const int upButton = 2;

void setup()
{
Serial1.begin(9600);
Mouse.begin();
pinMode(upButton, INPUT);
}

void loop()
{
if (digitalRead(upButton) == HIGH) {
Mouse.move(50,0);
Mouse.end();
}
}

Look at the “State Change Detection” example in the IDE.

I tried but this had no help but thanks for trying to help. My reason is that the code keeps repeating the mouse movement until I stop it unlike me having to press the button. But what I need is to press the button execute the mouse movement once only.

I down for more responses if guys want to help me.

Show us the modified code you tried using the technique you were linked to.

Here you go the code larryd, if you think I did something wrong please correct me:

#include <Mouse.h>
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
Mouse.move(50, 0);

}
}

if (buttonPushCounter % 2 == 0)
{
Mouse.move(50, 0);
}

Move the above lines of code into the if() section of code below.

if (buttonState == HIGH)
{
. . .
}

i.e. after this line:

buttonPushCounter++;

larryd is this how the code is supposed to look like:

if (buttonState == HIGH) {
Mouse.move(50, 0);
}
buttonPushCounter++;

Please correct me if I am wrong, I am not a good programmer.

Wrong. You don't have to be a good programmer right now. You just have to follow the directions in reply #5. Go back and read it again, then try again.

Well did I write the program correctly from what larry explained.

larryd I tried the suggestion that you gave me but I still have the same result please someone help me if you guys know the answer to my question.

Let’s see your current sketch.

I think he referred to the code you posted in #4

buttonState != lastButtonState

Still it is not helping my arduino leonardo run as I want it to.

Yo guys thx. I solved it. This is the final code:

#include <Mouse.h>
#include <Keyboard.h>

#define button 3
int buttonPress = 0;

void setup() {
pinMode(button, OUTPUT);
Serial.begin(9600);
Mouse.begin();
}

void loop() {
buttonPress = digitalRead(button);
if(buttonPress == 1) {
delay(50);
buttonPress = digitalRead(button);
if(buttonPress == 0){
Keyboard.press();
Keyboard.press();
Keyboard.press();
Serial1.println("90s");
Keyboard.press();
Mouse.move(50, 0);
}}
else{
delay(100);
}
}

#include <Mouse.h>
#include <Keyboard.h>

#define button 3
int buttonPress = 0;

void setup() {
  pinMode(button, OUTPUT);
  Serial.begin(9600);
  Mouse.begin();
}

void loop() {
  buttonPress = digitalRead(button);
  if (buttonPress == 1) {
    delay(50);
    buttonPress = digitalRead(button);
    if (buttonPress == 0) {
      Keyboard.press();
      Keyboard.press();
      Keyboard.press();
      Serial1.println("90s");
      Keyboard.press();
      Mouse.move(50, 0);
    }
  }
  else {
    delay(100);
  }
}