Advertisement
Hisma

AA Steering Wheel Controls

Dec 23rd, 2018
1,614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. // By: Rutger de Graaf
  2. // 06-10-2018
  3. // modified 12/23/18 by Richard Meyer for mazda 6 AA SWC
  4.  
  5. #include "Keyboard.h"
  6.  
  7. // buttons[i][0] = minTrigger,
  8. // buttons[i][1] = maxTrigger,
  9. // buttons[i][2] = funcType(1=pinOut or 2=Usb),
  10. // buttons[i][3] = funcValue(pinout or ascii),
  11. const int buttons[][4] = {
  12.   {945, 965, 2, 201}, //vol up    F8
  13.   {985, 1020, 2, 200}, //vol down  F7
  14.   {800, 830, 2, 118}, //prev track     v
  15.   {885, 915, 2, 110}, //next track      n
  16. };
  17. const int analogInPin = A3;  // Analog input pin that the stepped resistor circuit is attached to
  18.  
  19. int x = 3;                //value read from the pot
  20. int i;                    //button loop-counter
  21. int c = 0;                //button streak-counter
  22. boolean found = false;    //global counter-stop
  23. int v = 4;                //verify necessary detection length in loops to press button
  24. int vr = 4;               //verify necessary detection length in loops to release button
  25. int d = 10;               //check-loop duration in ms
  26.  
  27. int d2 = 10;              //button-hold-loop duration in ms
  28. int pressed = false;      //loop break condition for holding the button
  29.  
  30. void setup() {
  31.   //initialize serial communications at 9600 bps
  32.   Serial.begin(9600);
  33.   //set pinout types
  34.   int j;
  35.   for(j = 0; j <= 5; j = j + 1) {
  36.     //if button type is pinOut
  37.     if(buttons[j][2] == 1) {
  38.       //enable that pin as output
  39.       pinMode(buttons[j][3], OUTPUT);
  40.     }
  41.   }
  42.   Keyboard.begin();
  43. }
  44.  
  45. void loop() {
  46.   //read the analog in value and print to serial
  47.   x = analogRead(analogInPin);
  48.   Serial.println(x);
  49.  
  50.   //loop through all possible buttons
  51.   for(i = 0; i <= 3; i = i + 1) {
  52.     //if this button is not detected: skip to next iteration
  53.     if(x <= buttons[i][0] || x >= buttons[i][1]) {
  54.       continue;
  55.     }
  56.     //button is detected
  57.     Serial.print("button detected for value ");
  58.     Serial.print(x);
  59.     c = c + 1;
  60.     Serial.print(", c = ");
  61.     Serial.println(c);
  62.     //if button is not detected enough times: break
  63.     if(c < v) {
  64.       break;
  65.     }
  66.     //send button press event
  67.     buttonPress(i);
  68.     c = 0;
  69.     break;
  70.   }
  71.  
  72.   //delay next read
  73.   delay(d);
  74. }
  75.  
  76. void buttonPress(int i){
  77.   Serial.print("going to send press for button int ");
  78.   Serial.println(i);
  79.   if(buttons[i][2] == 1) {
  80.     Serial.println("sending gpio");
  81.     buttonGpio(i);
  82.   }
  83.   if(buttons[i][2] == 2) {
  84.     Serial.println("sending usb");
  85.     buttonUsb(i);
  86.   }
  87. }
  88.  
  89. void buttonGpio(int i) {
  90.   int pinOut = buttons[i][3];
  91.   c = 0;
  92.   Serial.print("pressed gpio button ");
  93.   Serial.println(pinOut);
  94.   digitalWrite(pinOut, HIGH);
  95.   pressed = true;
  96.   while(pressed) {
  97.     x = analogRead(analogInPin);
  98.     if(x <= buttons[i][0] || x >= buttons[i][1]) {
  99.       Serial.print("Outvalue detected: ");
  100.       Serial.println(x);
  101.       c = c + 1;
  102.     } else {
  103.       c = 0;
  104.     }
  105.     if(c >= vr) {
  106.       pressed = false;
  107.     }
  108.     delay(d2);
  109.   }
  110.   digitalWrite(pinOut, LOW);
  111.   Serial.print("released gpio button ");
  112.   Serial.println(pinOut);
  113. }
  114.  
  115. void buttonUsb(int i) {
  116.   Keyboard.begin();
  117.   int ascii = buttons[i][3];
  118.   c = 0;
  119.   Serial.print("pressed usb button ");
  120.   Serial.println(ascii);
  121.   Keyboard.press(ascii);
  122.   pressed = true;
  123.   while(pressed) {
  124.     x = analogRead(analogInPin);
  125.     if(x <= buttons[i][0] || x >= buttons[i][1]) {
  126.       Serial.print("Outvalue detected: ");
  127.       Serial.println(x);
  128.       c = c + 1;
  129.     } else {
  130.       c = 0;
  131.     }
  132.     if(c >= vr) {
  133.       pressed = false;
  134.     }
  135.     delay(d2);
  136.   }
  137.   Keyboard.release(ascii);
  138.   Serial.print("released usb button ");
  139.   Serial.println(ascii);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement