Advertisement
Guest User

Untitled

a guest
Oct 18th, 2016
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include "Joystick.h"
  2.  
  3. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  4.                    JOYSTICK_TYPE_MULTI_AXIS, 32, 0,
  5.                    true, true, true, false, false, false,
  6.                    false, true, false, false, false);
  7.  
  8. const bool testAutoSendMode = true;
  9.  
  10. #include <Wire.h>
  11. #include <LiquidCrystal_I2C.h>
  12.  
  13. LiquidCrystal_I2C lcd(0x20, 20, 4); // set the LCD address to 0x20 for a 16 chars and 2 line display
  14.  
  15. unsigned long previousMillis = 0;
  16. const long interval = 200;
  17.  
  18. int xValue = 0;
  19. int yValue = 0;
  20. int throttleInput = 0;
  21. int reverseValue = 0;
  22. int throttleValue = 0;
  23.  
  24. void setup() {
  25.   Joystick.setXAxisRange(0, 1023);
  26.   Joystick.setYAxisRange(0, 1023);
  27.   Joystick.setThrottleRange(0, 1023);
  28.   Joystick.setZAxisRange(0, 1023);
  29.   Joystick.begin();
  30.   //Serial.begin(9600);
  31.   lcd.init();
  32.   lcd.init();
  33.  
  34.   lcd.backlight();
  35. }
  36.  
  37. void loop() {
  38.  
  39.   readAnalog();
  40.  
  41.   Joystick.setXAxis(xValue);
  42.   Joystick.setYAxis(yValue);
  43.   Joystick.setThrottle(throttleValue);
  44.   Joystick.setZAxis(reverseValue);
  45.  
  46.   //serialOutput();
  47.  
  48.   unsigned long currentMillis = millis();
  49.  
  50.   if (currentMillis - previousMillis >= interval) {
  51.     previousMillis = currentMillis;
  52.  
  53.     updateLCD();
  54.   }
  55.  
  56. }
  57.  
  58. void readAnalog()
  59. {
  60.   xValue = analogRead(A3);
  61.   yValue = 1023 - analogRead(A2);
  62.   throttleInput = analogRead(A1);
  63.  
  64.   if (throttleInput >= 950)
  65.   {
  66.     throttleValue = 1023;
  67.     reverseValue = 512;
  68.   }
  69.   else if (throttleInput >= 532)
  70.   {
  71.     throttleValue = (throttleInput - 512) * 2;
  72.     reverseValue = 512;
  73.   }
  74.   else if (throttleInput <= 50)
  75.   {
  76.     throttleValue = 0;
  77.     reverseValue = 0;
  78.   }
  79.   else if (throttleInput <= 492)
  80.   {
  81.     throttleValue = 0;
  82.     reverseValue = throttleInput;
  83.   }
  84.   else
  85.   {
  86.     throttleValue = 0;
  87.     reverseValue = 512;
  88.   }
  89. }
  90.  
  91. void updateLCD()
  92. {
  93.   lcd.setCursor(0, 0);
  94.   lcd.print("ThumbV = ");
  95.   lcd.print(yValue);
  96.   lcd.print("   ");
  97.   lcd.setCursor(0, 1);
  98.   lcd.print("ThumbH = ");
  99.   lcd.print(xValue);
  100.   lcd.print("   ");
  101.   lcd.setCursor(0, 2);
  102.   lcd.print("Throttle = ");
  103.   lcd.print(throttleValue);
  104.   lcd.print("   ");
  105.   lcd.setCursor(0, 3);
  106.   lcd.print("Reverse = ");
  107.   lcd.print(reverseValue);
  108.   lcd.print("   ");
  109.   lcd.setCursor(15, 0);
  110.   lcd.print("    ");
  111.   lcd.setCursor(15, 0);
  112.   lcd.print(throttleInput);
  113. }
  114.  
  115.  
  116. void serialOutput()
  117. {
  118.   Serial.print("x = ");
  119.   Serial.print(xValue);
  120.   Serial.print(" y = ");
  121.   Serial.print(yValue);
  122.   Serial.print(" t = ");
  123.   Serial.print(throttleValue);
  124.   Serial.print(" t = ");
  125.   Serial.print(throttleValue);
  126.   Serial.println();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement