Controling a continuous servo using bluetooth

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);

}

bluetooth_servo.ino (428 Bytes)

What is sending the data - maybe it is sending '0' followed by a carriage-return and line-feed.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And there is this clever idea if you just want to check for single characters.

...R

Here is your loop() function formatted better

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).

THANKS UKHeliBob, that fixed my problem, youre awesome man. well earned karma

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