Issue I am having is when the Xbee is attached in my sketch, the brushless motors I'm using experience pulse randomly every few seconds. When I comment out the Xbee declaration and associated code for reading the serial they work fine (setting the throttle above 1100us).
//COORDINATOR
#include <SoftwareSerial.h>
SoftwareSerial xbee(2,3);
int sensorPin = A0;
int sensorValue = 0;
void setup()
{
Serial.begin(9600);
xbee.begin(9600);
}
void loop()
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue,0,714,0,500);
Serial.println(sensorValue);
xbee.println(sensorValue);
delay(40);
}
//MOTORS
#include <SoftwareSerial.h>
SoftwareSerial xbee(8,9);
#include <Servo.h>
double throttle = 1000; //Microseconds
int a;
const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
Servo esc1;
void setup()
{
xbee.begin(9600);
Serial.begin(9600);
esc1.attach(4);
esc1.writeMicroseconds(1000);
delay(1000);
}
void loop()
{
recvWithEndMarker();
showNewData();
Serial.println(a);
esc1.writeMicroseconds(throttle + a);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
// if (Serial.available() > 0) {
while (xbee.available() > 0 && newData == false) {
rc = xbee.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
newData = false;
a = atoi(receivedChars);
}
}