Advertisement
Vagante

Untitled

Oct 27th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. // HCS301 decoder //
  2. const int buttonPin = 2; // número do pino pushbutton
  3. const int ledPin = 3; // número do pino LED
  4.  
  5. #include <IRremote.h>
  6. int RECV_PIN = 4; // Diz que o receptor IR esta conectado no pino 4
  7. int estado = 0; // variável para leitura do pushbutton
  8. int guarda_estado = LOW; // variável para armazenar valores do pushbutton
  9. IRrecv irrecv(RECV_PIN); // cria a biblioteca IR
  10. decode_results results; // dados IR vão ser armazenados em results
  11.  
  12.  
  13. #define HCS_RECIEVER_PIN 2 //Pino do receptor RF 433Mhz
  14. #define RELAY_PIN 3 //Pino saída para relé
  15.  
  16. class HCS301 {
  17. public:
  18. unsigned BatteryLow : 1;
  19. unsigned Repeat : 1;
  20. unsigned Btn1 : 1;
  21. unsigned Btn2 : 1;
  22. unsigned Btn3 : 1;
  23. unsigned Btn4 : 1;
  24. unsigned long SerialNum;
  25. unsigned long Encript;
  26. void print();
  27. };
  28.  
  29. volatile boolean HCS_Listening = true;
  30. byte HCS_preamble_count = 0;
  31. uint32_t HCS_last_change = 0;
  32. uint32_t HCS_start_preamble = 0;
  33. uint8_t HCS_bit_counter;
  34. uint8_t HCS_bit_array[66];
  35. uint32_t HCS_Te = 400; //Typical Te duration
  36. uint32_t HCS_Te2_3 = 600; //HCS_TE * 3 / 2
  37. boolean relayOn = false;
  38. unsigned long millisStart = 0;
  39. unsigned long relayDuration = 100000; //Duracão do relé ligado
  40. //unsigned long relayDuration = 5*60*1000; //Duration of relay on
  41.  
  42. HCS301 hcs301;
  43.  
  44.  
  45. //////////////////////////////////////////////////////////////
  46. void setup()
  47. {
  48. Serial.begin(115200);
  49. pinMode(HCS_RECIEVER_PIN, INPUT);
  50. pinMode(buttonPin, INPUT);
  51. pinMode(RELAY_PIN, OUTPUT);
  52. pinMode(ledPin, OUTPUT);
  53. attachInterrupt(0, HCS_interrupt, CHANGE);
  54. Serial.println("Setup OK");
  55. }
  56.  
  57. /////////////////////////////////////////////////////////////
  58.  
  59. void loop()
  60. { //Abre loop
  61. estado = digitalRead(RELAY_PIN); // le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
  62. long CurTime = millis();
  63.  
  64. if(HCS_Listening == false) {
  65. //get message
  66. HCS301 msg;
  67. memcpy(&msg,&hcs301,sizeof(HCS301));
  68.  
  69.  
  70. //do something
  71. msg.print();
  72. if(HCS_RECIEVER_PIN == 268435455) { //if remote serial matches
  73. millisStart = CurTime;
  74. relayOn = true;
  75.  
  76. }
  77. msg.print();
  78. if(msg.SerialNum == 268435455 && guarda_estado == HIGH) { //if remote serial matches
  79. millisStart = CurTime;
  80. relayOn = false;
  81.  
  82. }
  83.  
  84. // verifica se o botão (pushbutton) está pressionado
  85. if (estado == HIGH) {
  86. // inverte valor da variável variable_buttonEstado
  87. guarda_estado = !guarda_estado;
  88.  
  89. }
  90.  
  91.  
  92. //listen for another command
  93. HCS_Listening = true;
  94. }
  95.  
  96. if((millis() - millisStart < relayDuration) && relayOn) {
  97. digitalWrite(RELAY_PIN, HIGH);
  98. }
  99. else {
  100. digitalWrite(RELAY_PIN, LOW);
  101. relayOn = false;
  102. }
  103.  
  104. if (results.value == 33444015){ digitalWrite(3,HIGH);}// IR ativa o led na porta 3
  105. if (results.value == 33486855){// IR desliga os 3 leds.
  106. digitalWrite(3,LOW);
  107. }
  108.  
  109. if (irrecv.decode(&results)) {
  110.  
  111. Serial.println(results.value, DEC);// Os valores encontrados em cada botão do controle vao ser exibidos no "Serial Monitor"
  112. irrecv.resume();
  113. }
  114.  
  115. } //Fecha loop
  116.  
  117.  
  118.  
  119. //print data from HCS301
  120. void HCS301::print(){
  121.  
  122. String btn;
  123. if (Btn1 == 1) btn += "B1 ";
  124. if (Btn2 == 1) btn += "B2 ";
  125. if (Btn3 == 1) btn += "B3 ";
  126. if (Btn4 == 1) btn += "B4 ";
  127.  
  128. String it2;
  129. it2 += "Encript=";
  130. it2 += Encript;
  131. it2 += " Serial=";
  132. it2 += SerialNum;
  133. it2 += " Button=";
  134. it2 += btn;
  135. it2 += " BatteryLow=";
  136. it2 += BatteryLow;
  137. it2 += " Rep=";
  138. it2 += Repeat;
  139.  
  140. Serial.println(it2);
  141.  
  142. }
  143.  
  144. //new data
  145. void HCS_interrupt() {
  146.  
  147. if(HCS_Listening == false) {
  148. return;
  149. }
  150.  
  151. uint32_t cur_timestamp = micros();
  152. uint8_t cur_status = digitalRead(HCS_RECIEVER_PIN);
  153. uint32_t pulse_duration = cur_timestamp - HCS_last_change;
  154. HCS_last_change = cur_timestamp;
  155.  
  156.  
  157.  
  158.  
  159. // gets preamble
  160. if(HCS_preamble_count < 12) {
  161. if(cur_status == HIGH){
  162. if( ((pulse_duration > 150) && (pulse_duration < 500)) || HCS_preamble_count == 0) {
  163. if(HCS_preamble_count == 0){
  164. HCS_start_preamble = cur_timestamp;
  165. }
  166. } else {
  167. HCS_preamble_count = 0;
  168. goto exit;
  169. }
  170. } else {
  171. if((pulse_duration > 300) && (pulse_duration < 900)) {
  172. HCS_preamble_count ++;
  173. if(HCS_preamble_count == 12) {
  174. HCS_Te = (cur_timestamp - HCS_start_preamble) / 23;
  175. HCS_Te2_3 = HCS_Te * 3 / 2;
  176. HCS_bit_counter = 0;
  177. goto exit;
  178. }
  179. } else {
  180. HCS_preamble_count = 0;
  181. goto exit;
  182. }
  183. }
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190. // gets data
  191. if(HCS_preamble_count == 12) { //12
  192. if(cur_status == HIGH){
  193. if(((pulse_duration > 250) && (pulse_duration < 900)) || HCS_bit_counter == 0){
  194. // beginning of data pulse
  195. } else {
  196. // incorrect pause between pulses
  197. HCS_preamble_count = 0;
  198. goto exit;
  199. }
  200. } else {
  201. // end of data pulse
  202. if((pulse_duration > 250) && (pulse_duration < 900)) {
  203. HCS_bit_array[65 - HCS_bit_counter] = (pulse_duration > HCS_Te2_3) ? 0 : 1;
  204. HCS_bit_counter++;
  205. if(HCS_bit_counter == 66){
  206. // all bits captured
  207. HCS_Listening = false;
  208. HCS_preamble_count = 0;
  209.  
  210. hcs301.Repeat = HCS_bit_array[0];
  211. hcs301.BatteryLow = HCS_bit_array[1];
  212. hcs301.Btn1 = HCS_bit_array[2];
  213. hcs301.Btn2 = HCS_bit_array[3];
  214. hcs301.Btn3 = HCS_bit_array[4];
  215. hcs301.Btn4 = HCS_bit_array[5];
  216.  
  217. hcs301.SerialNum = 0;
  218. for(int i = 6; i < 34;i++){
  219. hcs301.SerialNum = (hcs301.SerialNum << 1) + HCS_bit_array[i];
  220. };
  221.  
  222. uint32_t Encript = 0;
  223. for(int i = 34; i < 66;i++){
  224. Encript = (Encript << 1) + HCS_bit_array[i];
  225. };
  226. hcs301.Encript = Encript;
  227. }
  228. } else {
  229. HCS_preamble_count = 0;
  230. goto exit;
  231. }
  232. }
  233. }
  234. exit:;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement