CuriousScientist

Arduino with 3-axis joystick

Jul 23rd, 2020 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to this tutorial video: https://youtu.be/97yzEBo4DZo
  3. //Please visit https://curiousscientist.tech
  4.  
  5. //16x2 LCD
  6. #include <LiquidCrystal_I2C.h>
  7. LiquidCrystal_I2C lcd(0x27, 16, 2); //A4 - SDA, A5 - SCK (SCL)
  8.  
  9. //Pins
  10. const byte Analog_X_pin = A0; //x-axis readings
  11. const byte Analog_Y_pin = A1; //y-axis readings
  12. const byte Analog_R_pin = A2; //R-axis readings
  13. //const byte Analog_Button_pin = 2; //attachinterrupt compatible pin
  14.  
  15.  
  16. //Variables
  17. int Analog_X = 0; //x-axis value
  18. int Analog_Y = 0; //y-axis value
  19. int Analog_R = 0; //R-axis value
  20. //bool Analog_Button = false; //button status
  21.  
  22. float timeNow; //timer for printing on the LCD
  23.  
  24. void setup()
  25. {
  26.  
  27.   //SERIAL
  28.   //Serial.begin(9600);
  29.   //-----------------
  30.   //LCD
  31.   lcd.begin();
  32.   lcd.setCursor(0,0); //Defining position to write from first row,first column .
  33.   lcd.print("Analog joystick");
  34.   lcd.setCursor(0,1);
  35.   lcd.print("Demonstration"); //You can write 16 Characters per line .
  36.   //
  37.   delay(3000); //wait 3 sec
  38.   PrintLCD();
  39.   //----------------------------------------------------------------------------  
  40.   //PINS
  41.   pinMode(Analog_X_pin, INPUT);
  42.   pinMode(Analog_Y_pin, INPUT);  
  43.   pinMode(Analog_R_pin, INPUT);  
  44.   //pinMode(Analog_Button_pin, INPUT_PULLUP);
  45.   //attachInterrupt(digitalPinToInterrupt(Analog_Button_pin), ButtonPressed ,FALLING);
  46.   //----------------------------------------------------------------------------
  47.   timeNow = millis();
  48. }
  49.  
  50. void loop()
  51. {
  52.  
  53.   ReadAnalog(); //reading x,y, and r.
  54.  
  55.    
  56.     if(millis() - timeNow > 200) //updating the LCD every 200 ms
  57.     {
  58.     UpdateLCD();
  59.     timeNow = millis(); //resetting timer
  60.     }
  61.    
  62.  
  63. }
  64.  
  65. void ReadAnalog()
  66. {
  67.  
  68.   Analog_X = analogRead(Analog_X_pin);  
  69.   delay(10); //allowing a little time between two readings
  70.   Analog_Y = analogRead(Analog_Y_pin);    
  71.   delay(10); //allowing a little time between two readings
  72.   Analog_R = analogRead(Analog_R_pin);    
  73.  
  74. }
  75.  
  76. void PrintLCD()
  77. {
  78.   //printing on the LCD - these are permanent
  79.   lcd.clear();
  80.   lcd.setCursor(0,0);
  81.   lcd.print(" X    Y     R ");  
  82. }
  83.  
  84. void UpdateLCD()
  85. {    
  86.   //We only update the values that are changing  
  87.   lcd.setCursor(0,1);
  88.   lcd.print("    ");
  89.   lcd.setCursor(0,1);
  90.   lcd.print(Analog_X);
  91.   lcd.setCursor(5,1);
  92.   lcd.print("    ");
  93.   lcd.setCursor(5,1);
  94.   lcd.print(Analog_Y);
  95.   lcd.setCursor(11,1);
  96.   lcd.print("    ");
  97.   lcd.setCursor(11,1);
  98.   lcd.print(Analog_R);//Print the value  
  99. }
  100.  
  101. //Not used in this code, but I keep it in the code, so you can add a button easily. Just uncomment the related parts.
  102. void ButtonPressed()
  103. {
  104.     Analog_Button = true; //flip the variable
  105. }
Add Comment
Please, Sign In to add comment