I've run into some weird behaviour and looks like I can't find an answer. I've written some simple code to control a servo motor via bluetooth using my phone.
#include <Servo.h>
#include "SoftwareSerial.h";
int servoPin = 2;
int bluetoothTx = 11;
int bluetoothRx = 10;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
Servo myservo;
char unChar;
String readString;
void setup() {
Serial.begin(9600);
bluetooth.begin(115200);
delay(100);
bluetooth.begin(9600);
myservo.attach(servoPin, 800, 2200);
}
void loop() {
if (bluetooth.available()) {
unChar = bluetooth.read();
Serial.println(unChar);
if (unChar == 'A') {
motor();
}
}
}
void motor() {
delay(15);
while (bluetooth.available()) {
char c = bluetooth.read();
readString += c;
}
if (readString.length() > 0) {
Serial.println(readString.toInt());
myservo.write(readString.toInt());
readString = "";
}
}
For android, I used the MIT App Inventor to make a basic slider.
I'm using an Arduino Mega 2560, Power HD High-Torque High-Voltage Digital Servo 1218TH and a Bluetooth Mate Silver RN-41.
Everything works fine with the exception of the servo stuttering while dragging the slider on the phone. That is one small problem. The weird behaviour appears when I comment or delete my code in the arduino loop. The servo still stutters on its place when I play with the slider. What causes this behaviour?