Arduino SoftwareSerial weird problem!

Hello,

I am using an Arduino UNO with a ZigBee mounted on top of it. So, I am using SoftwareSerial library to open a Serial terminal with the ZigBee pro S1 wire Antenna module, my Arduino version is 1.0. But, there is a weird problem, is that when the rate of the sent data to the zigBee increases, the Arduino becomes crazy, and some connected components to its PWM pins starts to move/ shake!. For ex, I am connecting a Electronic Speed Control (ESC) with a PWM pin, when I send more data to the zigBee the ESC arms the motor and it rotates!!!! then it stops immediately! So it's like the components have a huge voltage drop that forces them to power on then turn off again. The zigBee module and all extra components are powered up from an external battery. The zigbee module is mounted on top of a custom made board, which is mounted on top of the Arduino UNO board. I really don't know what I have to do it's my bachelor project and I am MAD! ANY HELP PLEASE! Here is my code

#include <Servo.h>
#include <SoftwareSerial.h>
#define RxD2 4
#define TxD2 5
Servo  aileronsServo;
Servo elevatorServo;
Servo rudderServo;
Servo motorServo;
int motorPos=50;
SoftwareSerial zigbeeSerial(RxD2,TxD2);
char recvChar;
int num;

void setup() 
{
  elevatorServo.attach(9);
  motorServo.attach(11);
  rudderServo.attach(6);
  aileronsServo.attach(10);

  pinMode(RxD2, INPUT);
  pinMode(TxD2, OUTPUT);
  zigbeeSerial.begin(9600);
} 

void loop() 
{ 
  if(zigbeeSerial.available() >= 4){
    recvChar = zigbeeSerial.read();
    if(recvChar == 'e'){
      char Str3[3]  = {
        zigbeeSerial.read(), zigbeeSerial.read() ,   zigbeeSerial.read()};
      num = atoi(Str3);
      elevatorServo.write(num);
    }
    else if(recvChar == 'm'){
      char  Str3[3] = {
        zigbeeSerial.read(), zigbeeSerial.read() ,   zigbeeSerial.read()};
      num = atoi(Str3);
      setSpeed(num);
    }
    else if(recvChar == 'r'){
      char  Str3[3] = {
        zigbeeSerial.read(), zigbeeSerial.read() ,   zigbeeSerial.read()};
      num = atoi(Str3);
      rudderServo.write(num);
    }
    else if(recvChar == 'a'){
      char   Str3[3] = {
        zigbeeSerial.read(), zigbeeSerial.read() ,   zigbeeSerial.read()};
      num = atoi(Str3);
      aileronsServo.write(num);

    }
  }
}
void setSpeed(int speed){
  int angle = map(speed, 50, 170
    , 50
    , 180);
  motorServo.write(angle);    
}
SoftwareSerial zigbeeSerial(RxD2,TxD2);

  pinMode(RxD2, INPUT);
  pinMode(TxD2, OUTPUT);

You told the SoftwareSerial instance that RxD2 and TxD2 are its pins. Then you go mucking about with them. Why?

      char Str3[3]  = {
        zigbeeSerial.read(), zigbeeSerial.read() ,   zigbeeSerial.read()};
      num = atoi(Str3);

This won't work. Str3 is NOT a string, which is what atoi() expects as input. You need to NULL terminate the array of characters to make it a string. The array is not large enough for that, though. I'm not sure that declaring an array, and initializing it with executable code, in one step works.

So it's like the components have a huge voltage drop that forces them to power on then turn off again. The zigBee module and all extra components are powered up from an external battery.

That battery may not be adequate.

What is Serial doing? Do you have the same problems if you use Serial instead of SoftwareSerial?

PaulS:

SoftwareSerial zigbeeSerial(RxD2,TxD2);

pinMode(RxD2, INPUT);
  pinMode(TxD2, OUTPUT);



You told the SoftwareSerial instance that RxD2 and TxD2 are its pins. Then you go mucking about with them. Why?



char Str3[3]  = {
        zigbeeSerial.read(), zigbeeSerial.read() ,  zigbeeSerial.read()};
      num = atoi(Str3);



This won't work. Str3 is NOT a string, which is what atoi() expects as input. You need to NULL terminate the array of characters to make it a string. The array is not large enough for that, though. I'm not sure that declaring an array, and initializing it with executable code, in one step works.



> So it's like the components have a huge voltage drop that forces them to power on then turn off again. The zigBee module and all extra components are powered up from an external battery.


That battery may not be adequate.

What is Serial doing? Do you have the same problems if you use Serial instead of SoftwareSerial?

I am very sorry for the late reply, but I lost my internet connection :(. Anyways, I tried it the same code/ procedure with Arduino duemilanove which is somehow similar to the UNO and it worked perfectly! I really don't understand what was the problem but I guess my UNO board isn't working correctly anymore!