Skip to content

Instantly share code, notes, and snippets.

@acturcato
Created January 21, 2014 12:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acturcato/8538821 to your computer and use it in GitHub Desktop.
Save acturcato/8538821 to your computer and use it in GitHub Desktop.
Since the data previously encrypted using the encoder HT6P20B. The main idea of this sketch is to decode the values ​​received via an RF receiver module connected to Arduino. For more details, access: http://acturcato.wordpress.com/2014/01/04/decoder-for-ht6p20b-encoder-on-arduino-board-english/
/*
TITLE: DECODER FOR HT6P20B ENCODER
CREATED BY: AFONSO CELSO TURCATO
DATE: 27/12/2013
E-MAIL: acturcato (at) gmail.com
LICENSE: GPL
REV.: 01
DESCRIPTION:
http://acturcato.wordpress.com/2014/01/04/decoder-for-ht6p20b-encoder-on-arduino-board-english/
DESCRIÇÃO:
http://acturcato.wordpress.com/2013/12/20/decodificador-para-o-encoder-ht6p20b-em-arduino/
*/
byte pinRF; // Pin where RF Module is connected
boolean startbit;
boolean dataok;
boolean anticodeok;
int counter; //received bits counter: 22 of Address + 2 of Data + 4 of EndCode (Anti-Code)
int lambda; // on pulse clock width (if fosc = 2KHz than lambda = 500 us)
int dur0, dur1; // pulses durations (auxiliary)
unsigned long buffer=0; //buffer for received data storage
void setup(){
pinRF = 52; //If necessary, change this for you project
pinMode(pinRF, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(13, digitalRead(pinRF)); //blink de onboard LED when receive something
if (!startbit)
{// Check the PILOT CODE until START BIT;
dur0 = pulseIn(pinRF, LOW); //Check how long DOUT was "0" (ZERO) (refers to PILOT CODE)
//If time at "0" is between 9200 us (23 cycles of 400us) and 13800 us (23 cycles of 600 us).
if((dur0 > 9200) && (dur0 < 13800) && !startbit)
{
lambda = dur0 / 23; //calculate wave length - lambda
dur0 = 0;
buffer = 0;
counter = 0;
dataok = false;
startbit = true;
}
}
// If Start Bit is OK, then starts measure os how long the signal is level "1" and check is value is into acceptable range.
if (startbit && !dataok && counter < 28)
{
++counter;
dur1 = pulseIn(pinRF, HIGH);
if((dur1 > 0.5 * lambda) && (dur1 < (1.5 * lambda))) //If pulse width at "1" is between "0.5 and 1.5 lambda", means that pulse is only one lambda, so the data é "1".
{
buffer = (buffer << 1) + 1; // add "1" on data buffer
}
else if((dur1 > 1.5 * lambda) && (dur1 < (2.5 * lambda))) //If pulse width at "1" is between "1.5 and 2.5 lambda", means that pulse is two lambdas, so the data é "0".
{
buffer = (buffer << 1); // add "0" on data buffer
}
else
{
//Reset the loop
startbit = false;
}
}
//Check if all 28 bits were received (22 of Address + 2 of Data + 4 of Anti-Code)
if (counter==28)
{
// Check if Anti-Code is OK (last 4 bits of buffer equal "0101")
if ((bitRead(buffer, 0) == 1) && (bitRead(buffer, 1) == 0) && (bitRead(buffer, 2) == 1) && (bitRead(buffer, 3) == 0))
{
anticodeok = true;
}
else
{
//Reset the loop
startbit = false;
}
if (anticodeok)
{
dataok = true;
counter = 0;
startbit = false;
anticodeok = false;
Serial.print("Data: ");
Serial.println(buffer, BIN);
unsigned long addr = buffer >> 6;
Serial.print("-Address: ");
Serial.println(addr, HEX);
Serial.println("-Button1: " + (String)bitRead(buffer, 4));
Serial.println("-Button2: " + (String)bitRead(buffer, 5));
Serial.println("-----------------------------------");
delay(100);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment