Joystick Sensor(KY-023) - Author AC
Have you ever wondered what is behind the amazing joystick behind your video game console? With the KY-023, you can see the action happening before your eyes! You can use the Arduino Uno board to show this amazing process work.
What do you need?
You need an
- Arduino Uno Board
- Arduino IDE installed on a computer
- Arduino KY-023 sensor(Joystick)
- Cable to connect the Arduino to the Computer
- female-to-male wires
How to Wire the Arduino:
Follow the wiring above to connect the pins to the joystick. (Source: ArduinoGetStarted.com)
The Code
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define LEFT_THRESHOLD 400
#define RIGHT_THRESHOLD 800
#define UP_THRESHOLD 400
#define DOWN_THRESHOLD 800
#define COMMAND_NO 0x00
#define COMMAND_LEFT 0x01
#define COMMAND_RIGHT 0x02
#define COMMAND_UP 0x04
#define COMMAND_DOWN 0x08
int xValue = 0 ; // To store value of the X axis
int yValue = 0 ; // To store value of the Y axis
int command = COMMAND_NO;
void setup() {
Serial.begin(9600) ;
}
void loop() {
// read analog X and Y analog values
xValue = analogRead(VRX_PIN);
yValue = analogRead(VRY_PIN);
// converts the analog value to commands
// reset commands
command = COMMAND_NO;
// check left/right commands
if (xValue < LEFT_THRESHOLD)
command = command | COMMAND_LEFT;
else if (xValue > RIGHT_THRESHOLD)
command = command | COMMAND_RIGHT;
// check up/down commands
if (yValue < UP_THRESHOLD)
command = command | COMMAND_UP;
else if (yValue > DOWN_THRESHOLD)
command = command | COMMAND_DOWN;
// NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS
// print command to serial and process command
if (command & COMMAND_LEFT) {
Serial.println("COMMAND LEFT");
// TODO: add your task here
}
if (command & COMMAND_RIGHT) {
Serial.println("COMMAND RIGHT");
// TODO: add your task here
}
if (command & COMMAND_UP) {
Serial.println("COMMAND UP");
// TODO: add your task here
}
if (command & COMMAND_DOWN) {
Serial.println("COMMAND DOWN");
// TODO: add your task here
}
}
This code provides instructions to move which way. To move up, down, right, or left.
(Source: arduinogetstarted.com)
This blog and experiment is inspired from arduinogetstarted.com and written by Avviin Chandrasekaran, 8th Grade Student at Kennedy Middle School, Cupertino
California.
Avviin is interested in learning Science and computer concepts and programming.
He was doing Scratch programming from 7 year old and made multiple Youtube
Videos.
Comments
Post a Comment