Voice control and Bluetooth

Hello everyone I'm new to this. And I can't connect the code together.
I wanted to do voice control and control via Bluetooth HC-06. The code works separately. And if you connect no. ESP8266 nodemcu microcontroller. I want to make a single code

Voice control:

byte com = 0;
void setup() {
  Serial.begin(9600);
  Serial.write(0xAA);
  Serial.write(0x37);
  delay(1000);
  Serial.write(0xAA);
  Serial.write(0x21);
}
void loop() {
  while (Serial.available()) {
    com = Serial.read();
    switch (com) {
      case 0x11:   
        Serial.println("1");
        break;
      case 0x12:  
        Serial.println("2");
        break;
      case 0x13: 
        Serial.println("3");
        break;
      case 0x14:  
        Serial.println("4");
        break;
      case 0x15:  
        Serial.println("5");
        break;
    }
  }
}

Bluetooth

#include <SoftwareSerial.h>
SoftwareSerial BLUETOOTH(2, 0);
String readString;
void setup() {
  BLUETOOTH.begin(9600);
  Serial.begin(9600);
}

void loop() {
  while (BLUETOOTH.available()) {
    delay(3);
    char c = BLUETOOTH.read();
    readString += c;
  }
  if (readString.length() > 0) {
    if (readString == "|")
    {
      Serial.println("Send");
    }
    readString = "";
  }
}

Start from the bluetooth code.
At the end of loop You paste the content of the other loop.
Do the same for setup.
Do the same for declarations.
Delete duplicates like Serial.begin.

1 Like

Is that true?

#include <SoftwareSerial.h>
SoftwareSerial BLUETOOTH(2, 0);
String readString;
void setup() {
  BLUETOOTH.begin(9600);
}

void loop() {
  while (BLUETOOTH.available()) {
    char c = BLUETOOTH.read();
    readString += c;
  }
  if (readString.length() > 0) {
    if (readString == "|")
    {
    }
    readString = "";
  }
}

Yes.
Make sure the 2 codes use different I/O pins. Don't use delay.

1 Like

Fixed it.
Bluetooth D3 D4
RX TX voice control
Next, what needs to be done?

Compile it! If it's free from errors, download the code and let it run.

1 Like

Well, he initially worked. Both sketches work.
I can't combine 2 sketches and make 1 sketch

I don't understand You. Follow the instructions in reply #2, line by line. Then You will have all the code in one sketch. Give it a new name and save it.

1 Like

Here's the full sketch:

byte com = 0;
#include <SoftwareSerial.h>
SoftwareSerial BLUETOOTH(2, 0);
String readString;
void setup() {
  Serial.begin(9600);
  Serial.write(0xAA);
  Serial.write(0x37);
  delay(1000);
  Serial.write(0xAA);
  Serial.write(0x21);
  BLUETOOTH.begin(9600);
}
void loop() {
  while (Serial.available()) {
    com = Serial.read();
    switch (com) {
      case 0x11:
        Serial.println("1");
        break;
      case 0x12:
        Serial.println("2");
        break;
      case 0x13:
        Serial.println("3");
        break;
      case 0x14:
        Serial.println("4");
        break;
      case 0x15:
        Serial.println("5");
        break;
    }
  }
  while (BLUETOOTH.available()) {
    delay(3);
    char c = BLUETOOTH.read();
    readString += c;
  }
  if (readString.length() > 0) {
    if (readString == "|")
    {
    }
    readString = "";
  }

}

Please tell me what is wrong?

I don't see any errors. Compile and run the code. Remember to move all perpheral stuff to that one board.

1 Like

The BT code reads the BT but doesn't do anyting more with the data read.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.