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 !