Guest User

Untitled

a guest
Dec 9th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. //Master
  2. //Throttle Base Pro Micro ATmega32U4
  3. //I2C 12 buttons 2 axis
  4. //1 axis direct
  5.  
  6. #include "Joystick.h"
  7.  
  8. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  9.                    JOYSTICK_TYPE_MULTI_AXIS, 12, 0,
  10.                    true, true, true, false, false, false,
  11.                    false, true, false, false, false);
  12.  
  13. const bool testAutoSendMode = true;
  14.  
  15. int throttleInput = 0;
  16. int reverseValue = 0;
  17. int throttleValue = 0;
  18.  
  19. #include <Wire.h>
  20. #include <EasyTransferI2C.h>
  21.  
  22. //create object
  23. EasyTransferI2C ET;
  24.  
  25. struct RECEIVE_DATA_STRUCTURE {
  26.   int frontButton1;
  27.   int frontButton2;
  28.   int frontButton3;
  29.   int thumbButton1;
  30.   int thumbButton2;
  31.   int thumbButton3;
  32.   int encoderButton;
  33.   int aStickButton;
  34.   int dPadN;
  35.   int dPadE;
  36.   int dPadS;
  37.   int dPadW;
  38.   int aStickHorizontal;
  39.   int aStickVertical;
  40. };
  41.  
  42. //give a name to the group of data
  43. RECEIVE_DATA_STRUCTURE mydata;
  44.  
  45. //define slave i2c address
  46. #define I2C_SLAVE_ADDRESS 9
  47.  
  48. const int ledPin =  17;
  49. int ledState = LOW;
  50.  
  51. void setup() {
  52.   Joystick.setXAxisRange(0, 1023);
  53.   Joystick.setYAxisRange(0, 1023);
  54.   Joystick.setThrottleRange(0, 1023);
  55.   Joystick.setZAxisRange(0, 1023);
  56.   Joystick.begin();
  57.   Wire.begin(I2C_SLAVE_ADDRESS);
  58.   //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  59.   ET.begin(details(mydata), &Wire);
  60.   //define handler function on receiving data
  61.   Wire.onReceive(receive);
  62.  
  63.   pinMode(ledPin, OUTPUT);
  64.   Serial.begin(9600);
  65. }
  66.  
  67. void loop() {
  68.   //check and see if a data packet has come in.
  69.   if (ET.receiveData()) {
  70.  
  71.     digitalWrite(ledPin, LOW);
  72.   } else {
  73.     // turn LED off:
  74.  
  75.     Serial.println("No data");
  76.     if (ledState == LOW) {
  77.       ledState = HIGH;
  78.     } else {
  79.       ledState = LOW;
  80.     }
  81.     digitalWrite(ledPin, ledState);
  82.     delay(100);
  83.  
  84.   }
  85.  
  86.   readAnalog();
  87.  
  88.   Joystick.setXAxis(mydata.aStickHorizontal);
  89.   Joystick.setYAxis(mydata.aStickVertical);
  90.   Joystick.setThrottle(throttleValue);
  91.   Joystick.setZAxis(reverseValue);
  92.   Joystick.setButton(0, !mydata.frontButton1);
  93.   Joystick.setButton(1, !mydata.frontButton2);
  94.   Joystick.setButton(2, !mydata.frontButton3);
  95.   Joystick.setButton(3, !mydata.thumbButton1);
  96.   Joystick.setButton(4, !mydata.thumbButton2);
  97.   Joystick.setButton(5, !mydata.thumbButton3);
  98.   Joystick.setButton(6, !mydata.encoderButton);
  99.   Joystick.setButton(7, !mydata.aStickButton);
  100.   Joystick.setButton(8, !mydata.dPadN);
  101.   Joystick.setButton(9, !mydata.dPadE);
  102.   Joystick.setButton(10, !mydata.dPadS);
  103.   Joystick.setButton(11, !mydata.dPadW);
  104. }
  105.  
  106. void readAnalog()
  107. {
  108.   throttleInput = 1023 - analogRead(A7);
  109.  
  110.   if (throttleInput >= 950)
  111.   {
  112.     throttleValue = 1023;
  113.     reverseValue = 512;
  114.   }
  115.   else if (throttleInput >= 532)
  116.   {
  117.     throttleValue = (throttleInput - 512) * 2;
  118.     reverseValue = 512;
  119.   }
  120.   else if (throttleInput <= 50)
  121.   {
  122.     throttleValue = 0;
  123.     reverseValue = 0;
  124.   }
  125.   else if (throttleInput <= 492)
  126.   {
  127.     throttleValue = 0;
  128.     reverseValue = throttleInput;
  129.   }
  130.   else
  131.   {
  132.     throttleValue = 0;
  133.     reverseValue = 512;
  134.   }
  135. }
  136.  
  137.  
  138. void receive(int numBytes) {}
Add Comment
Please, Sign In to add comment