Advertisement
Guest User

Helder board + battery - GPIO

a guest
Jul 8th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. /* pinouts:
  2. * A0: analog y axis
  3. * A1: analog x axis
  4. * A2: ADC5
  5. * A3: ADC4
  6. * A4: ADC1
  7. * A5: ADC0
  8. * 9: R1
  9. * 10: R2
  10. * 11: L2
  11. */
  12.  
  13. #include "HID-Project.h"
  14.  
  15. #define NUM_BUTTONS 14
  16.  
  17. #define BTN_UP 0
  18. #define BTN_DOWN 1
  19. #define BTN_LEFT 2
  20. #define BTN_RIGHT 3
  21. #define BTN_A 4
  22. #define BTN_START 5
  23. #define BTN_Y 6
  24. #define BTN_L1 7
  25. #define BTN_X 8
  26. #define BTN_R1 A0
  27. //#define BTN_R1 9
  28. //#define BTN_R2 10
  29. //#define BTN_L2 11
  30. #define BTN_B 12
  31. #define BTN_SELECT 13
  32.  
  33. const int battPin = A2;
  34. const int chargingPin = A3;
  35. const int chargedPin = A4;
  36. const int shutdownPin = A5;
  37. const int redPin = 9;
  38. const int greenPin = 10;
  39. const int bluePin = 11;
  40. const float lowBattVoltage = 0; //FIX
  41. const float shutdownVoltage = 0; //FIX
  42. float voltage;
  43. int charging;
  44. int charged;
  45.  
  46. void setup() {
  47. for (int i = 0; i < NUM_BUTTONS; i++) {
  48. pinMode(i, INPUT_PULLUP);
  49. }
  50.  
  51. pinMode(battPin, INPUT);
  52. pinMode(chargingPin, INPUT);
  53. pinMode(chargedPin, INPUT);
  54. pinMode(shutdownPin, OUTPUT);
  55. pinMode(redPin, OUTPUT);
  56. pinMode(greenPin, OUTPUT);
  57. pinMode(bluePin, OUTPUT);
  58. Serial.begin(9600);
  59.  
  60. // Sends a clean report to the host. This is important on any Arduino type.
  61. Gamepad.begin();
  62. }
  63.  
  64. void loop() {
  65.  
  66. //Battery monitor
  67. voltage = analogRead(battPin)// * (5.00 / 1023.00);
  68. Serial.println(voltage);
  69. charging = digitalRead(chargingPin);
  70. charged = digitalRead(chargedPin);
  71.  
  72. if (voltage <= shutdownVoltage) {
  73. digitalWrite(shutdownPin, HIGH);
  74. } else {
  75. digitalWrite(shutdownPin, LOW);
  76. }
  77.  
  78. if (charged == HIGH) {
  79. Serial.println("charged");
  80. analogWrite(redPin, 255);
  81. analogWrite(greenPin, 255);
  82. analogWrite(bluePin, 245);
  83. } else if (charging == LOW) {
  84. Serial.println("charging");
  85. analogWrite(redPin, 220);
  86. analogWrite(greenPin, 218);
  87. analogWrite(bluePin, 248);
  88. } else if (voltage <= lowBattVoltage) {
  89. Serial.println("lowbatt");
  90. analogWrite(redPin, 245);
  91. analogWrite(greenPin, 255);
  92. analogWrite(bluePin, 255);
  93. } else {
  94. Serial.println("power");
  95. analogWrite(redPin, 255);
  96. analogWrite(greenPin, 245);
  97. analogWrite(bluePin, 255);
  98. }
  99.  
  100. //Gamepad
  101. bool down = !digitalRead(BTN_DOWN);
  102. bool up = !digitalRead(BTN_UP);
  103. bool left = !digitalRead(BTN_LEFT);
  104. bool right = !digitalRead(BTN_RIGHT);
  105.  
  106. Gamepad.dPad1(GAMEPAD_DPAD_CENTERED);
  107.  
  108. if (down) {
  109. Gamepad.dPad1(GAMEPAD_DPAD_DOWN);
  110. if (left) {
  111. Gamepad.dPad1(GAMEPAD_DPAD_DOWN_LEFT);
  112. } if (right) {
  113. Gamepad.dPad1(GAMEPAD_DPAD_DOWN_RIGHT);
  114. }
  115. } else if (up) {
  116. Gamepad.dPad1(GAMEPAD_DPAD_UP);
  117. if (left) {
  118. Gamepad.dPad1(GAMEPAD_DPAD_UP_LEFT);
  119. } if (right) {
  120. Gamepad.dPad1(GAMEPAD_DPAD_UP_RIGHT);
  121. }
  122. } else if (left) {
  123. Gamepad.dPad1(GAMEPAD_DPAD_LEFT);
  124. } else if (right) {
  125. Gamepad.dPad1(GAMEPAD_DPAD_RIGHT);
  126. }
  127.  
  128. for (int i = BTN_A; i < NUM_BUTTONS; i++) {
  129. if (!digitalRead(i)) {
  130. Gamepad.press(i);
  131. } else {
  132. Gamepad.release(i);
  133. }
  134. }
  135.  
  136. Gamepad.write();
  137. delay(10);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement