hi there im using this code for the NRF tx and rx but is able to send the data properly in analog but at the receving end it is showing only 223 as a result
#include <SPI.h>
#include <RH_NRF24.h>
// Singleton instance of the radio driver
RH_NRF24 nrf24;
int button = 5;
int greenLed = 3;
int redLed = 4;
const int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(button, INPUT);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
}
void loop()
{
if (digitalRead(button)) {
sensorValue = analogRead(analogInPin);
//button is pressed, message should be sent, turn the green LED on
digitalWrite(greenLed, HIGH);
Serial.println("sending");
// outputValue = map(sensorValue, 0, 1023, 0, 255);
// Serial.println(sensorValue);
Serial.println(outputValue);
// Send a message
uint8_t data = (outputValue);
Serial.println(data);
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
digitalWrite(greenLed, LOW);
delay(5);
memset(data, 0, sizeof(data));
}
else {
// Wait for a message
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
while (nrf24.waitAvailableTimeout(200) && nrf24.recv(buf, &len))
{ Serial.println((uint8_t)buf);
uint8_t rec = buf;
Serial.println(rec);
//something was received, turn the right LED on
digitalWrite(redLed, HIGH);
Serial.println("recieving");
memset(buf, 0, sizeof(buf));
}
digitalWrite(redLed, LOW);
}
delay(10);
}
yeah robin i have looked to the suggested example as you said and im currently doing editing on that to receive the analog data when i done that i'll update here..........
hey i have done the editing as the master is sending the string properly but at the slave end it is having the garbage value and at the slave end i have also tried changing the data type to char, to string, to char array, but nothing worked....
Laharsh:
hey i have done the editing as the master is sending the string properly
The only way you can know that is when the slave receives the message that was sent.
You will notice that my examples do not use the String (capital S) class and I have no idea what happens when you try to send an instance of the String class.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Change your master program so it is not using the String class. And see Serial Input Basics - simple reliable ways to receive data.