Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CSCI373

Robotics Design & Coding

Week 11
Joystick
Joystick:
Joystick definition:
An Arduino joystick typically refers to a joystick device used in
combination with an Arduino microcontroller.

It consists of a stick that can be moved in different directions


and one or button. The joystick provides analog or digital
signals corresponding to the direction and magnitude of the
stick's movement, as well as the state of any buttons.

This input data can be read by the Arduino board, allowing for
various applications such as controlling motors, servos, games,
or other interactive projects.
Joystick wiring:
Joystick wiring:
A Joystick has 5 pins:
❑ GND pin: needs to be connected to GND (0V)
❑ VCC pin: needs to be connected to VCC
❑ VRX pin: outputs an analog value corresponding to the
horizontal position (called X-coordinate)
❑ VRY pin: outputs an analog value corresponding to the vertical
position (called Y-coordinate).
❑ SW pin: is the output from the pushbutton inside the joystick.
It’s normally open. If we use a pull-up resistor in this pin, the
SW pin will be HIGH when it is not pressed, and LOW when it
is pressed.
Add "ezButton" library:
Sketch → Include Library → Add .Zip Library
Joystick Example: Control a piezo from Joystick
#include <ezButton.h>
//create ezButton object attached to pin 2;
ezButton button(2);
int UD = 0, LR = 0, piezo = 9;

/*Arduino Micro Input Pins */


int IUP = A1, ILR = A0;
int MID = 0, LRMID = 0, UPMID = 0;
void setup() {
Serial.begin(9600);
pinMode(piezo, OUTPUT);
button.setDebounceTime(50);
//calibrate center
LRMID = analogRead(ILR);
UPMID = analogRead(IUP);
}
//MID = 0, UPMID = 0;
void loop() {
UD = analogRead(IUP);
LR = analogRead(ILR);
// UP-DOWN
if (UD < UPMID - MID) {
tone(piezo, 1000, 100);
Serial.println("UP");
// TODO: add your task here
} else {
noTone(piezo);
}
if (UD > UPMID + MID) {
tone(piezo, 1100, 100);
Serial.println("Down");
// TODO: add your task here
} else {
noTone(piezo);
}
// Left-RIGHT
if (LR < LRMID - MID) {
tone(piezo, 1200, 100);
Serial.println("left");
// TODO: add your task here

} else {
noTone(piezo);
}
if (LR > LRMID + MID) {
tone(piezo, 1300, 100);
Serial.println("Right");
// TODO: add your task here

} else {
noTone(piezo);
}
// Button
// MUST call the button loop() function first
button.loop();
if (button.isPressed()) {
Serial.println("The button is pressed");
tone(piezo, 1400, 100);
// TODO: add your task here

}
if (button.isReleased()) {
Serial.println("The button is released");
// TODO: add your task here

} delay(100);
}
Practice Exercise-1
Make Car system using the Joystick with 4
LEDs and one piezo.
We have one joystick, one piezo and 4 LEDs:
❑If the user clicks on the button of the joystick the
piezo should play a sound.
❑If the user push the joystick's thump to Up/Down
the red/green LEDs should light up.
❑If the user push the joystick's thump to Left/Right
the blue/yellow LEDs should light up.
Practice Exercise-2
We have one joystick and one seven segment display:
❑If the user clicks on the button of the joystick the
seven segment display should display the number 0.
❑If the user push the joystick's thump to Up/Down
the seven segment display should increase/decrease
the number by 1.
❑If the user push the joystick's thump to Left/Right
the seven segment display should increase/decrease
the number by 2.

You might also like