I'm working on RF modules using Arduino but I observed the if statement is only running once and I need to reset the receiver every time to make it work with real-time changes in the transmitter.
I connected the 16X2 LCD display for displaying the data. If I start the transmitter and started sending data as "1", it's showing "Bye" but when I change the transmitter data to "0", then also it is showing "Bye" only.
I changed a code a little bit but I felt it is the problem with the previous data stored in buf variable.
I'm expecting the LCD should display "Hi" when I changed the transmitting data to "0" from "1" which is not happening only "Bye" is displaying every time.
Here is the Arduino receiver code:
#include <RH_ASK.h>
#include <SPI.h>
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
RH_ASK rf_driver;
char message[2];
void setup()
{
rf_driver.init();
Serial.begin(9600);
lcd.begin(16,2);
}
void loop()
{
uint8_t buf[2];
uint8_t buflen = sizeof(buf);
buf[1] = '\0';
if (!rf_driver.recv(buf, &buflen))
{
if (strcmp((char*)buf, "0") == 0) {
Serial.println("Hi");
if (strcmp(message[0], (char*)buf[0]) != 0) {
lcd.print("Hi");
}
lcd.display();
}
else if (strcmp((char*)buf, "1") == 0) {
Serial.println("Bye");
if (strcmp(message[0], (char*)buf[0]) != 0) {
lcd.print("Bye");
lcd.display();
}
}
}
message[0] = (char*)buf[0];
}
Here is the transmitter code:
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
void setup()
{
rf_driver.init();
Serial.begin(9600);
}
void loop()
{
const char *msg = "1";
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(1000);
}
