Noob question Arduino not in sync?



  • 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. Receiver output is connected to the D2 pin on my 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 )
    I tried a different pin D3. Does the nano support interupts?
    I tried a different receiver.

    Does anybody recognize what I am doing wrong?? or can give me a suggestion?
    I could not find and topic on this one. Apologies if there is a similar topic already.

    Cheers, and many thanks for your time.

    Frank

    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
    
    /*
    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);
      }
    }
    
    

  • Mod

    @freijn shot in the dark: did you connect an antenna to the receiver module?



  • No, I can do , but the transmitter is about 10 cm away.
    The 'fustrating' bit is that with all other libraries NOTHING is comming out of the serial port.
    Only with the code here in my post I am able to get some signs out of it. Seems to me a timing issue, because even with the above code the result is never the same, even by pressing the same button.

    I read a solution from sombody else to set the correct clock timing in the arduino sketch on the compiling menu. I have selecte 'Arduino Nano" when I do select that one, I have no option to select 8Mhz of 16Mhz. Also I see contradition info about int0 not being available on Ardino Nano.

    It must be something like that I am afraid, but spend so many evenings now with no result, hence I am desparate seeking help 😞

    I will connect an antenna and do the test again to be 100% sure.
    MANY THANKS for the reply !!!!

    Frank



  • You can't specify frequency on the Arduino Nano, it's only using 16MHz as described here:
    https://www.arduino.cc/en/Main/ArduinoBoardNano#techspecs

    Off course the INT0 is available on Arduino Nano, all those boards are using same ATmega328.

    Hmm debugging from beginning. what if you remove all code in the "loop" and your functions below? just as a test to see some basic stuff working...

    I have at some point had some weird stuff that I have used up memory and the program just rebooted right after I started, might have been stack or heap that collided


  • Mod

    @freijn I agree with @bjacobse . Start simple when you run into unexplainable behavior.
    The Arduino IDE comes with some very basic examples, e.g:

    • 01.Basics/Blink (blinks a led one second on, one second off)
    • 01.Basics/DigitalReadSerial (Read a digital input and present its state over the serial port at 9600 baud).

    Try those first and see if they function correctly.



  • @Yveaux said:

    Basics/DigitalReadSerial

    So blink test does work fine,

    Basics/DigitalReadSerial however produces constant change like
    0
    1
    1
    0
    1
    0
    1
    1
    1
    1
    0
    1
    1
    1
    0
    1
    1
    0
    1
    1
    1
    1
    1
    1
    0
    1
    1
    0
    1
    1
    0
    0
    1

    If I disconnect the D2 pin it keeps running at the same weird speed.
    Only if I disconnect the +5 or ground to the receiver it does stop producing this output.

    I can see difference in blinking speed when I do transmit 433 signals with my remote controller.
    More or less can see my messages between the other blinking.

    I do not understand why it is so busy blinking and producing 1's and 0's
    Level of output of the receiver to D2 not correct?
    The reveicer is connected to the +v5 and GND on the arduino.

    Any hints?

    Thanks

    Frank



  • Hmm strange,
    could it be due to receiving something else that you remote? I assume the 433MHz receiver is receiving anything it can pick up


 

262
Online

7.8k
Users

8.7k
Topics

93.1k
Posts