Hi, i have recently bought a brand new HC-06 bluetooth module and wanted to try it out with my arduino uno, however i'm having some problems. tough the module works fine (i turned a LED on and off succesfully), i cant get it to run my servo, it just doesnt do anything. i want the servo to turn on and spin in one direction when i type 1 and stop when i type 0. Can anyone tell me what is teh problem with my code
#include <Servo.h>
int ledPin = 13;
int state = 0;
Servo myservo;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Default connection rate for my BT module
myservo.attach(9);
}
void loop() {
if (Serial.available() > 0)
state = Serial.read();
if (state == '0')
digitalWrite(ledPin, LOW);
myservo.write(90);
if (state == '1')
digitalWrite(ledPin, HIGH);
myservo.write(95);
void loop()
{
if (Serial.available() > 0)
state = Serial.read();
if (state == '0')
digitalWrite(ledPin, LOW);
myservo.write(90);
if (state == '1')
digitalWrite(ledPin, HIGH);
myservo.write(95);
}
When the if statements are true only the next line of code will be executed. The servo.write() commands do not depend on the if statements and will always be executed,
Try this
void loop()
{
if (Serial.available() > 0)
{
state = Serial.read();
if (state == '0')
{
digitalWrite(ledPin, LOW);
myservo.write(90);
}
if (state == '1')
{
digitalWrite(ledPin, HIGH);
myservo.write(95);
}
}
}
Note how the dependant statements are in pairs of { and }
Im using the "S2 terminal" app for sending the data. and as i said the same 1 or 0 controls the LED fine so shouldn't it be the same for the servo? i dont really know
want the servo to turn on and spin in one direction when i type 1 and stop when i type 0.
Do you have a continuous rotation servo? If so, the servo.write() command is wrong
Description
Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).
gustavs:
THANKS UKHeliBob, that fixed my problem, youre awesome man. well earned karma
Do you understand what was wrong ?
I suggest that you use { and } round dependant code blocks even if there is only one line of code. Auto Formatting the code in the IDE helps to show up such problems