0

I am making a car that is to be controlled by Android app through bluetooth. The problem that I am facing with servo motors is that before bluetooth module hc-05 gets connected to any bluetooth device, the servo motors rotate irrespective of whether it is connected or not.
Here's my arduino code for servo motors:

#include<Servo.h>
Servo servo1;
Servo servo2;
char val;
void setup()
{
  Serial.begin(9600);
  servo1.attach(5);
  servo2.attach(9);
}

void loop()
{
  if(Serial.available())
  {
    switch(Serial.read())
    {
      case 'F':
        servo1.write(180);
        servo2.write(180);
      break;
      case 'B':
        servo1.write(90);
        servo2.write(90);
      break;
    }
  }
}

I don't know why it' rotating. It's very frustating. Plz help. By the way the servo motors that I bought rotates only 180 degree.But I made it 360 degree by removing some part from inside. Plz help and correct me if my code is wrong.

Irfan Ansari
  • 73
  • 2
  • 9
  • 1
    Your code looks fine (tested here with one servo). Your modification to the motor is probably the issue. I think you want a [continuous rotation servo](https://learn.adafruit.com/adafruit-motor-selection-guide/continuous-rotation-servos). But I'm not an engineer. You might get a better answer at [arduino.se]. – 001 Mar 01 '17 at 14:29
  • Thank you very much Johnny Mopp for your advice. I'll try to test it with continuous rotation servo – Irfan Ansari Mar 01 '17 at 14:36

1 Answers1

0

Try to add here:

switch(Serial.read())
- '0'`

Like here:

switch(Serial.read() - '0')
Lowder
  • 25
  • 1
  • 1
  • 7
  • This does not seem to solve the issue, since the `switch` has only two cases `F` and `B`, and by no means the output of `Serial.read()` is fed as input to either `servo`. – Patrick Trentin Mar 01 '17 at 16:02