1

The code below is receiver code using nRF24L01. In the loop section I'm comparing the char that was being sent from the Arduino transmitter. In this case the char array says "I'm off" just like the char array compare below. Even though the text is the same, it does not do what it is supposed to do.

Basically the if function below does not seem to work for some reason, both are char arrays (the text[32] and compare[32]) and both will have the same value which is "I'm off" yet it is not registered as the same thing. I cannot figure out what is wrong with the code, please help.

This is the receiver code. The question is about the if function in the loop below.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CNS, CE
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = "";
    char compare[32] = "I'm off";
    radio.read(&text, sizeof(text));
    Serial.println(text);
    Serial.println(compare);
    // this is the problem  -->
    if (text == compare) {
      // it is supposed to print this but it does not -->
      Serial.println("not on");
    }
  }
}

This is the transmitter code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CNS, CE
const byte address[6] = "00001";
const int button = 5;

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
  pinMode(button,INPUT);
}

void loop() {
  int reading = digitalRead(button);
  if(reading == HIGH) {
    // send a message to the radio
    const char text[] = "I'm on";
    radio.write(&text, sizeof(text));
    delay(1000);
  }
  if(reading == LOW) {
    // send a message to the radio
    const char text[] = "I'm off";
    radio.write(&text, sizeof(text));
    delay(1000);
  }
}
2

That is not a good way to solve it. That is a hack. The String class does bad things on microcontrollers and it is something you should try to stay away from. What you need to compare two char arrays is the strcmp function.

if(strcmp(someCharArray, anotherCharArray) == 0){
    // the strings are the same
}

Use google to find out more about strcmp.

1

Finally i found a solution, turns out the problem was that Arduino will not comparison between char == char like this if (char == char). then i decided to CONVERT char to string. this is the example code for Receiver.

void loop() {

  if (radio.available()) {
    char text[32] = "";
    char compare[32] = "im off";
    radio.read(&text, sizeof(text));
    // you must convert char to String for it to work
    test1 = String(text);
    test2 = String(compare);

    Serial.println(text);
    //Serial.println(compare);

    if (test1 == test2)
    {
    delay(500);
    Serial.println("not on");
    }

  }

i have convert char to String then i compare the value of it, and it works like a charm. if any of you have the same problem with me and solve it after this please give me thumbs up

  • 1
    What you have said is incorrect. An Arduino will quite happily perform char == char but that is not what you are expecting it to do. char text[32] is a string (not the String object you are talking about) and to compare two strings you must use either a loop of strcmp() (or similar). By making text a string and comparing that object you are letting the class do the same thing. I know it seems pedantic, but an array of something is not the same as the something and arrays cannot be compared without some function to do it (even if that function is operator==). Nice fix though! – Code Gorilla Jun 22 '17 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.