I wanted to control my servo with a Bluetooth module.
I gave certain values for rotating it. When I press those values the servo rotates, but then retains its original position and the PC also makes the sound of the Arduino board disconnecting because I powered the board with my PC (tried with powerbank also). Even my Bluetooth disconnects.
This is my code:
#include <Servo.h>
Servo myservo;
int pos = 0;
char data = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
data = Serial.read();
if (data == 'F') {
// for (pos = 0; pos <= 180; pos += 1)
myservo.write(180);
delay(1);
}
}
else if (data == 'B') {
// for (pos = 180; pos >= 0; pos -= 1)
myservo.write(0);
delay(1);
}
}
Connections:
Servo:
red wire -- 5 V (not working at 3.3 V)
orange wire -- pin9
brown wire -- gnd
Bluetooth (hc05):
5 V -- 5 V
gnd -- gnd
rx -- tx
tx -- rx
F
is not doing anything except waiting (you are not writing to the servo there). If your USB connection fails when the servo moves that can indicate a power issue. What type of servo do you have exactly? Have you tried powering the servo seperately from a more capable power source?}}
, unless you are an expert programmer ... always position}
at the correct indentation level for each block that they delimit ... prepare yourself for a world of hurt if you do not learn to format your code properly