top of page
Search

Sonar Mouse click

  • Writer: Jason Ron
    Jason Ron
  • May 11, 2021
  • 2 min read

Updated: May 12, 2021

So here is a fun quick project that helps you play Minecraft without clicking... just put your hand right above the sonar detector and that does the mouse click for you. This project is part of my augmented gameplay projects to increase functionality of the game or playability.


For the sonar detector I used the HC-SR04 that was part of the Elegoo 37 sensors kit. So please use and download the sonar the library at

Then add the "new ping" library by adding the HC-SR04 . zip library to your Arduino sketch.


You do not need to buy the 37 sensors kit for this project just buy the HC-SR04 and download the free libraries and pdf reference.

Bill of materials

(1) Arduino Leonardo

(2) Ultrasonic distance sensor: HC-SR04



Wiring diagram


Code (for both left and right mouse clicks)


Arduino Code //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <NewPing.h>

#include <Mouse.h>


#define TRIGGER_PIN1 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN1 11 // Arduino pin tied to echo pin on the ultrasonic sensor.

#define TRIGGER_PIN2 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.

#define ECHO_PIN2 10 // Arduino pin tied to echo pin on the ultrasonic sensor.

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.



const int inputpin = 7;

int lastButtonState2 = 0;

int buttonState2 = 0;

int buttonPushCounter2 = 0;



NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

pinMode(inputpin, INPUT);

}


void loop() {


buttonState2 = digitalRead(inputpin);

if (buttonState2 != lastButtonState2) {

if (buttonState2 == LOW) {


buttonPushCounter2++;

}


}

lastButtonState2 = buttonState2;



unsigned int val1 = sonar1.ping(); // Send ping, get ping time in microseconds (uS).

unsigned int val2 = sonar2.ping(); // Send ping, get ping time in microseconds (uS).

delay(100); // Wait 500ms between pings (about 2 pings/sec). 29ms should be the shortest delay between pings.


Serial.print(val1);

Serial.print(" , ");

Serial.println(val2);


if (buttonPushCounter2 == 1)

{

Mouse.begin();



if(val1<600){

//Send the message

Mouse.press(MOUSE_LEFT);

}

else if(val2<600){

//Send the message

Mouse.press(MOUSE_RIGHT);

}

else{

Mouse.release(MOUSE_LEFT);

Mouse.release(MOUSE_RIGHT);

}

} //if button





} //loop



 
 
 

Comments


Post: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2018 by Jason JCAD. Proudly created with Wix.com

bottom of page