Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. //
  2. String purple = "00111100111100001111000011000011000011000011001111000011000011";
  3.  
  4.  
  5.  
  6. //
  7. volatile bool received = false;
  8. volatile unsigned long timings[25];
  9. volatile static int i = 0;
  10. void handler()
  11. {
  12.  
  13. static unsigned long duration = 0;
  14. static unsigned long lastTime = 0;
  15. // static int i = 0;
  16. static bool sync1 = false; // wykrycie synchronizacji
  17.  
  18.  
  19. if (received == true)
  20. {
  21. return;
  22. }
  23.  
  24. long time = micros();
  25. duration = time - lastTime;
  26. lastTime = time;
  27.  
  28.  
  29.  
  30.  
  31. if ( duration > 1800 && duration < 2000)
  32. {
  33. sync1 = true;
  34. }
  35.  
  36. if (sync1 == true)
  37. {
  38. i = (i + 1);
  39. timings[i] = duration;
  40. if ( duration > 2000)
  41. {
  42. received = true;
  43. sync1 = false;
  44. }
  45. }
  46.  
  47. }
  48.  
  49. void setup()
  50. {
  51. Serial.begin(9600);
  52. Serial.println("Start");
  53. pinMode(2, INPUT_PULLUP);
  54. attachInterrupt(digitalPinToInterrupt(2), handler, CHANGE);
  55. }
  56.  
  57.  
  58. void loop()
  59.  
  60. {
  61. String bitcode;
  62. String code;
  63. if (received == true)
  64. {
  65. detachInterrupt(digitalPinToInterrupt(2));
  66. for(unsigned int a=2; a < i ; a++ )
  67. {
  68. if ( a%2 == 0)
  69. {
  70. if( timings[a] > 180 && timings[a] < 320)
  71. {
  72. bitcode = "00" ;
  73. code = code + String(bitcode);
  74. }
  75. else if(timings[a] > 400 && timings[a] < 700)
  76. {
  77. bitcode = "0000" ;
  78. code = code + String(bitcode);
  79. }
  80. }
  81. else if( a%2 !=0)
  82. {
  83. if( timings[a] > 180 && timings[a] < 320)
  84. {
  85. bitcode = "11" ;
  86. code = code + String(bitcode);
  87. }
  88. else if(timings[a] > 400 && timings[a] < 700)
  89. {
  90. bitcode = "1111" ;
  91. code = code + String(bitcode);
  92. }
  93. }
  94. }
  95. Serial.println(code);
  96. if(code == purple)
  97. {
  98. Serial.println("receive ok");
  99. }
  100. delay(3000);
  101. received = false;
  102. attachInterrupt(digitalPinToInterrupt(2), handler, CHANGE);
  103. i=0;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement