0
votes

today I tried to setup a servo motor running along with this sketch on Arduino. For some reason, when I add the setup lines for the servo, the software serial port doesn't receive data normally. I tried to change the software serial to the normal Serial port on Arduino but the same thing happens.

#include<SoftwareSerial.h>
#include<Servo.h>

Servo myServo;
SoftwareSerial BT1(3,2); // TX, RX 

int val;
int servoPin = 7;
int ledPin = 10;

void setup() {
  pinMode(ledPin,OUTPUT);
  BT1.begin(9600);

  myServo.attach(servoPin);
  myServo.writeMicroseconds(2000); //2 ms for Tower Pro SG 90 Servo
  myServo.write(90); //Initialize at servo's middle point

}

void loop() {

  if (BT1.available()) {
    int i = (int)BT1.read();
    setIntensity(i);
  }

}

void setIntensity(int value) {
  if (value >= 0 && value <= 110) {
    analogWrite(ledPin, value);
  }
}

When I comment out the setup lines for myServo, the Arduino works like a charm. How can I solve this? I'm using an Arduino Duemilanove.

1
I would suggest you to use AltSoftSerial its better than SoftwareSerial.SoftwareSerial is bit depricatedMathews Sunny
Can you specify what s the trouble tht you faced when you changed.as i couldnt find any mistakesMathews Sunny
When using Servo and SoftwareSerial libraries, there is a conflict between them. When adding the setup lines for myServo: myServo.attach(servoPin); myServo.writeMicroseconds(2000); //2 ms for Tower Pro SG 90 Servo myServo.write(90); //Initialize at servo's middle point , the SoftwareSerial doesn't work right.karl_m
Is the above given code the one you saying not working fine?Mathews Sunny
It's probably an issue with the libraries using the same timers and hence interfering with each other. You can try to use another library that uses different timers. Or use hardware serial.Laurenz

1 Answers

0
votes

NeoSWSerial is a suitable replacement for SoftwareSerial. NeoSWSerial won't interfere with Servo, because it doesn't use any extra resources, and it doesn't block interrupts for long periods of time.

It's also available from the IDE Library Manager, under the menu Sketch -> Include Library -> ManageLibraries.