Advertisement
Guest User

rennan1221

a guest
Feb 16th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1.  
  2. #define SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
  3.  
  4. volatile byte impulse = 0; // kolejny puls
  5. volatile int bufor[53];
  6. volatile boolean header = false;
  7. volatile unsigned long StartPeriod = 0; // set in the interrupt
  8. volatile boolean stop_ints = false;
  9.  
  10.  
  11. void setup()
  12. {
  13. attachInterrupt(SIGNAL_IN, calcInput, CHANGE);
  14.  
  15. Serial.begin(9600);
  16. }
  17.  
  18. void loop()
  19. {
  20. if (stop_ints) //data in buffer
  21. {
  22. unsigned long binary = 1;
  23. //byte i = 0;
  24. for (byte j = 0; j < 46; j++)
  25. {
  26. //Serial.print(binary);
  27. if ((bufor[j] > 220) &&
  28. (bufor[j] < 400))
  29. {
  30. binary <<= 1;
  31. //binary |= 1;
  32. //i++;
  33. bitSet(binary,0);
  34. }
  35. else if ((bufor[j] > 90) &&
  36. (bufor[j] < 220) && (bufor[j + 1] > 90) &&
  37. (bufor[j + 1] < 220)) {
  38. binary <<= 1;
  39. j++;
  40.  
  41. }
  42. else if ((bufor[j] > 90) &&
  43. (bufor[j] < 220) && (bufor[j + 1] > 220) &&
  44. (bufor[j + 1] < 400)) {
  45. binary <<= 1;
  46. bitSet(binary,0);
  47. //i += 2;
  48. j++;
  49. }
  50. else break;
  51. }
  52. //Serial.println(bitRead(binary,4));
  53. if (bitRead(binary,23))
  54. {
  55. bitClear(binary,23);
  56. Serial.print("remoteID:");
  57. Serial.print((binary / 128) & 65535);
  58. Serial.print(" - ");
  59. Serial.print("key code:");
  60. Serial.println(binary & 127);
  61. }
  62. else {
  63. Serial.println("wrong code ");
  64. Serial.println(binary, BIN);
  65. }
  66. delay (1000);
  67. header = false;
  68. impulse = 0;
  69. stop_ints = false;
  70.  
  71. // }
  72. }
  73. }
  74.  
  75. // interrupt below...
  76.  
  77.  
  78. void calcInput()
  79. {
  80. // get the time using micros
  81. unsigned int duration = (int)(micros() - StartPeriod); // save pulse length to bufor
  82. StartPeriod = micros(); //begin next impulse
  83. //Serial.println(StartPeriod);
  84. if (stop_ints) return;
  85. if ((duration < 90) || (duration > 600)) goto reset; //impulse not right
  86. bufor[impulse++] = duration;
  87. if (duration < 415) return;
  88. if (!header)
  89. {
  90. header = true;
  91. impulse = 0;
  92. return;
  93. }
  94. else
  95. {
  96. if ((impulse < 23) || (impulse > 52)) goto reset; //too long or too short info
  97. stop_ints = true;
  98. return;
  99. }
  100. reset:
  101. header = false;
  102. impulse = 0;
  103. return;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement