Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code reads the position of the joystick and the state of the button on the KY-023 Joystick Module and prints the result on the Serial Monitor of the Arduino IDE. The analogRead() function is used to read the X-axis and Y-axis analog values from the joystick, and the digitalRead() function is used to read the button state. The map() function is used to convert the analog values to joystick positions within the range of -100 to 100.
- To use this code, connect the X-axis and Y-axis outputs of the KY-023 Joystick Module to the analog input pins of your Arduino board (usually A0 and A1 for most Arduino boards), and connect the button output of the joystick to a digital input pin of the Arduino board (in this case, pin 2). The joystick should be powered with a 5V DC power source.
- Note that the joystick positions and button state may vary depending on the type and quality of the joystick module used.
- */
- const int x_pin = A0; // Pin connected to X-axis output of the joystick
- const int y_pin = A1; // Pin connected to Y-axis output of the joystick
- const int button_pin = 2; // Pin connected to the button output of the joystick
- void setup() {
- Serial.begin(9600);
- pinMode(button_pin, INPUT_PULLUP);
- }
- void loop() {
- // Read the analog values from the joystick
- int x_value = analogRead(x_pin);
- int y_value = analogRead(y_pin);
- // Convert the analog values to joystick positions
- int x_position = map(x_value, 0, 1023, -100, 100);
- int y_position = map(y_value, 0, 1023, -100, 100);
- // Read the button state
- int button_state = digitalRead(button_pin);
- // Print the joystick positions and button state
- Serial.print("Joystick position: ");
- Serial.print(x_position);
- Serial.print(", ");
- Serial.print(y_position);
- Serial.print(" Button state: ");
- Serial.println(button_state);
- // Wait for 100 milliseconds before taking the next reading
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement