Advertisement
Guest User

GimbalStick

a guest
Jul 15th, 2019
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. // A joystick with calibration function and/or some added buttons.
  2. // Using RC joystick gimbal (dual pot type) and Arduino Pro Micro(32u4)
  3. // User holds calibrate button for 2s to enter calmode.  Stick is moved to extremes then cal button pressed again to exit and save.
  4.  
  5. // Makes use of ArduinoJoystickLibrary by MHeironimus
  6. // https://github.com/MHeironimus/ArduinoJoystickLibrary
  7. // JC_Button library by Jack Christensen
  8. // https://github.com/JChristensen/JC_Button
  9. // and the default EEPROM library from Arduino
  10. // By: Daniel Spargo for Technology for Ageing and Disability QLD (TADQ)
  11.  
  12. #include "Arduino.h"
  13. #include "Joystick.h"
  14. #include "EEPROM.h"
  15. #include "JC_Button.h"
  16. #include "FadeLed.h"
  17.  
  18. #define xAxis A0
  19. #define yAxis A2
  20. #define UpButton 15
  21. #define DownButton 14
  22. #define LeftButton 16
  23. #define RightButton 10
  24. #define CalButton 9
  25. #define LEDpin 3
  26. #define RangeScaleFactor 0.85
  27. #define debounce 10
  28.  
  29. //EEPROM addresses to be used
  30. int XcentaddH = 0, XcentaddL = 1;
  31. int YcentaddH = 2, YcentaddL = 3;
  32. int XrangeAdjaddH = 4, XrangeAdjaddL = 5;
  33. int YrangeAdjaddH = 6, YrangeAdjaddL = 7;
  34. //Various stick position vals
  35. int16_t xVal = 0,yVal = 0;
  36. int16_t xValAdj = 0,yValAdj = 0;
  37. int16_t Ycenter = 0,Xcenter = 0;
  38. int16_t Ymax = 0,Ymin = 0;
  39. int16_t Xmax = 0,Xmin = 0;
  40. int16_t YmaxAdj = 0, XmaxAdj = 0;
  41. int16_t Xrange = 0, Yrange = 0;
  42. int16_t XrangeAdj = 0, YrangeAdj = 0;
  43. //Flag for indicating status of calibration mode
  44. bool calmode = 0;
  45.  
  46. // Create Joystick
  47. // Joystick(hidReportID,joystickType,buttonCount,hatSwitchCount,rest are axes enable-X_Y_Z_RX_RY_RZ)
  48. Joystick_ Joystick(0x04, JOYSTICK_TYPE_JOYSTICK, 4, 0,true,true,false,false,false,false,false,false,false,false,false);
  49.  
  50. //Create Button Debouncer and Reader
  51. Button UpBtn(UpButton,debounce,true,true);
  52. Button DownBtn(DownButton,debounce,true,true);
  53. Button LeftBtn(LeftButton,debounce,true,true);
  54. Button RightBtn(RightButton,debounce,true,true);
  55. Button CalBtn(CalButton,debounce,true,true);
  56.  
  57. //Create FadeLed
  58. FadeLed BreatheLED(LEDpin);
  59.  
  60. void setup() {
  61.   Joystick.begin();
  62.   //Joystick.setYAxisRange(0,1023);
  63.   //Joystick.setXAxisRange(0,1023);
  64.  
  65.   //Set params for LED
  66.   BreatheLED.setTime(3000, true);
  67.   BreatheLED.set(127);
  68.  
  69.  
  70.   //INPUTS
  71.   //Analog inputs from stick pots
  72.   pinMode(xAxis, INPUT);
  73.   pinMode(yAxis, INPUT);
  74.   //Button inputs, set internal pullups
  75.   UpBtn.begin();
  76.   DownBtn.begin();
  77.   LeftBtn.begin();
  78.   RightBtn.begin();
  79.   CalBtn.begin();
  80.  
  81.   //Get values from EEPROM and calculate max vals
  82.   //Get high byte shift it left then append low byte
  83.   Xcenter = EEPROM.read(XcentaddH);
  84.   Xcenter = Xcenter<<8;
  85.   Xcenter |= EEPROM.read(XcentaddL);
  86.   Ycenter = EEPROM.read(YcentaddH);
  87.   Ycenter = Ycenter<<8;
  88.   Ycenter |= EEPROM.read(YcentaddL);
  89.   XrangeAdj = EEPROM.read(XrangeAdjaddH);
  90.   XrangeAdj = XrangeAdj<<8;
  91.   XrangeAdj |= EEPROM.read(XrangeAdjaddL);
  92.   YrangeAdj = EEPROM.read(YrangeAdjaddH);
  93.   YrangeAdj = YrangeAdj<<8;
  94.   YrangeAdj |= EEPROM.read(YrangeAdjaddL);
  95.   //Max Value for axis is center + adjusted range
  96.   XmaxAdj = Xcenter+XrangeAdj;
  97.   YmaxAdj = Ycenter+YrangeAdj;
  98. }
  99.  
  100. void loop() {
  101.   //Read analog inputs
  102.   xVal = analogRead(xAxis);
  103.   yVal = analogRead(yAxis);
  104.   //Read button inputs and set button or LED outputs
  105.   Joystick.setButton(0,UpBtn.read());
  106.   Joystick.setButton(1,DownBtn.read());
  107.   Joystick.setButton(2,LeftBtn.read());
  108.   Joystick.setButton(3,RightBtn.read());
  109.   CalBtn.read();
  110.   FadeLed::update();
  111.  
  112.    //Calibration button held for 2s? Enter calmode. Grab center vals.
  113.   if(CalBtn.pressedFor(2000)&&(!calmode)){
  114.     calmode = 1;
  115.     Ycenter = yVal;
  116.     Xcenter = xVal;
  117.     BreatheLED.off();
  118.   }
  119.   //constantly update max/min axis vals while in calmode
  120.   if(calmode){
  121.     if(xVal>Xmax)Xmax=xVal;
  122.     if(yVal>Ymax)Ymax=yVal;
  123.     if(xVal<Xmin)Xmin=xVal;
  124.     if(yVal<Ymin)Ymin=yVal;
  125.  
  126.     if(BreatheLED.done()){
  127.       if(BreatheLED.get()){
  128.         BreatheLED.off();
  129.       }
  130.       else{
  131.         BreatheLED.on();
  132.       }
  133.     }
  134.   }
  135.   //If we are in calmode and a rising edge is detected from CalBtn then exit calmode and update EEPROM.
  136.   if(CalBtn.wasPressed()&&calmode){
  137.     calmode = false;
  138.  
  139.     if((Xmax-Xcenter)>=(Xcenter-Xmin)){ //Find X smallrange
  140.         Xrange = Xcenter-Xmin;
  141.      }
  142.      if((Xmax-Xcenter)<=(Xcenter-Xmin)){
  143.         Xrange = Xmax-Xcenter;
  144.      }
  145.      if((Ymax-Ycenter)>=(Ycenter-Ymin)){ //Find Y smallrange
  146.         Yrange = Ycenter-Ymin;
  147.      }
  148.      if((Ymax-Ycenter)<=(Ycenter-Ymin)){
  149.         Yrange = Ymax-Ycenter;
  150.      }
  151.      //Adjust smallranges by a bit
  152.      XrangeAdj = round(Xrange*RangeScaleFactor);
  153.      YrangeAdj = round(Yrange*RangeScaleFactor);
  154.      
  155.      //Use adjusted smallranges to calculate adjusted max vals
  156.      XmaxAdj = Xcenter+XrangeAdj;
  157.      YmaxAdj = Ycenter+YrangeAdj;
  158.      //Store center and adjusted range values to EEPROM
  159.      EEPROM.write(XcentaddH,Xcenter>>8);
  160.      EEPROM.write(XcentaddL,Xcenter);
  161.      EEPROM.write(YcentaddH,Ycenter>>8);
  162.      EEPROM.write(YcentaddL,Ycenter);
  163.      EEPROM.write(XrangeAdjaddH,XrangeAdj>>8);
  164.      EEPROM.write(XrangeAdjaddL,XrangeAdj);
  165.      EEPROM.write(YrangeAdjaddH,YrangeAdj>>8);
  166.      EEPROM.write(YrangeAdjaddL,YrangeAdj);
  167.  
  168.      BreatheLED.stop();
  169.      BreatheLED.set(50);
  170.   }
  171.  
  172.   //Not in calmode? be a Joystick. Do Joystick things.
  173.   if(!calmode){
  174.   xVal = constrain(xVal, 0, 1023);
  175.   yVal = constrain(yVal, 0, 1023);
  176.   // Just ensuring center and max values that we send are in the expected range.  Can swap 1023 and 0 to flip axes orientation.
  177.   xValAdj = map(xVal,Xcenter,XmaxAdj, 511, 0);
  178.   yValAdj = map(yVal,Ycenter,YmaxAdj, 511, 1023);
  179.   //Send current stick positions
  180.   Joystick.setXAxis(xValAdj);
  181.   Joystick.setYAxis(yValAdj);
  182.   }
  183.  
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement