Advertisement
microrobotics

5 Direction Joystick PCB module

Apr 17th, 2023
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. In this code, the joystick pins are connected to digital input pins 3, 4, 5, 6, 7, 8, and 9, respectively. The code reads the state of each pin using the digitalRead() function and prints their states to the serial monitor.
  3.  
  4. Again, note that the specific pin mappings and pin states may vary depending on the joystick module you are using. You should consult the module's documentation or contact the manufacturer for more information on how to interface with the module.
  5. */
  6.  
  7. const int UP_PIN = 3; // Joystick up
  8. const int DOWN_PIN = 4; // Joystick down
  9. const int LEFT_PIN = 5; // Joystick left
  10. const int RIGHT_PIN = 6; // Joystick right
  11. const int MIDDLE_PIN = 7; // Joystick middle
  12. const int SET_PIN = 8; // Joystick set
  13. const int RESET_PIN = 9; // Joystick reset
  14.  
  15. void setup() {
  16.   pinMode(UP_PIN, INPUT_PULLUP);
  17.   pinMode(DOWN_PIN, INPUT_PULLUP);
  18.   pinMode(LEFT_PIN, INPUT_PULLUP);
  19.   pinMode(RIGHT_PIN, INPUT_PULLUP);
  20.   pinMode(MIDDLE_PIN, INPUT_PULLUP);
  21.   pinMode(SET_PIN, INPUT_PULLUP);
  22.   pinMode(RESET_PIN, INPUT_PULLUP);
  23.   Serial.begin(9600);
  24. }
  25.  
  26. void loop() {
  27.   int upState = digitalRead(UP_PIN);
  28.   int downState = digitalRead(DOWN_PIN);
  29.   int leftState = digitalRead(LEFT_PIN);
  30.   int rightState = digitalRead(RIGHT_PIN);
  31.   int middleState = digitalRead(MIDDLE_PIN);
  32.   int setState = digitalRead(SET_PIN);
  33.   int resetState = digitalRead(RESET_PIN);
  34.  
  35.   // Print values to serial monitor
  36.   Serial.print("Up: ");
  37.   Serial.print(upState);
  38.   Serial.print(" Down: ");
  39.   Serial.print(downState);
  40.   Serial.print(" Left: ");
  41.   Serial.print(leftState);
  42.   Serial.print(" Right: ");
  43.   Serial.print(rightState);
  44.   Serial.print(" Middle: ");
  45.   Serial.print(middleState);
  46.   Serial.print(" Set: ");
  47.   Serial.print(setState);
  48.   Serial.print(" Reset: ");
  49.   Serial.println(resetState);
  50.  
  51.   delay(100);
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement