Advertisement
Guest User

Fortnite Controller

a guest
Jul 18th, 2018
4,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <I2Cdev.h>
  4. #include <MPU6050.h>
  5. #include <Mouse.h>
  6. #include <Keyboard.h>
  7.  
  8. MPU6050 mpu;
  9. int16_t ax, ay, az, gx, gy, gz;
  10. int16_t accx, accy, accz;
  11. int vx, vy;
  12. int TRIGGER_BUTTON = 5;
  13. int WEAPON_TOGGLE = 9;
  14. bool weaponModeOn = false;
  15. float angle;
  16.  
  17. //code for smoothing input
  18. int readIndex = 0;
  19. const int numReadings = 20;
  20. int angleReadings[numReadings];
  21. int total = 0;
  22. float averageAngle = 0.0;
  23.  
  24.  
  25. int oldZ = 0;
  26. int newZ = 0;
  27.  
  28. void setup() {
  29. Serial.begin(115200);
  30. Wire.begin();
  31. mpu.initialize();
  32. if (!mpu.testConnection()) {
  33. while (1);
  34. }
  35. pinMode(TRIGGER_BUTTON, INPUT_PULLUP);
  36. pinMode(WEAPON_TOGGLE, INPUT_PULLUP);
  37.  
  38. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  39. angleReadings[thisReading] = 0;
  40. }
  41. }
  42.  
  43. void loop() {
  44.  
  45. int triggerPushed = digitalRead(TRIGGER_BUTTON);
  46. int weaponToggle = digitalRead(WEAPON_TOGGLE);
  47.  
  48. total = total - angleReadings[readIndex];
  49. angleReadings[readIndex] = angle;
  50. total = total + angleReadings[readIndex];
  51. readIndex = readIndex + 1;
  52. if (readIndex >= numReadings) {
  53. readIndex = 0;
  54. }
  55.  
  56.  
  57. if (triggerPushed == HIGH) {
  58. //digitalWrite(13, LOW);
  59. if (Mouse.isPressed()) {
  60. Mouse.release();
  61. }
  62. } else {
  63. // Serial.println("button pushed");
  64. Mouse.press();
  65. }
  66.  
  67. if (weaponToggle == HIGH) {
  68. weaponModeOn = false;
  69. } else {
  70. if (!weaponModeOn) {
  71. Keyboard.write('2');
  72. delay(400);
  73. }
  74. weaponModeOn = true;
  75. }
  76.  
  77. //accx, accy, accz;
  78. mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  79. mpu.getAcceleration(&accx, &accy, &accz);
  80.  
  81. //Serial.println(gy);
  82.  
  83. // Serial.println(angle);
  84.  
  85. oldZ = newZ;
  86. vx = (gx + 300) / 150; // "+300" because the x axis of gyroscope give values about -350 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
  87. vy = -(gz - 100) / 150; // same here about "-100"
  88.  
  89. /*
  90. Serial.print("gx = ");
  91. Serial.print(gx);
  92. Serial.print(" | gz = ");
  93. Serial.print(gz);
  94. Serial.print(" | gy = ");
  95. Serial.println(gy);
  96.  
  97. */
  98.  
  99. if (gx > 32000) {
  100. Serial.println("Flick Right");
  101. for (int i; i < 12; i++) {
  102. Mouse.move(50, 0);
  103. }
  104. delay(250);
  105. } else if (gx < -32000) {
  106. Serial.println("Flick Left");
  107. for (int i; i < 12; i++) {
  108. Mouse.move(-50, 0);
  109. }
  110. delay(250);
  111. }
  112.  
  113. /*
  114. Serial.print("accx = ");
  115. Serial.print(accx);
  116. Serial.print(" | accy = ");
  117. Serial.print(accy);
  118. Serial.print(" | accz = ");
  119. Serial.println(accz);
  120. */
  121.  
  122.  
  123. averageAngle = total / numReadings;
  124. if (!weaponModeOn && accy > 16000)
  125. {
  126.  
  127.  
  128. if (averageAngle < 10) {
  129. Serial.println("Build floor");
  130. Keyboard.write(KEY_F2);
  131. } else if (averageAngle > 10 & averageAngle < 45) {
  132. Serial.println("Build ramp");
  133. Keyboard.write(KEY_F3);
  134. } else {
  135. Serial.println("Build wall");
  136. Keyboard.write(KEY_F1);
  137. }
  138.  
  139. // Serial.println(averageAngle);
  140.  
  141. //Serial.println("shake");
  142. //Keyboard.write(KEY_F1);
  143. delay(250);
  144. Mouse.click();
  145. } else if (weaponModeOn && accy > 16000)
  146. {
  147. if (averageAngle < 10) {
  148. Keyboard.write('r');
  149. }
  150. }
  151.  
  152. if (weaponModeOn) {
  153. //Serial.println("Weapon mode on");
  154. Mouse.move(vx, vy);
  155. }
  156.  
  157. //working
  158. //angle = atan2((float) (ay - 16384), (float) (ax - 16384)) * (180.0 / PI) * -1;
  159. angle = atan2((float) ay, (float) ~ax) * (180.0 / PI);
  160. //float angle = atan2((float) ay, (float) -ax) * (180.0 / PI);
  161.  
  162. //Serial.println(averageAngle);
  163.  
  164. delay(20);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement