Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HCS301 decoder //
- const int buttonPin = 2; // número do pino pushbutton
- const int ledPin = 3; // número do pino LED
- #include <IRremote.h>
- int RECV_PIN = 4; // Diz que o receptor IR esta conectado no pino 4
- int estado = 0; // variável para leitura do pushbutton
- int guarda_estado = LOW; // variável para armazenar valores do pushbutton
- IRrecv irrecv(RECV_PIN); // cria a biblioteca IR
- decode_results results; // dados IR vão ser armazenados em results
- #define HCS_RECIEVER_PIN 2 //Pino do receptor RF 433Mhz
- #define RELAY_PIN 3 //Pino saída para relé
- class HCS301 {
- public:
- unsigned BatteryLow : 1;
- unsigned Repeat : 1;
- unsigned Btn1 : 1;
- unsigned Btn2 : 1;
- unsigned Btn3 : 1;
- unsigned Btn4 : 1;
- unsigned long SerialNum;
- unsigned long Encript;
- void print();
- };
- volatile boolean HCS_Listening = true;
- byte HCS_preamble_count = 0;
- uint32_t HCS_last_change = 0;
- uint32_t HCS_start_preamble = 0;
- uint8_t HCS_bit_counter;
- uint8_t HCS_bit_array[66];
- uint32_t HCS_Te = 400; //Typical Te duration
- uint32_t HCS_Te2_3 = 600; //HCS_TE * 3 / 2
- boolean relayOn = false;
- unsigned long millisStart = 0;
- unsigned long relayDuration = 100000; //Duracão do relé ligado
- //unsigned long relayDuration = 5*60*1000; //Duration of relay on
- HCS301 hcs301;
- //////////////////////////////////////////////////////////////
- void setup()
- {
- Serial.begin(115200);
- pinMode(HCS_RECIEVER_PIN, INPUT);
- pinMode(buttonPin, INPUT);
- pinMode(RELAY_PIN, OUTPUT);
- pinMode(ledPin, OUTPUT);
- attachInterrupt(0, HCS_interrupt, CHANGE);
- Serial.println("Setup OK");
- }
- /////////////////////////////////////////////////////////////
- void loop()
- { //Abre loop
- estado = digitalRead(RELAY_PIN); // le o estado pushbutton: ligado (HIGH) ou desligado (LOW)
- long CurTime = millis();
- if(HCS_Listening == false) {
- //get message
- HCS301 msg;
- memcpy(&msg,&hcs301,sizeof(HCS301));
- //do something
- msg.print();
- if(HCS_RECIEVER_PIN == 268435455) { //if remote serial matches
- millisStart = CurTime;
- relayOn = true;
- }
- msg.print();
- if(msg.SerialNum == 268435455 && guarda_estado == HIGH) { //if remote serial matches
- millisStart = CurTime;
- relayOn = false;
- }
- // verifica se o botão (pushbutton) está pressionado
- if (estado == HIGH) {
- // inverte valor da variável variable_buttonEstado
- guarda_estado = !guarda_estado;
- }
- //listen for another command
- HCS_Listening = true;
- }
- if((millis() - millisStart < relayDuration) && relayOn) {
- digitalWrite(RELAY_PIN, HIGH);
- }
- else {
- digitalWrite(RELAY_PIN, LOW);
- relayOn = false;
- }
- if (results.value == 33444015){ digitalWrite(3,HIGH);}// IR ativa o led na porta 3
- if (results.value == 33486855){// IR desliga os 3 leds.
- digitalWrite(3,LOW);
- }
- if (irrecv.decode(&results)) {
- Serial.println(results.value, DEC);// Os valores encontrados em cada botão do controle vao ser exibidos no "Serial Monitor"
- irrecv.resume();
- }
- } //Fecha loop
- //print data from HCS301
- void HCS301::print(){
- String btn;
- if (Btn1 == 1) btn += "B1 ";
- if (Btn2 == 1) btn += "B2 ";
- if (Btn3 == 1) btn += "B3 ";
- if (Btn4 == 1) btn += "B4 ";
- String it2;
- it2 += "Encript=";
- it2 += Encript;
- it2 += " Serial=";
- it2 += SerialNum;
- it2 += " Button=";
- it2 += btn;
- it2 += " BatteryLow=";
- it2 += BatteryLow;
- it2 += " Rep=";
- it2 += Repeat;
- Serial.println(it2);
- }
- //new data
- void HCS_interrupt() {
- if(HCS_Listening == false) {
- return;
- }
- uint32_t cur_timestamp = micros();
- uint8_t cur_status = digitalRead(HCS_RECIEVER_PIN);
- uint32_t pulse_duration = cur_timestamp - HCS_last_change;
- HCS_last_change = cur_timestamp;
- // gets preamble
- if(HCS_preamble_count < 12) {
- if(cur_status == HIGH){
- if( ((pulse_duration > 150) && (pulse_duration < 500)) || HCS_preamble_count == 0) {
- if(HCS_preamble_count == 0){
- HCS_start_preamble = cur_timestamp;
- }
- } else {
- HCS_preamble_count = 0;
- goto exit;
- }
- } else {
- if((pulse_duration > 300) && (pulse_duration < 900)) {
- HCS_preamble_count ++;
- if(HCS_preamble_count == 12) {
- HCS_Te = (cur_timestamp - HCS_start_preamble) / 23;
- HCS_Te2_3 = HCS_Te * 3 / 2;
- HCS_bit_counter = 0;
- goto exit;
- }
- } else {
- HCS_preamble_count = 0;
- goto exit;
- }
- }
- }
- // gets data
- if(HCS_preamble_count == 12) { //12
- if(cur_status == HIGH){
- if(((pulse_duration > 250) && (pulse_duration < 900)) || HCS_bit_counter == 0){
- // beginning of data pulse
- } else {
- // incorrect pause between pulses
- HCS_preamble_count = 0;
- goto exit;
- }
- } else {
- // end of data pulse
- if((pulse_duration > 250) && (pulse_duration < 900)) {
- HCS_bit_array[65 - HCS_bit_counter] = (pulse_duration > HCS_Te2_3) ? 0 : 1;
- HCS_bit_counter++;
- if(HCS_bit_counter == 66){
- // all bits captured
- HCS_Listening = false;
- HCS_preamble_count = 0;
- hcs301.Repeat = HCS_bit_array[0];
- hcs301.BatteryLow = HCS_bit_array[1];
- hcs301.Btn1 = HCS_bit_array[2];
- hcs301.Btn2 = HCS_bit_array[3];
- hcs301.Btn3 = HCS_bit_array[4];
- hcs301.Btn4 = HCS_bit_array[5];
- hcs301.SerialNum = 0;
- for(int i = 6; i < 34;i++){
- hcs301.SerialNum = (hcs301.SerialNum << 1) + HCS_bit_array[i];
- };
- uint32_t Encript = 0;
- for(int i = 34; i < 66;i++){
- Encript = (Encript << 1) + HCS_bit_array[i];
- };
- hcs301.Encript = Encript;
- }
- } else {
- HCS_preamble_count = 0;
- goto exit;
- }
- }
- }
- exit:;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement