NRF24 analog read and analog recieve

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

I am not familiar with the nRF24 library you are using.

This Simple nRF24L01+ Tutorial may help.

...R

//    outputValue = map(sensorValue, 0, 1023, 0, 255);

It's useless to comment this out and expect outputValue to ever be anything other than 0.

    uint8_t data = (outputValue);

(What's) (with) (the) (useless) (parentheses) (?)

    nrf24.send(data, sizeof(data));

Typically, a method like this expects a pointer to the data, NOT the data.

but at the receving end it is showing only 223 as a result

I'm willing to bet that more stupid stuff happens on the receiving end.

char dataToSend[10] = "Message 0";

thanks robin for support can i change this "message 0;" with another variable int or char that to send the analog readings....

Laharsh:
thanks robin for support can i change this "message 0;" with another variable int or char that to send the analog readings....

Have you looked through all the examples - there are illustrations of sending different types of data.

...R

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....

slave_code.ino (2.4 KB)

master_code.ino (2.22 KB)

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.

...R

ok let me try it

MasterSwapRoles
#define CE_PIN   8
#define CSN_PIN 10

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

   String    aStringobject;
char data[100];

int i = 0;

//char dataToSend[10] = "Message 0";
char txNum = '0';
int dataReceived[2]; // to hold the data from the slave - must match replyData[] in the slave
bool newData = false;

unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
const byte masterAddress[5] = {'T', 'X', 'a', 'a', 'a'};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Started");
    Serial.println("MasterSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(slaveAddress);
    radio.openReadingPipe(1, masterAddress);

    radio.setRetries(3,5); // delay, count
//    send(); // to get things started
    prevMillis = millis(); // set clock
}

void loop() {
  // put your main code here, to run repeatedly:

  while (Serial.available() > 0) {

    char c = Serial.read();
     Serial.print(c);
    data[i] = c;
  send();
   i++;

    if (data[i - 1] == '\n')
    {
        Serial.print(data);

       //  aStringobject = data;
   //   Serial.print(aStringobject);
//    send();
      i = 0;
      memset(data, 0, sizeof(data));
    }
 
    
  }
}

void send(){
        Serial.print("sending ");
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &data, sizeof(data) );
        radio.startListening();
        Serial.print("Data Sent ");
        Serial.print(data);
        if (rslt) {
            Serial.println("  Acknowledge received");
         
        }
        else {
         //   Serial.println("  Tx failed");
        }
}


void getData() {

    if ( radio.available() ) {
        radio.read( &data, sizeof(data) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.print(dataReceived[0]);
        Serial.print(", ");
        Serial.println(dataReceived[1]);
        Serial.println();
        newData = false;
    }
}

the master work perfectly but on the rx side still I am getting only one byte or only one character

Laharsh:
the master work perfectly but on the rx side still I am getting only one byte or only one character

Did you not read the first sentence of Reply #7 ? ? ?

Please post the latest version of the Receive program that you are using alongside the code in Reply #9

...R