Professional Documents
Culture Documents
CSCI373_Week_11 - Joystick
CSCI373_Week_11 - Joystick
Week 11
Joystick
Joystick:
Joystick definition:
An Arduino joystick typically refers to a joystick device used in
combination with an Arduino microcontroller.
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;
} 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.