Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Master
- //Throttle Base Pro Micro ATmega32U4
- //I2C 12 buttons 2 axis
- //1 axis direct
- #include "Joystick.h"
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
- JOYSTICK_TYPE_MULTI_AXIS, 12, 0,
- true, true, true, false, false, false,
- false, true, false, false, false);
- const bool testAutoSendMode = true;
- int throttleInput = 0;
- int reverseValue = 0;
- int throttleValue = 0;
- #include <Wire.h>
- #include <EasyTransferI2C.h>
- //create object
- EasyTransferI2C ET;
- struct RECEIVE_DATA_STRUCTURE {
- int frontButton1;
- int frontButton2;
- int frontButton3;
- int thumbButton1;
- int thumbButton2;
- int thumbButton3;
- int encoderButton;
- int aStickButton;
- int dPadN;
- int dPadE;
- int dPadS;
- int dPadW;
- int aStickHorizontal;
- int aStickVertical;
- };
- //give a name to the group of data
- RECEIVE_DATA_STRUCTURE mydata;
- //define slave i2c address
- #define I2C_SLAVE_ADDRESS 9
- const int ledPin = 17;
- int ledState = LOW;
- void setup() {
- Joystick.setXAxisRange(0, 1023);
- Joystick.setYAxisRange(0, 1023);
- Joystick.setThrottleRange(0, 1023);
- Joystick.setZAxisRange(0, 1023);
- Joystick.begin();
- Wire.begin(I2C_SLAVE_ADDRESS);
- //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
- ET.begin(details(mydata), &Wire);
- //define handler function on receiving data
- Wire.onReceive(receive);
- pinMode(ledPin, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- //check and see if a data packet has come in.
- if (ET.receiveData()) {
- digitalWrite(ledPin, LOW);
- } else {
- // turn LED off:
- Serial.println("No data");
- if (ledState == LOW) {
- ledState = HIGH;
- } else {
- ledState = LOW;
- }
- digitalWrite(ledPin, ledState);
- delay(100);
- }
- readAnalog();
- Joystick.setXAxis(mydata.aStickHorizontal);
- Joystick.setYAxis(mydata.aStickVertical);
- Joystick.setThrottle(throttleValue);
- Joystick.setZAxis(reverseValue);
- Joystick.setButton(0, !mydata.frontButton1);
- Joystick.setButton(1, !mydata.frontButton2);
- Joystick.setButton(2, !mydata.frontButton3);
- Joystick.setButton(3, !mydata.thumbButton1);
- Joystick.setButton(4, !mydata.thumbButton2);
- Joystick.setButton(5, !mydata.thumbButton3);
- Joystick.setButton(6, !mydata.encoderButton);
- Joystick.setButton(7, !mydata.aStickButton);
- Joystick.setButton(8, !mydata.dPadN);
- Joystick.setButton(9, !mydata.dPadE);
- Joystick.setButton(10, !mydata.dPadS);
- Joystick.setButton(11, !mydata.dPadW);
- }
- void readAnalog()
- {
- throttleInput = 1023 - analogRead(A7);
- if (throttleInput >= 950)
- {
- throttleValue = 1023;
- reverseValue = 512;
- }
- else if (throttleInput >= 532)
- {
- throttleValue = (throttleInput - 512) * 2;
- reverseValue = 512;
- }
- else if (throttleInput <= 50)
- {
- throttleValue = 0;
- reverseValue = 0;
- }
- else if (throttleInput <= 492)
- {
- throttleValue = 0;
- reverseValue = throttleInput;
- }
- else
- {
- throttleValue = 0;
- reverseValue = 512;
- }
- }
- void receive(int numBytes) {}
Add Comment
Please, Sign In to add comment