# Deciphering RF Codes. (!= error???)

**URL:** <https://forum.arduino.cc/t/deciphering-rf-codes-error/519402>\
**Category:** Programming\
**Created:** [April 9, 2018, 11:51pm UTC](https://forum.arduino.cc/t/deciphering-rf-codes-error/519402 "2018-04-09T23:51:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrMetro](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@MrMetro](https://forum.arduino.cc/u/MrMetro)\
**Post date:** [April 9, 2018, 11:51pm UTC](https://forum.arduino.cc/t/deciphering-rf-codes-error/519402/1 "2018-04-09T23:51:13Z")

</div>

I have 2 arduino's connected via RF. There are 2 buttons on one and 3 LED's on the other. One LED stays on verifying connection. When button1 is pushed LED2 stays on, when button2 is pushed LED3 flashes.

I have this broken down as such, when button1 is pushed, it sends a 6 digit code to the LED arduino. When button 2 is pushed it sends another code to the LED arduino. I can see the codes in the serial monitor.

I am trying to put together a garage door monitor (dang kid leaves it up). I want to know I have a connection to the RF unit, when it is up (button1) or when it is down (button2). The other applications for this code would be amazing and I'd love to share. I have searched all over the googles and youtubes but they mostly all use virtual wire or don't use multiple buttons to send codes for specific pins. I found one example out there that seemed close to what I'm trying to achieve but it was in .hex and I couldn't get it to open in visual studio.

Transmitter:

```cpp
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

// tx/rx configuration
const int txSpeed = 2000;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
const int buttonPin1 = 2;    // the number of the pushbutton pin
const int buttonPin2 = 3;    // the number of the pushbutton pin
int buttonState = 0;        // variable for reading the pushbutton status

RH_ASK driver(txSpeed, rxPin, txPin, pttPin);

void setup()
{
        if (!driver.init())
    Serial.println("rf driver init failed");
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(buttonPin1, INPUT);
    pinMode(buttonPin2, INPUT);
}

void loop()
{
   
  buttonState = digitalRead(buttonPin1);
  if (buttonState == HIGH)
  {    // turn LED on:
 
    const char *msg1 = "349516";
    driver.send((uint8_t *)msg1, strlen(msg1));
    driver.waitPacketSent();
    delay(20);
 }
  buttonState = digitalRead(buttonPin2);
  if (buttonState == HIGH)
  {    // turn LED on:
 
  const char *msg2 = "654321";
    driver.send((uint8_t *)msg2, strlen(msg2));
    driver.waitPacketSent();
    delay(20);
}}

```

Receiver:

```cpp
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

// tx/rx configuration
const int txSpeed = 2000;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 8;

int val1 = 349516;
int val2 = 135790;

RH_ASK driver(txSpeed, rxPin, txPin, pttPin);

void setup()
{
  Serial.begin(9600);  // Debugging only
  if (!driver.init())
    Serial.println("init failed");
    pinMode(ledPin4, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin5, OUTPUT);
}

void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};
  uint8_t buflen = sizeof(buf);
  if (driver.recv(buf, &buflen)) {// if message received, save it
  for(int i=0;i<sizeof(buf);i++) {
  Serial.println((char*)buf); // print received message, used for debugging.
   
  }
 if(buf[0] != 654321){ // and if the first letter in message array is X
     
      digitalWrite(ledPin4,HIGH);
      delay(500);
      digitalWrite(ledPin4,LOW);
      delay(500);
    }
    if(buf[0] != 349516){ // and if the first letter in message array is X
       
      digitalWrite(ledPin3,HIGH);
      delay(500);
      digitalWrite(ledPin3,LOW);
      delay(500);
    }
  }
    else if(buf[0]!= 000000){
    digitalWrite(ledPin5,HIGH);

    }
  }

```

There seems to be an issue with the loop. I believe don't have the correct code here: if(buf[0] != 654321

What really happens, is the loop happens no matter which code is received: 654321 or 349516.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 10, 2018, 12:02am UTC](https://forum.arduino.cc/t/deciphering-rf-codes-error/519402/2 "2018-04-10T00:02:27Z")

</div>

`if(buf[0] != 654321) {`"buf" is a character array, and buf[0] is the first byte in that array. Consequently, its value can only be between 0 and 255.

If you want to compare character arrays (also called C-strings), one method would be to use the function [strcmp()](https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm).

If you want to compare single ASCII characters stored in the array, then something like the following will work:

```cpp
if(buf[0] != '6') {

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:07pm UTC](https://forum.arduino.cc/t/deciphering-rf-codes-error/519402/3 "2021-05-05T23:07:28Z")

</div>

Closed on May 5, 2021, 11:07pm UTC


