CuriousScientist

Arduino and joysticks - Part 1

Jul 18th, 2020
181
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/Zjyif7TKC0A
  3. //Please visit https://curiousscientist.tech
  4.  
  5. //16x2 LCD
  6. #include <LiquidCrystal_I2C.h>
  7. LiquidCrystal_I2C lcd(0x27, 16, 2);
  8.  
  9.  
  10. //Pins
  11. const byte Analog_X_pin = A0; //x-axis readings
  12. const byte Analog_Y_pin = A1; //y-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. bool Analog_Button = false; //button status
  20.  
  21. float timeNow; //timer for printing on the LCD
  22.  
  23. void setup()
  24. {
  25.  
  26.   //SERIAL
  27.   Serial.begin(115200);
  28.   //-----------------
  29.   //LCD
  30.   lcd.begin();
  31.   lcd.setCursor(0,0); //Defining positon to write from first row,first column .
  32.   lcd.print("Analog joystick");
  33.   lcd.setCursor(0,1);
  34.   lcd.print("Demonstration"); //You can write 16 Characters per line .
  35.   //
  36.   delay(3000); //wait 3 sec
  37.   PrintLCD();
  38.   //----------------------------------------------------------------------------
  39.  
  40.   //PINS
  41.   pinMode(Analog_X_pin, INPUT);
  42.   pinMode(Analog_Y_pin, INPUT);  
  43.   pinMode(Analog_Button_pin, INPUT_PULLUP);
  44.   attachInterrupt(digitalPinToInterrupt(Analog_Button_pin), ButtonPressed ,FALLING);
  45.   //----------------------------------------------------------------------------
  46.   timeNow = millis();
  47. }
  48.  
  49. void loop()
  50. {
  51.  
  52.   ReadAnalog();
  53.  
  54.    
  55.     if(millis() - timeNow > 200) //updating the LCD every 200 ms (this does not block the code!)
  56.     {
  57.     UpdateLCD();
  58.     timeNow = millis(); //resetting timer
  59.     }
  60.    
  61.  
  62. }
  63.  
  64. void ReadAnalog()
  65. {
  66.  
  67.   Analog_X = analogRead(Analog_X_pin);  
  68.   delay(10); //allowing a little time between two readings
  69.   Analog_Y = analogRead(Analog_Y_pin);    
  70.  
  71. }
  72.  
  73. void PrintLCD()
  74. {
  75.   //printing on the LCD
  76.   lcd.clear();
  77.   lcd.setCursor(0,0);
  78.   lcd.print("X-axis: ");
  79.   lcd.setCursor(8,0);
  80.   lcd.print(Analog_X);    //Print the value  
  81.   //
  82.   lcd.setCursor(0,1);
  83.   lcd.print("Y-axis: ");
  84.   lcd.setCursor(8,1);
  85.   lcd.print(Analog_Y);    //Print the value    
  86. }
  87.  
  88. void UpdateLCD()
  89. {    
  90.   //We only update the values that are changing
  91.   lcd.setCursor(8,0);
  92.   lcd.print("      ");
  93.   lcd.setCursor(8,0);
  94.   lcd.print(Analog_X);    //Print the value  
  95.   //Turn off serial if you do not use it with the PC
  96.   Serial.print("X: ");
  97.   Serial.println(Analog_X);
  98.   //  
  99.   lcd.setCursor(8,1);
  100.   lcd.print("      ");
  101.   lcd.setCursor(8,1);
  102.   lcd.print(Analog_Y);    //Print the value  
  103.   //Turn off serial if you do not use it with the PC
  104.   Serial.print("Y: ");
  105.   Serial.println(Analog_Y);
  106.  
  107.   if(Analog_Button == true)
  108.   {
  109.     lcd.setCursor(15,1);
  110.     lcd.print("1");
  111.     Serial.println("Button pressed");
  112.     Analog_Button = false; //reset button's status
  113.   }
  114.   else
  115.   {
  116.     lcd.setCursor(15,1);
  117.     lcd.print("0");
  118.   }
  119.  
  120. }
  121.  
  122.  
  123. void ButtonPressed()
  124. {
  125.     Analog_Button = true; //flip the variable
  126. }
Add Comment
Please, Sign In to add comment