Once upon a time, I built a few Arduino projects and felt like I was progressing well.
Recently, I decided to have a crack at it again - I am spinning the simplest of projects, simply trapping some 433MHz rf code, and hopefully eventually posting the output to a Webservice of some sorts(probably just an ethernet shield and basic http post - or maybe, if I haven't pulled all my hair out yet, a quick post to MQTT)
I have managed to decode the RF signal (it's literally just a bit stream with a few binary values-but after a minute or two, my serial connection just dies.
I assume I am leaking memory somewhere, but I simply don;t know how to clean up.
Here is my code so far (it's ugly, I know - feel free to criticise, as long as it is constructive)
It was much prettier before I butchered it a thousand times and tried to dumb it down - I started with this: http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-
const int dataSize = 256; //Arduino memory is limited (max=1700)
#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
void setup(){
}
void loop(){
Serial.begin(9600);
byte storedData[dataSize]; //Create an array to store the data
pinMode(ledPin, OUTPUT);
while(analogRead(rfReceivePin)<1){
//Wait here until a LOW signal is received
}
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+1){
//Identify the length of the LOW signal---------------LOW
dataCounter=0; //reset the counter
while(analogRead(rfReceivePin)>upperThreshold && dataCounter<maxSignalLength){
dataCounter++;
}
//Identify the length of the HIGH signal---------------HIGH
dataCounter=0;//reset the counter
while(analogRead(rfReceivePin)<lowerThreshold && dataCounter<maxSignalLength){
dataCounter++;
}
if(dataCounter < 100){
if(dataCounter > 8){
storedData[i]=1;
} else {
storedData[i]=0;
}
} else {
//Anything over 100 came from elsewhere, we'll tag this one 2...
storedData[i] = 2;
}
//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.
//-------------------------------------------------------------
}
//Send report to the Serial Monitor
Serial.println("========GO=============");
Serial.println("LOW,HIGH");
delay(20);
int start = 0;
for(int n=0; n<dataSize; n=n+1){
if(start <= 1 && storedData[n] == 1 && storedData[n+1] == 1 && storedData[n+2] == 0 && storedData[n+3] == 0 && storedData[n+4] == 0 && storedData[n+5] == 0 && storedData[n+6] == 0 && storedData[n+7] == 0){
Serial.print("Found start");
start = n;
Serial.println(start);
Serial.print ( storedData[n+17] + (storedData[n+16] * 2) + (storedData[n+15] * 4) + (storedData[n+14] * 8) + (storedData[n+13] * 16) + (storedData[n+12] * 32) + (storedData[n+11] * 64) + (storedData[n+10] * 128) + (storedData[n+9] * 256) +(storedData[n+8] * 512));
Serial.print("F / ");
Serial.print ( storedData[n+29] + (storedData[n+28] * 2) + (storedData[n+27] * 4) + (storedData[n+26] * 8) + (storedData[n+25] * 16) + (storedData[n+24] * 32) + (storedData[n+23] * 64) + (storedData[n+22] * 128) + (storedData[n+21] * 256) +(storedData[n+20] * 512));
Serial.print("F");
}
}
// Serial.println("====LOG======");
// if(start >= 1){
// for(int i=start; i<start+32; i=i+1){
// Serial.print(storedData[i]);
// delay(5);
// }
// }
Serial.println("====FIN======");
}
Output goes like this:
641F / 1959F====LOG======
15:00:57.987 -> 11000000101000000121222122011111====FIN======
15:01:00.892 -> ========GO=============
15:01:00.939 -> LOW,HIGH
15:01:00.939 -> Found start262
79F / 77F====LOG======
15:01:00.973 -> 11000000000100111100000100110111====FIN======
15:01:06.890 -> ========GO=============
15:01:06.924 -> LOW,HIGH
15:01:06.924 -> Found start3
15:01:06.924 -> 79F / 77F====LOG======
15:01:06.958 -> 11000000000100111100000100110111⸮⸮==FIN======
15:01:13.976 -> ========GO=============
15:01:14.010 -> LOW,HIGH
15:01:14.010 -> Found start294
732F / 4F====LOG======
15:01:14.044 -> 11000000101100222011000000010010====FIN======
15:01:24.954 -> ========GO=============
15:01:34.216 -> LOW,HIGH
15:01:34.216 -> ====LOG======
15:01:34.250 -> ====FIN======
[color=black]15:01:34.250 -> O[/color]