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