Advertisement
Guest User

IRremote

a guest
Sep 26th, 2015
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include "IRremote.h"
  2. IRrecv irrecv(11); // Указываем пин, к которому подключен приемник
  3. decode_results results;
  4.  
  5. String inputString = "";
  6. boolean stringComplete = false;
  7. boolean LampIsON = false;
  8.  
  9. int lastPoz = 0;
  10. int lastPozTimeOut = 0;
  11. int lastPozTimeOutSet = 200;
  12.  
  13. #define BTN_ONOFF 9435
  14. #define BTN_PHOTO 5355
  15. #define BTN_DISPLAY 30855
  16. #define BTN_SELF_TIMER -11221
  17. #define BTN_ZERO_MEMORY 14535
  18. #define BTN_DATE_TIME 1275
  19. #define BTN_PHOTO_SEARCH -13261
  20. #define BTN_DUB -21421
  21. #define BTN_SLOW -10201
  22. #define BTN_X2 11475
  23. #define BTN_W 17595
  24. #define BTN_T -31621
  25. #define BTN_BACK 6375
  26. #define BTN_NEXT 22695
  27. #define BTN_PLAY -26521
  28. #define BTN_PAUSE 26775
  29. #define BTN_STOP -22441
  30. #define BTN_MINUS -5101
  31. #define BTN_PLUS 27795
  32. #define BTN_FADV 23715
  33. //#define BTN_ 0x;
  34.  
  35. void setup()
  36. {
  37.   Serial.begin(9600); // Выставляем скорость COM порта
  38.   irrecv.enableIRIn(); // Запускаем прием
  39.   pinMode(2, OUTPUT);
  40.   // reserve 200 bytes for the inputString:
  41.   inputString.reserve(200);
  42. }
  43.  
  44. void loop() {
  45.   if (irrecv.decode(&results)) // Если данные пришли
  46.   {
  47.     int res = results.value;
  48.     //Serial.println(results.value, HEX);
  49.     //Serial.println(res);
  50.    
  51.     if(res == BTN_ONOFF || res == BTN_PHOTO || res == BTN_DISPLAY || res == BTN_SELF_TIMER || res == BTN_ZERO_MEMORY || res == BTN_DATE_TIME || res == BTN_PHOTO_SEARCH || res == BTN_DUB || res == BTN_SLOW || res == BTN_X2 || res == BTN_W || res == BTN_T || res == BTN_BACK || res == BTN_NEXT || res == BTN_PLAY || res == BTN_PAUSE || res == BTN_STOP || res == BTN_MINUS || res == BTN_PLUS || res == BTN_FADV) {
  52.       if(res != lastPoz) {
  53.         if(lastPoz != 0) {
  54.           ButtonUp(lastPoz);
  55.         }
  56.         lastPoz = res;
  57.         ButtonDown(res);
  58.       } else {
  59.         ButtonHold(res);
  60.       }
  61.     }
  62.    
  63.     lastPozTimeOut = 0;
  64.    
  65.     irrecv.resume(); // Принимаем следующую команду
  66.   }
  67.  
  68.   if(stringComplete) {
  69.     //Serial.println(inputString);
  70.     if(inputString == "on\n") {
  71.       digitalWrite(2, HIGH);
  72.       LampIsON = true;
  73.     }
  74.     if(inputString == "off\n") {
  75.       digitalWrite(2, LOW);
  76.       LampIsON = false;
  77.     }
  78.     if(inputString == "stat\n") {
  79.       if(LampIsON) {
  80.         Serial.println("1");
  81.       } else {
  82.         Serial.println("0");
  83.       }
  84.     }
  85.     // clear the string:
  86.     inputString = "";
  87.     stringComplete = false;
  88.   }
  89.  
  90.   lastPozTimeOut += 1;
  91.   if(lastPozTimeOut >= lastPozTimeOutSet) {
  92.     if(lastPoz != 0) {
  93.       ButtonUp(lastPoz);
  94.       lastPoz = 0;
  95.     }
  96.   }
  97.   delay(1);
  98. }
  99.  
  100. void ButtonDown(int BtnCode) {
  101.   //Serial.println("ButtonDown: ");
  102.   //Serial.println(BtnCode);
  103. }
  104.  
  105. void ButtonUp(int BtnCode) {
  106.   //Serial.println("ButtonUp: ");
  107.   //Serial.println(BtnCode);
  108.   if(BtnCode == BTN_ONOFF) {
  109.     if(!LampIsON) {
  110.       digitalWrite(2, HIGH);
  111.       LampIsON = true;
  112.     } else {
  113.       digitalWrite(2, LOW);
  114.       LampIsON = false;
  115.     }
  116.   }
  117. }
  118.  
  119. void ButtonHold(int BtnCode) {
  120.   //Serial.println("ButtonHold: ");
  121.   //Serial.println(BtnCode);
  122. }
  123.  
  124. void serialEvent() {
  125.   while(Serial.available()) {
  126.     // get the new byte:
  127.     char inChar = (char)Serial.read();
  128.     // add it to the inputString:
  129.     inputString += inChar;
  130.     // if the incoming character is a newline, set a flag
  131.     // so the main loop can do something about it:
  132.     if (inChar == '\n') {
  133.       stringComplete = true;
  134.     }
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement