Go Down

Topic: Bluetooth Serial Module hc05 (Read 51 times) previous topic - next topic

Marciokoko

Hi guys,

I have this simple bluetooth-servo sketch which works beautifully.

This servo motor sketch only sorta works:

Code: [Select]
// www.roboremo.com
// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O Green------ Rx pin 11
// RX-I Yellow------ Tx pin 10
// Servo       Arduino
// GND ------- GND
// VCC ------- VCC
// signal ---- pin4

#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(11,10); //Rx, Tx method signature

Servo myServo;

void setup() {
  delay(500); // wait for bluetooth module to start
  bluetooth.begin(9600); // This is for comm with BT
  Serial.begin(57600);// This is for getting commandsfrom the Serial
  myServo.attach(4, 1000, 2000); //defaults to 544,2400 respectively
}

void loop() {
if(bluetooth.available()>0) {
    char c = (char)bluetooth.read();
    bluetooth.println(c);
    if(c=='1') { //if data is 1
      myServo.write(1000);
    }
    if(c=='0') { //if data is 0
      myServo.write(2000);
    }
  }
}


The problem Im dealing with is writing an exact value to the bt.  I tried using this code from roboremo:

Code: [Select]
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(11,10); //Rx, Tx method signature

Servo myServo;

char cmd[100];
int cmdIndex;

void exeCmd() { 
  // "servo" is the servo motor id
  if(cmd[0]=='s' &&
     cmd[1]=='e' &&
     cmd[2]=='r' &&
     cmd[3]=='v' &&
     cmd[4]=='o' &&
     cmd[5]==' ') {
       
       int val = 0; 
       for(int i=6; cmd[i]!=0; i++) {
         val = val*10 + (cmd[i]-'0');
       }
       myServo.writeMicroseconds(val);
       Serial.println(val);     
     }   
}
void setup() {
  delay(500); // wait for bluetooth module to start
  bluetooth.begin(9600); // This is for comm with BT
  Serial.begin(57600);// This is for getting commandsfrom the Serial
  myServo.attach(4, 1000, 2000); //defaults to 544,2400 respectively
  cmdIndex = 0;
}
void loop() {
  if(bluetooth.available()>0) {
    char c = (char)bluetooth.read();
    bluetooth.println(c);
    if(c=='\n') { //if data is a 'newline' then set the cmd[] element to 0
      cmd[cmdIndex] = 0;
      exeCmd();  // ...................................execute the command
      cmdIndex = 0; // .................................reset the cmdIndex
    } else {      //if data is real data, then set the cmd[] element to that data!
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;//.....if the cmdIndex<99 then add 1 to it!
    }
  }
}


but im getting weird garbage in the bt terminal app.  When i type in servo 1234 I get:

a letter s
a few symbols of a ? inside a diamond

and the servo just starts buzzing.

How can I debug why Im getting those symbols?

PaulS

Quote
How can I debug why Im getting those symbols?
You can stop doing anonymous printing.

Code: [Select]
       Serial.println(val);     
Something shows up in the Serial Monitor window, but you haven't a clue what it is.

Compare the code, and results, of the code above to:
Code: [Select]
       Serial.print("val = ");     
       Serial.println(val);   

Sure, your code was easier to type, but mine produces an order of magnitude more information.

Start by confirming that the data in cmd IS what you think it should be, in exeCmd. Add:
Code: [Select]
Serial.print("cmd = [");
Serial.print(cmd);
Serial.println("]");

to the top of exeCmd(). Does it print exactly what you think it should?

Go Up