-1

i wanted to control my servo with bluetooth modue. I gave certain values for rotating it. but when i press those values the servo rotates but then retains it's original position and pc pc also make the sound of arduino board disconnecting because i powered the board with my pc(tried with powerbank also)

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) even this line dosent work
         myservo.write(180);
         delay(1);
        }}
    else if(data == 'B'){     
//       for (pos = 180; pos >= 0; pos -= 1)  even this line dosent work
         myservo.write(0); 
         delay(1);
        }}

connections servo red wire -- 5v (not working in 3.3 v) orange wire -- pin9 brown wire -- gnd

bluetooth (hc05) 5v -- 5v gnd -- gnd rx -- tx tx -- rx

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94

1 Answers1

1

Your else if (data=='B'){ is in the wrong place - it's part of the if(Serial.available() > 0) { condition - instead of the if(data == 'F'){ condition. Notice the double }} in the line before.

I've corrected the code and reformated it so you can clearly see the matching opening and closing braces.

This works on Tinkercad Circuits passing commands through the serial monitor - so imagine it would work fine taking commands from bluetooth.

#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(pos);
         delay(1);
      }
    }
    else if(data == 'B'){ 
      for (pos = 180; pos >= 0; pos -= 1) {
         myservo.write(pos); 
         delay(1);
      }
    }
  }
}
cguk70
  • 611
  • 3
  • 7
  • that dint solve my error i am face the same error – Akshit Singh Jun 23 '22 at 15:06
  • Have you tried running this whilst connecting to your computer and sending the commands through the Serial monitor, rather than Bluetooth? Also - how have you set up the HC05 - normally it defaults to 38400 baud? I think the code looks alright - although you may want to setup a Software Serial port for the bluetooth so you can send debug through the standard serial port. You may find you get a better response from the [Arduino Stack Exchange](https://arduino.stackexchange.com/) site as they're likely to help with the bluetooth connectivity. – cguk70 Jun 23 '22 at 15:17
  • doing it with computer also dosent work – Akshit Singh Jun 23 '22 at 15:34
  • Sounds like it's not a programming issue then so you would be better off posting to the arduino site. Are you using a micro servo or something bigger? It's possible it's drawing too much power from the Arduino causing it to reset? – cguk70 Jun 24 '22 at 07:25