Skip to content

Joystick

Why Joystick?

We needed a component to control the LCD, our first choice, was the Rotary Encoder that has been done my group mate Hassan Mandeek you can check it Here. However, we faced several problems with it:

  • There is no way to rest the position, it has to be taken back to its zero state position manually after each use.

  • When rotating the rotary encoder, you have to rotate it very slowly, otherwise it would not operate ideally.

Due to these problems, we have discarded it and resorted to the Joystick. The joystick solve theses problems since it returns automatically to its original zero state and can function well with fast movement.


How does it works?

As you can see it picture above, the Joystick has 5 pins (Ground, VCC, VRX, VRY, SW).

  • VRX: is responsible for movement along the x-axis.

  • VRY: is responsible for the movement on the y-axis.

  • SW: is responsible for the button.

The Joy stick function as a coordinates system that. It has a range from 0 to 1023 in each axis.

The above picture demonstrates the how the coordinates system works, as you can see, the origin point or the (0, 0) point is (512, 512) in this case.

The Joystick uses a function called the map function, to be able to utilize the Joystick, you have to understand this function.

target = map(source, sourceLow, sourceHigh, targetLow, targetHigh)

Target: the target is end result of the movement of your joystick. TargetLow: The low end of the target. TargetHigh: The high end of teh target.

Source: is the thing that you want to be map (in most cases it is the x value / the y value)
SourceLow: The low end of the source.
SourceHigh: The high end of the source.

So basically, the map function will map the sourceLow to the TargetLow and will map the SourceHigh to the TargetHigh.


Coding

Click to expand
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// define the pins of the joy stick.
int xPin = A0;
int yPin = A1; 

// define veriables to the values, to read later. 
int xPosition = 0;
int yPosition = 0;

// map function sets the limits for the joy stick
int mapX = 0;
int mapY = 0;

LiquidCrystal_I2C lcd(0x27,  16, 2);


void setup() {
  // put your setup code here, to run once:
  lcd.init(); // initilize the screen
  lcd.backlight(); // Turn on the light of the screen
  lcd.clear(); // Clears any previous data in the screen
  Serial.begin(9600); 


    // define the state of the pins
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
}

void loop() {
  // analg read the values to use later in the map function. 
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);

// target = map(source, sourceLow, sourceHigh, targetLow, targetHigh) 

  mapX = map(xPosition, 0, 1023, -512, 512);
  mapY = map(yPosition, 0, 1023, -512, 512);

if( ( mapX <-200) && (mapY > 200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Panadol   ");

}

if((mapX >200) && (mapY <200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Accutane ");

}

if((mapX <200) && (mapY <-200)){
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("strepsils ");

}

if((mapX >-200) && (mapY >200)) {
lcd.setCursor(0,0);
lcd.print("Medication Type: ");

lcd.setCursor(0,1);
lcd.print("Vicks    ");

}
}

results:


Last update: September 14, 2024