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);
  }
}
dda
  • 1,595
  • 1
  • 12
  • 17
jakson
  • 121
  • 4

2 Answers2

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

jakson
  • 121
  • 4
1

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.

Delta_G
  • 3,391
  • 2
  • 13
  • 24