Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1.  
  2. int PIN = 9;          // Digital IO pin connected to base of transistor
  3. int Length = 537;     // Length in Microseconds
  4. int IncomingByte = 0; // Initialize Serial Buffer
  5. int Reps = 3;         // Number of times to repeat each transmission
  6.  
  7. // Коды имеющихся на руле кнопок
  8. const int VOL_UP=1;
  9. const int VOL_DN=2;
  10. const int PREV_TR=3;
  11. const int NEXT_TR=4;
  12. const int MODE=5;
  13.  
  14. int wheelPin=A2; // аналоговый пин, на котором мы считываем сопротивление нажатой на руле кнопки
  15. int wheelMod=12; // цифровой пин, на котором считываем состояние кнопки Mode
  16.  
  17. int i=0;
  18. int prevButton=0;
  19.  
  20. unsigned long  pressedTime; // Время нажатия кнопки
  21.  
  22. void setup() {
  23.   pinMode(wheelPin, INPUT);  //set pin A2 as INPUT
  24.   pinMode(wheelMod, INPUT);  //set pin 12 as INPUT
  25.   pinMode(PIN, OUTPUT); // Set pin to output
  26.   digitalWrite(PIN, LOW); // Make PIN low to shut off transistor
  27.  
  28.   //Serial.begin(9600);
  29. }
  30.  
  31. int getR() { // Эта функция читает сопротивление с кнопок на руле и возвращает код нажатой кнопки, либо 0
  32.  
  33.   // читаем сопротивление (на самом деле напряжение, конечно) на аналоговом пине
  34.   int r = analogRead(wheelPin);
  35.  
  36.   //Serial.println(r);
  37.  
  38.   // Ищем, какая кнопка соответствует этому сопротивлению.
  39.   // Данные значения сопротивлений подходят для Toyota, для других автомобилей числа будут другие.
  40.   if (r>=235 && r<=245) return(VOL_UP); // 240
  41.   if (r>=496 && r<=506) return(VOL_DN); // 501
  42.   if (r>=88 && r<=98) return(PREV_TR); // 93
  43.   if (r>=0 && r<=5) return(NEXT_TR); // 0
  44.  
  45.   // если ни одна из кнопок на пине wheelPin не нажата, проверяем нажата ли кнопка Mode
  46.      r = digitalRead(wheelMod);
  47.      if (r == LOW) return(MODE);
  48.      
  49.   // если ни одна из кнопок не нажата, возвращаем 0
  50.   return (0);
  51. }
  52.  
  53.  
  54. void loop() {
  55.  int currButton=getR(); // заносим в переменную currButton код нажатой кнопки
  56.   if (currButton!=prevButton) { // если значение поменялось с прошлого раза
  57.  
  58.     delay(10);
  59.     currButton=getR(); // ждем 10ms и читаем еще раз, чтобы исключить "дребезг" кнопки
  60.  
  61.     if (currButton!=prevButton) { // если код кнопки точно поменялся с прошлого раза
  62.       //Serial.println(currButton);
  63.  
  64.       prevButton=currButton;     // сохраняем новое значение в переменную prevButton
  65.    
  66.       switch(currButton) {
  67.        case VOL_UP: JVCVolUp(); break;  
  68.        case VOL_DN: JVCVolDn(); break;  
  69.        case PREV_TR: JVCSkipBack(); break;
  70.        case NEXT_TR: JVCSkipFwd(); break;  
  71.        case MODE: JVCSource(); break;
  72.  
  73.        default: break;
  74.      }
  75.    }
  76.   }
  77.   delay(5);
  78.  
  79. }
  80.  
  81. void JVCVolUp() {      // Send 0x04
  82.   for (int i = 1; i <= Reps; i++); {
  83.     Preamble();
  84.    
  85.     bZERO();
  86.     bZERO();
  87.     bONE();    // 4
  88.     bZERO();
  89.    
  90.     bZERO();
  91.     bZERO();   // 0
  92.     bZERO();
  93.  
  94.     Postamble();
  95.   }
  96. }
  97.  
  98. void JVCVolDn() {      // Send 0x05
  99.   for (int i = 1; i <= Reps; i++); {
  100.     Preamble();
  101.    
  102.     bONE();
  103.     bZERO();
  104.     bONE();    // 5
  105.     bZERO();
  106.    
  107.     bZERO();
  108.     bZERO();   // 0
  109.     bZERO();
  110.  
  111.     Postamble();
  112.   }
  113. }
  114.  
  115. void JVCSource() {      // Send 0x08
  116.   for (int i = 1; i <= Reps; i++); {
  117.     Preamble();
  118.    
  119.     bZERO();
  120.     bZERO();
  121.     bZERO();    // 8
  122.     bONE();
  123.    
  124.     bZERO();
  125.     bZERO();   // 0
  126.     bZERO();
  127.  
  128.     Postamble();
  129.   }
  130. }
  131.  
  132. void JVCSkipFwd() {      // Send 0x12
  133.   for (int i = 1; i <= Reps; i++); {
  134.     Preamble();
  135.    
  136.     bZERO();
  137.     bONE();
  138.     bZERO();    // 2
  139.     bZERO();
  140.    
  141.     bONE();
  142.     bZERO();   // 1
  143.     bZERO();
  144.  
  145.     Postamble();
  146.   }
  147. }
  148.  
  149. void JVCSkipBack() {      // Send 0x13
  150.   for (int i = 1; i <= Reps; i++); {
  151.     Preamble();
  152.    
  153.     bONE();
  154.     bONE();
  155.     bZERO();    // 3
  156.     bZERO();
  157.    
  158.     bONE();
  159.     bZERO();   // 1
  160.     bZERO();
  161.  
  162.     Postamble();
  163.   }
  164. }
  165.  
  166. void bONE() {                     // Send a binary ONE over the line
  167.   digitalWrite(PIN, HIGH);        // Pull 3.5mm TIP low
  168.   delayMicroseconds(Length);      // for 537us
  169.   digitalWrite(PIN, LOW);         // Allow 3.5mm TIP to go high
  170.   delayMicroseconds(Length * 3);  // for 537 * 3 = 1611us
  171. }
  172.  
  173.  
  174. void bZERO() {                    // Send a binary ZERO over the line
  175.   digitalWrite(PIN, HIGH);        // Pull 3.5mm TIP low
  176.   delayMicroseconds(Length);      // for 537us
  177.   digitalWrite(PIN, LOW);         // Allow 3.5mm TIP to go high
  178.   delayMicroseconds(Length);      // for 537us
  179. }
  180.  
  181. void Preamble() {
  182.   digitalWrite(PIN, LOW);         // Not sure what this does
  183.   delayMicroseconds(Length * 1);
  184.  
  185.   digitalWrite(PIN, HIGH);        // AGC
  186.   delayMicroseconds(Length * 16);
  187.  
  188.   digitalWrite(PIN, LOW);         // AGC
  189.   delayMicroseconds(Length * 8);
  190.  
  191.   bONE();    // 1 Start Bit
  192.  
  193.   bONE();    //       (7 bit device code)
  194.   bONE();    
  195.   bONE();    // 7
  196.   bZERO();
  197.  
  198.   bZERO();
  199.   bZERO();    //4
  200.   bONE();
  201. }
  202.    
  203.    
  204. void Postamble() {
  205.   bONE();
  206.   bONE();    // 2 stop bits
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement