I am getting the "nan" message on the receiver serial monitor. I read that it shows when you declare non-number variable as float type. However, I cannot find the root cause of it and would like to see an explanation on it.
Here is the transmitter code:
#include <NewPing.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define TRIGGER_PIN1 2
#define TRIGGER_PIN2 4
#define ECHO_PIN1 3
#define ECHO_PIN2 5
#define MAX_DISTANCE 400
float distance1;
float distance2;
RF24 radio(9,8);
const byte address[6]="00001";
const byte address1[6]="00002";
NewPing sonar1(TRIGGER_PIN1,ECHO_PIN1,MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN2,ECHO_PIN2,MAX_DISTANCE);
void setup()
{
delay (1000);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.openWritingPipe(address1);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop()
{
distance1=sonar1.ping_cm();
distance2=sonar2.ping_cm();
if (distance1>=400 || distance1<=0 && distance2>=400 || distance2<=0)
{
radio.write(&distance1, sizeof(distance1));
radio.write(&distance2, sizeof(distance2));
delay(1000);
}
}
And here is the receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9,8);
const byte address[6]="00001";
const byte address1[6]="00002";
float distance1;
float distance2;
void setup()
{
delay(1000);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.openReadingPipe(1, address1);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop()
{
if (radio.available())
{
while(radio.available())
{
radio.read(&distance1, sizeof(distance1));
radio.read(&distance2, sizeof(distance2));
Serial.println(distance1);
Serial.println(distance2);
}
}
delay(1000);
}
What I am trying to get is two ultrasonic sensors to get distance on the TX Arduino in a sort of a X, Y coordination for a working machine which should move in X and Y coordinates. I need the ultrasonic sensors to get the distance data and send it to the receiver, which will later be saved on an SD. But I first need to straighten this problem with the "nan". Thank you, and all help is welcomed