Skip to main content

Arduino Beginner Experiments: Arduino and Joystick Module part 2

HELLO THERE!!!

    How are you guys? I hope everyone is fine and in a good mood always. So, I decided to continue my post last week by implementing the Joystick module as a mouse. In this experiment, the joystick will control the mouse movement and left button click.


HARDWARE:
1.Arduino Leonardo
2.Joystick Module



SOFTWARE:
1.Arduino IDE

CIRCUIT ASSEMBLY
 The assembly of the circuit is similar with what we did last week:


Joystick Module --------------->Arduino
GND                                            GND
5V                                                  5V
VRx                                              (any analog pin)
VRy                                              (any analog pin)
SW                                                (any digital pin)




The schematic of the circuit


  CODING THE ARDUINO 
  The code below is taken from the Arduino website. I also added a piece of code which enables the function of the button. The button sometimes take more than a few seconds to function though.   

#include<Mouse.h>

const int xAxis = A1;         //analog sensor for X axis  
const int yAxis = A2;         // analog sensor for Y axis

int range = 12;               // output range of X or Y movement
int responseDelay = 2;       // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value
int minima[] = { 
  1023, 1023};                // actual analogRead minima for {x, y}
int maxima[] = {
  0,0};                       // actual analogRead maxima for {x, y}
int axis[] = {
  xAxis, yAxis};              // pin numbers for {x, y}
int mouseReading[2];          // final mouse readings for {x, y}


void setup() {
  pinMode(digitalRead(2), INPUT); //initialize button as input
 Mouse.begin();


}

void loop() {

// read and scale the two axes:
  int xReading = readAxis(0);
  int yReading = readAxis(1);

if(digitalRead(2) == HIGH){ //if button gives high value,enable click
    Mouse.click();
  } 
// move the mouse:
    Mouse.move(xReading, yReading, 0);
    delay(responseDelay);
    
}

/*
  reads an axis (0 or 1 for x or y) and scales the 
  analog input range to a range from 0 to <range>
*/

int readAxis(int axisNumber) {
  int distance = 0;    // distance from center of the output range

  // read the analog input:
  int reading = analogRead(axis[axisNumber]);

// of the current reading exceeds the max or min for this axis,
// reset the max or min:
  if (reading < minima[axisNumber]) {
    minima[axisNumber] = reading;
  }
  if (reading > maxima[axisNumber]) {
    maxima[axisNumber] = reading;
  }

  // map the reading from the analog input range to the output range:
  reading = map(reading, minima[axisNumber], maxima[axisNumber], 0, range);

 // if the output reading is outside from the
 // rest position threshold,  use it:
  if (abs(reading - center) > threshold) {
    distance = (reading - center);
  } 

  // the Y axis needs to be inverted in order to 
  // map the movemment correctly:
  if (axisNumber == 1) {
    distance = -distance;
  }

  // return the distance for this axis:
  return distance;
}    
      Upload the code to Arduino and play with the joystick!! Move the joystick module around and see your mouse move correspondingly. Click down and see the mouse performs a left click (might take some delays). Well, that's all from me, guys. I hope to see you guys next week. Till then, HAPPY TINKERING

Comments

Popular posts from this blog

Arduino and Multi Function Shield: Pots,LEDs and buzzers

HELLO THERE!!!!!    How are you guys? I hope everyone is fine and in good mood always. This week, I continue my tinkerings with the Multi Function Shield. In this experiment, I used the potentiometer in the shield to control the LEDs in the shield. Without further wasting time, let's jump in straight to the experiment!! HARDWARE 1.ARDUINO UNO  2.MULTI-FUNCTION SHIELD SOFTWARE: 1. ARDUINO IDE CIRCUIT ASSEMBLY    The circuit assembly is fairly simple. Just attach the multi function shield on top of Arduino properly. Please attach the shield properly as failure to do so would cause the shield  not to function as intended. CODING THE ARDUINO: Let's refer to the multi-function shield image below: Referring to the image of the shield, it can be seen that the potentiometer (blue object near buttons) is connected to pin A0 (analog 0)     int pot = A0; //declaring the pot ...

Arduino Beginner Experiments: Arduino and Light Blocking Sensor

HELLO THERE!!!     How are you guys? I hope everyone is fine and in a good mood always. This week, I would like to share with you guys my experiment on Arduino and Light Blocking Sensor. The objective of this experiment is to receive feedback from the sensor ( Digital and Analog) What is Light Blocking Sensor?      A linear hall sensor is a type of sensor which responds when the sensor is blocked out from light. The sensor is different for LDR's as LDR's responds to the amount of light it received while the light blocking sensor responds if the light is blocked out or not from the sensor.It can be used for both digital and analog measurements. HARDWARE: 1.Arduino UNO 2. Light Blocking Sensor SOFTWARE: 1.Arduino IDE CIRCUIT ASSEMBLY   Let's refer to the image of the blocking sensor below: The Connection of the Sensor to the Arduino is shown below: SENSOR  ----->       ARDUINO Signal   ...

Arduino and Keyes KY-017 Mercury Tilt Sensor

HELLO THERE!!!     How are you guys? I hope everyone is fine and good as always. I am back in tinkering after a few months of hiatus( busy stuff :) ). So, today i would like to share with you guys on KY-017, which is a Keyes Mercury Tilt Sensor. KY-017 Sensor KY-017      The picture above shows the KY-017 Sensor from Keyes. It has 3 pins, A ground, A vcc, and A signal pin. If you look closely, there is a mercury ball inside the bulb like object. The mercury ball acts like a switch for this sensor. KY-017 Working Procedure: 1. When the sensor is not tilted, the mercury ball will be at the bottom of the bulb. This will complete the circuit and turns on the LED of the sensor. The output that is sent through the signal pin will be LOW. 2.When the sensor is tilted, the mercury ball will be at the top of the bulb(near at the sharper end). This will complete break circuit and turns off the LED of the sensor. The output that is sent through t...