#include "Mouse.h"
      
      // set pin numbers for the five buttons:
      const int upButton = 2;
      const int downButton = 3;
      const int leftButton = 4;
      const int rightButton = 5;
      const int mouseButton = 6;
      int range = 5; // output range of X or Y movement; affects movement speed
      int responseDelay = 10; // response delay of the mouse, in ms
      void setup() {
         // initialize the buttons' inputs:
         pinMode(upButton, INPUT);
         pinMode(downButton, INPUT);
         pinMode(leftButton, INPUT);
         pinMode(rightButton, INPUT);
         pinMode(mouseButton, INPUT);
         // initialize mouse control:
         Mouse.begin();
      }
      void loop() {
         // read the buttons:
         int upState = digitalRead(upButton);
         int downState = digitalRead(downButton);
         int rightState = digitalRead(rightButton);
         int leftState = digitalRead(leftButton);
         int clickState = digitalRead(mouseButton);
         // calculate the movement distance based on the button states:
         int xDistance = (leftState - rightState) * range;
         int yDistance = (upState - downState) * range;
         // if X or Y is non-zero, move:
         if ((xDistance != 0) || (yDistance != 0)) {
            Mouse.move(xDistance, yDistance, 0);
         }
         // if the mouse button is pressed:
         if (clickState == HIGH) {
            // if the mouse is not pressed, press it:
            if (!Mouse.isPressed(MOUSE_LEFT)) {
               Mouse.press(MOUSE_LEFT);
            }
         } else {                           // else the mouse button is not pressed:
            // if the mouse is pressed, release it:
            if (Mouse.isPressed(MOUSE_LEFT)) {
               Mouse.release(MOUSE_LEFT);
            }
         }
         // a delay so the mouse does not move too fast:
         delay(responseDelay);
      }
      

      Cpp language logoArduino - Mouse Button Control

      uno
      arduino

      Using the Mouse library, you can control a computer's onscreen cursor with an Arduino Leonardo, Micro, or Due.

      This particular example uses five pushbuttons to move the onscreen cursor. Four of the buttons are directional (up, down, left, right) and one is for a left mouse click. Cursor movement from Arduino is always relative. Every time an input is read, the cursor's position is updated relative to its current position.

      Whenever one of the directional buttons is pressed, Arduino will move the mouse, mapping a HIGH input to a range of 5 in the appropriate direction.

      The fifth button is for controlling a left-click from the mouse. When the button is released, the computer will recognize the event.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.