- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
/* | |
RF Remote Capture sketch | |
Written by ScottC 24 Jun 2014 | |
Arduino IDE version 1.0.5 | |
Website: http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html | |
Receiver: XY-MK-5V | |
Description: Use Arduino to Receive RF Remote signal | |
------------------------------------------------------------- */ | |
const int dataSize = 500; //Arduino memory is limited (max=1700) | |
byte storedData[dataSize]; //Create an array to store the data | |
#define ledPin 13 //Onboard LED = digital pin 13 | |
#define rfReceivePin A0 //RF Receiver data pin = Analog pin 0 | |
const unsigned int upperThreshold = 100; //upper threshold value | |
const unsigned int lowerThreshold = 80; //lower threshold value | |
int maxSignalLength = 255; //Set the maximum length of the signal | |
int dataCounter = 0; //Variable to measure the length of the signal | |
unsigned long startTime=0; //Variable to record the start time | |
unsigned long endTime=0; //Variable to record the end time | |
unsigned long signalDuration=0; //Variable to record signal reading time | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
/* The following code will only run ONCE -------------- | |
---Press the reset button on the Arduino to run again-- */ | |
while(analogRead(rfReceivePin)<1){ | |
//Wait here until a LOW signal is received | |
startTime=micros(); //Update start time with every cycle. | |
} | |
digitalWrite(ledPin, HIGH); //Turn LED ON | |
//Read and store the rest of the signal into the storedData array | |
for(int i=0; i<dataSize; i=i+2){ | |
//Identify the length of the LOW signal---------------LOW | |
dataCounter=0; //reset the counter | |
while(analogRead(rfReceivePin)>upperThreshold && dataCounter<maxSignalLength){ | |
dataCounter++; | |
} | |
storedData[i]=dataCounter; | |
//Identify the length of the HIGH signal---------------HIGH | |
dataCounter=0;//reset the counter | |
while(analogRead(rfReceivePin)<lowerThreshold && dataCounter<maxSignalLength){ | |
dataCounter++; | |
} | |
storedData[i+1]=dataCounter; | |
//Any readings between the two threshold values will be ignored. | |
//The LOW or HIGH signal length must be less than the variable "maxSignalLength" | |
//otherwise it will be truncated. All of the HIGH signals and LOW signals combined | |
//must not exceed the variable "dataSize", otherwise it will be truncated. | |
//The maximum number of signals is 1700 - if you try to extend this variable to a higher | |
//number than 1700 - then the Arduino will freeze up and sketch will not work. | |
//------------------------------------------------------------- | |
} | |
endTime=micros(); //Record the end time of the read period. | |
signalDuration = endTime-startTime; | |
digitalWrite(ledPin, LOW);//Turn LED OFF | |
//Send report to the Serial Monitor | |
Serial.println("====================="); | |
Serial.print("Read duration: "); | |
Serial.print(signalDuration); | |
Serial.println(" microseconds"); | |
Serial.println("====================="); | |
Serial.println("LOW,HIGH"); | |
delay(20); | |
for(int i=0; i<dataSize; i=i+2){ | |
Serial.print(storedData[i]); | |
Serial.print(","); | |
Serial.println(storedData[i+1]); | |
delay(20); | |
} | |
} | |
void loop(){ | |
//Do nothing here | |
} |
The following code was produced from pushing the button responsible for turning the light off: | |
The code sequence above may seem a bit random until you start graphing it. I grabbed the LOW column - and produced the following chart: | |
The chart above is a bit messy - mainly because the timing is slightly out... in that sometimes it can squeeze an extra read from a particular signal. But what is important to note here is that you can differentiate a LONG signal from a SHORT signal. I have drawn a couple of red dotted lines where I believe most of the readings tend to sit. I then used a formula in the spreadsheet to calibrate the readings and make them a bit more uniform. For example, if the length of the signal was greater than 4 analogReads, then I converted this to 6. If it was less than 4 analogReads, then I converted it to 2. I used a frequency table to help decide on the cutoff value of 4, and just decided to pick the two values (2 for short, and 6 for long) based on the frequency tables below. I could have chosen 5 as the LONG value, but there were more 6's overall. **The meaning of "frequency" in the following tables relate to the "number of times" a specific signal length is recorded. | |
And this is the resulting chart: | |
You will notice that the pattern is quite repetitive. I helped to identify the sections with vertical red lines (near the bottom of the chart). In other words, the signal produced by the remote is repeated 6 times. I then did the same for the HIGH signal column and combined the two to create the following chart: | |
You will notice that the HIGH signals also have a repetitive pattern, however have a Very long length at the end of each section. This is almost a break to separate each section. This is what a single section looks like zoomed in: | |
SL = [Short LOW] signal. - or short blue bar SH = [Short HIGH] signal - or short yellow bar LL = [Long LOW] signal - or long blue bar LH = [Long HIGH] signal - or long yellow bar VLH = [Very long HIGH} signal - or very long yellow bar (~92 analogReads in length) You will notice that there are only about 6 different combinations of the signals mentioned above. We can use this to create a coding system as described below: | |
We can use this coding system to describe the signals. The charts below show the difference between turning the LIGHT ON and LIGHT OFF. | |
PLEASE NOTE: You may notice when you copy the signals from the Serial monitor that you get a series of (0,255) combinations. This is actually a timeout sequence - which generally occurs after the signal is complete. Here is an example of what I mean. | |
Comments
Post a Comment