Xbee bi-directional communication

Hello,

With two Xbee pro S2C (AT mode), I try to remotely control a servomotor via a potentiometer and at the same time receive data from sensors (BMP280 and a Sharp IR distance sensor).
Concretely, on one side, I have an arduino uno (connected to a computer) connected to a potentiometer and an Xbee. On the other, I have an arduino nano connected to a servo motor, a bmp280, a distance sensor and an Xbee.
The first board (Uno) sends the potentiometer data to the second board (nano) and must receive the sensor data. The second card (nano) executes the commands of the potentiometer to make the servo motor turn and at the same time sends the data from the sensors to the first card (Uno).

The problem is that my codes do not work!

Here are the codes:
For the first board (Uno):

#include <SoftwareSerial.h>
SoftwareSerial xbee(0,1);

int potPin = 0;//A0

void setup() {
  // put your setup code here, to run once:
   xbee.begin(9600);
Serial.begin (9600);

}

void loop() {
   if(xbee.available()) {
    while(xbee.available())  {
      Serial.write(xbee.read());
      }
  // put your main code here, to run repeatedly:
int val = map(analogRead(potPin), 0, 1023, 0, 9);
Serial.println(val); 
delay(100);
}
}

For the second board (nano):


#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial xbee(0,1);

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

int Capteur_distance = A1;  // Broche analogique pour lire le capteur IR

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

int servoPin = 9; 
Servo astrocanServo;

void setup() {
  
Serial.begin (9600);
Serial.println(F("BMP280 test"));

  //if (!bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID)) {
  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    while (1) delay(10);
  }

  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */

astrocanServo.attach(servoPin);
}

void loop() {

 {
    Serial.print(bmp.readTemperature( ));
    Serial.print(",");
       
    Serial.print(bmp.readPressure( ));    
    Serial.print(",");
       
    Serial.print(bmp.readAltitude(1037.5)); /* Adjusted to local forecast! */
    Serial.print(",");
    
     float volts = analogRead(Capteur_distance)  * 0.0048828125;  // valeur du capteur * (5/1024)
 int distance = 5 * pow (volts, -1);  // élaboré à partir du graphique 5 = distance théorique / (1 / Volts)
 Serial.println (distance);  // Distance dans le moniteur série
     delay(1000);
  }
  {
while(Serial.available() == 0);

int data = Serial.read() - '0';

int pos = map(data, 0, 9, 0, 180);
pos = constrain(pos, 0, 180);
Serial.println(data);
astrocanServo.write(pos);
Serial.flush();
  }

}

Thank you for your help !

this pins are preferred for Serial. take for SoftwareSerial other pins.

#include <SoftwareSerial.h>
SoftwareSerial xbee(0,1);

remove this in second sketch

Thank you for your answer. I have already tried with other pins (in both codes). It doesn't work.

if you tried other pins why in earth do you came to 0/1 pins?

write working codes

I don't understand this kind of remarks?

yes. pls, excuse me. what I want to say is you should provide any possible information and if someone say something to test then you do it, then provides results of this test. so we learn your project and comparing with our own experience.

Ok, thanks. I tried these two codes by changing the pins (0,1) by (2,3) but it does not solve the problem. That's why I put (0,1) back in the code. Moreover, I can control the servomotor with the potentiometer via Xbee. I can receive the data from the sensors via Xbee. But I can't do both at the same time.

do you able control they at same time if everything connected to one same board?

constrain() is useless

{
while(Serial.available() == 0);

int data = Serial.read() - '0';

int pos = map(data, 0, 9, 0, 180);
pos = constrain(pos, 0, 180);
Serial.println(data);
astrocanServo.write(pos);
Serial.flush();
  }

compare

  {
    while (Serial.available() == 0);
    byte data = (Serial.read() - 48) * 20;
    while (Serial.available() > 0);
    Serial.println(data);
    astrocanServo.write(data);
  }

do you able control they at same time if everything connected to one same board?

Yes, I can receive data and control the servo when they are connected to the same motherboard.

do you mean sending to serial port and looking how servo is rotating should be simultaneous? why? you send 1 number and even on slowest rate of 300 it is done in 1/300 second

I have take a look on your code. pin definition is wrong. result of analogRead() is get only if it data from xbee is there. and even then result will not sent.

#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3);

const byte potPin = A0;

void setup() {
  xbee.begin (9600);
  Serial.begin(9600);
}

void loop() {
  static byte val = 0;
  while (xbee.available())Serial.write(xbee.read());
  val = map(analogRead(potPin), 0, 1023, 0, 9);
  Serial.println(val);
  xbee.print(val);
  delay(100);
}

do you mean sending to serial port and looking how servo is rotating should be simultaneous? why? you send 1 number and even on slowest rate of 300 it is done in 1/300 second

Running the motor to a certain position and receiving data from the sensors should not be simultaneous. Ideally, I should be able to move the motor and then receive data from the temperature, pressure and distance sensors.

UPD o-ops, wrong topic.

Thanks a lot! I will test these codes as soon as possible. I will keep you posted.

mark if it works.

Yes I will mark the post if it works

The code works! Many thanks.
It's just that the motor doesn't react as well to the potentiometer commands. It's a bit of a mess sometimes.