hello, I want to control a servo motor using an arduino nano connected with an HC-06 module. I manage to connect with my android and I use an application with a slider to move the servo between 0° and 180°. the problem is that when i vary the slider, the servo moves from left to right a few degrees and returns to its initial position at 90°. Here is the code I used:
#include<SoftwareSerial.h>
#include<Servo.h>
Servo x;
int bttx=2; //tx of bluetooth module is connected to pin 9 of arduino
int btrx=3; //rx of bluetooth module is connected to pin 10 of arduino
SoftwareSerial bluetooth(bttx,btrx);
void setup()
{
x.attach(9); // servo is connected to pin 11 of arduino
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
if(bluetooth.available()>0) //if bluetooth module is transmitting data
{
int pos=bluetooth.read(); // store the data in pos variable
Serial.println(pos);
x.write(pos); //move servo head to the given position
}
}
