Noob question Arduino not in sync?

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Noob question Arduino not in sync?

Post by freijn »

Hello,

I have just started in the world of domotics.
Bought the cheap 433 transmitter and receiver "DIY 433MHz Wireless Receiving Module Superregeneration for Arduino "
Connected them to my arduino Nano but am not able to receive any stable signal.
I have used all libraries like MySensors and Remote switch. Using these NOTHING is received by the Arduino.

I have found the below code. When I load this in the Arduino at least the serial monitor does give me some output but never the same even if
I press the same button again :-(

I even tried a different arduino ( different type of board )

Does anybody recognize what I am doing wrong??
I could not find and topic on this one. Appologies if there is a similair topic already.

Cheers,

Frank

The output from the serial monitor

Code: Select all

Init : Start decoding
Found Header : 3 bits word
001 => 1
Found Header : 15 bits word
001000001010001 => 1051
Found Header : 1 bits word
1 => 1
Found Header : 1 bits word
1 => 1
Found Header : 3 bits word
001 => 1
Found Header : 7 bits word
0000001 => 1
Found Header : 1 bits word
1 => 1
The only sample code which does make some chr's

Code: Select all

/* 
This is just a naive trial for decoding streams for 433MHz remote controls
The aim is just to have differents results for differents remote buttons
Image of the remote control (Jolly Open) : http://mgrigaut.free.fr/arduino/Plip.jpg
Output signal from the 433MHz receiver : http://mgrigaut.free.fr/arduino/Jolly2.gif (probably NRZ)
*/

// Led on Arduino hardware (I use Arduino Nano)
const int led = 13;  // Blink when code found
// debug=1 for debugging
const int debug=1;
// input pin that receive signal
const int sig = 2;
// Code timing :
// Short bit is 600 us ; Long bit is 1.2 ms so 900 us is a good threshold to compare.
const int shortbit=9; // 0.9 ms (9 times 100 microseconds)
int maxlength; // 5 time shortbit (computed in Setup)
const int headerlength=25; // 25ms minimum (different units than shortbit)
int last = 0;
int cur = 0;
int nochange = 0; // Number of times (of 1 ms) the signal does not change
char resu[32] = ""; // Contains binary code as as string of '0' and '1'
int dur=0; // The measured duration between two changes
int bit=0; // the decoded bit (0 or 1)
int nb=0;
unsigned long HexCode=0;  // The found code

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(sig, INPUT);
  maxlength=5 * shortbit;
  Serial.print("\nInit : Start decoding\n");
}

void loop() {
  // Header detection :
  cur=digitalRead(sig);
  // Wait for a long stable signal :
  if (last!=cur) {
    // Not stable yet
    last=cur;
    nochange=0;
  } else {
    nochange++;
    if (nochange>headerlength) {
        // Header found
        // pri() will print only if debug=1
        pri("Found Header : ");
        // Wait end of header (at 0.1 ms precision)
        while(cur==last) {
          cur=digitalRead(sig);
          delayMicroseconds(100);
        }
        // Start decode whatever comes after this end of header :
        nb=my_decode();
        // nb is the number of bits detected
        // If the expected number of bits is known, then the following
        //    condition can be more precise to have a better filtering.
        if (nb>0) {
          // All the following stuff is mainly for debugging
          pri((String)nb);
          pri(" bits word\n");
          pri(resu);
          pri(" => ");
          // Build a filter to print out only known codes :
          // Now, this is the only useful info (printed even if debug=0) :
          Serial.print(HexCode,HEX);
          if (HexCode==0xCCCCCD) {
            Serial.print(" DOOR SECTIO");
          }
          if (HexCode==0xCCCCD3) {
            Serial.print(" DOOR PORTAL");
          }
          Serial.println();
          // Blink led :
          // Not useful since Serial.print makes another led blink on Arduino Nano
          digitalWrite(led, HIGH);
          delay(5);
          digitalWrite(led, LOW);
        } else {
          pri("Noise...\n");
        }
        // Done. Wait now for a new header
        nochange=0;
    }
  }
  // Wait every ms for the header
  delay(1);
}

int my_decode() {
  int nbits=0;
  cur=last;
  resu[0]='\0';
  // Duration (in 0.1 ms):
  dur=0;
  // Result :
  HexCode=0;
  while(dur<maxlength) {
    dur=0;
    // Wait for a change
    while(cur==last) {
      cur=digitalRead(sig);
      delayMicroseconds(100);
      dur++;
    }
    // So, state has changed now.
    last=cur;
    
    // this coding does not follow any specs.
    // See if duration is greater than 900 us :
    // This If then else is there to spend the same time in each branch
    if(dur>shortbit) {
      bit=1;
    } else {
      bit=0;
   }
    // Add bit to string resu :
    sprintf(resu,"%s%d",resu,bit);
    // Shift HexCode 1 bit to multiply it by 2 :
    HexCode=HexCode<<1;
    // Then add bit to it :
    HexCode+=bit;
    // Increment number of bits
    nbits++;
    // Avoid overflow :
    if (nbits>31) {
      nbits=0;
      // exit loop :
      dur=maxlength+1;
    }
  }
  return(nbits);
}


void pri(String truc) {
  if (debug==1) {
    Serial.print(truc);
  }
}
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Noob question Arduino not in sync?

Post by freijn »

Nobody any suggestions help??

Am I the only one suffering ???? :-(
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest