Arduino robot remote control buttons and voice

I have a problem.I want to creeate a a project with arduino,to be more specific i want to creeate a 2 wheels robot,and I want to control the robot from my android phone.I already made the app that i need in android studio. So i tested the app with arduino incorporated led 13. I tested first butons,and after the voice control with diferent sketch,all works fine.

#include <SoftwareSerial.h>
String voice;
#define bTx 11
#define bRx 10
#define led 13
SoftwareSerial bluetooth(bTx, bRx);

void setup() {
pinMode(led, OUTPUT);
bluetooth.begin(9600);
}

void loop() {
if (bluetooth.available()) {
char comand = (char)bluetooth.read();
switch (comanda) {
case ‘W’:
digitalWrite(led, HIGH);
break;
case ‘S’:
digitalWrite(led, LOW);
break;
}
}

}
This is the code for remote control with buttons.

#include <SoftwareSerial.h>
#define led 13
//TX, RX respetively
SoftwareSerial BT(11, 10);
String voice;
void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(13, OUTPUT);

}

void loop() {
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
if (c == ‘#’) {break;} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
}
if (voice.length() > 0) {
Serial.println(voice);

if(voice == “go”)
{
digitalWrite(13, HIGH);
}

else if(voice == “stop”)
{
digitalWrite(13, LOW);
}

}
voice="";
}
And this is for voice remote control test.

How can i put all in one sketch? I tried diferent ways and i didn’t figure it out. I mean i want to use just one sketch for both remote control(buttons and voice control).When i upload it in diferent sketch it work. I dont want to use two sketchs one for butons,and one for voice. My android app include buttons and voice control

What about putting the code you have in both your loops in separate functions? So, basically create a function for using the buttons, and one for voice command, and then call them in your loop.

SMM2:
What about putting the code you have in both your loops in separate functions? So, basically create a function for using the buttons, and one for voice command, and then call them in your loop.

I have the code in diferent sketch Buttons.ino and voice.ino.When i want to put all in one sketch All.ino it doesen't work. I get the code from Buttons.ino and i put in void loop from voice.ino.

SMM2:
What about putting the code you have in both your loops in separate functions? So, basically create a function for using the buttons, and one for voice command, and then call them in your loop.

Can you give me an exemple?

When i want to put all in one sketch All.ino it doesen't work.

You didn't post that code.
You didn't define what it actually does.
You didn't define what you expected it to do.

You didn't really expect help, did you?

PaulS:
You didn't post that code.
You didn't define what it actually does.
You didn't define what you expected it to do.

You didn't really expect help, did you?

I posted the code. With this code i tested my android app witch have buttons functionality(button up,down,left,right) and voice capabilities. When i press button UP the led 13 is on(android app send W char to arduino),button down the led is of(android app send S char to arduino). When i use voice voice capabilities and i say "go"led 13 is on,when i say "stop" the led 13 is off.
My question was how i can put all the code in one sketch,because if you noticed i used 2 diferent sketch. You didn't even read the code :slight_smile:

You didn't even read the code

I looked at the code you posted. THEN, you said:

When i want to put all in one sketch All.ino it doesen't work.

THAT was what I was commenting on.

You need to improve your reading comprehension skills before you go throwing stones.

PaulS:
I looked at the code you posted. THEN, you said:THAT was what I was commenting on.

You need to improve your reading comprehension skills before you go throwing stones.

Look i have two sketch Button.ino and Voice.ino.When i try them one by one it work. I tried to make just 1 sketch from code. I made 2 function one for Button one for Voice and i call them in loop. But i still have some problems because i need to press the button like two or three times to light up the led(same for turn off the led),and sometimes at voice function bytes are lost:for example instead of sending ''stop" it only send"op" or “top”,I saw this in serial monitor.

Thank you for your response.

Here is the code

#include <SoftwareSerial.h>
#define bTx 4
#define bRx 2
#define led 13
SoftwareSerial bluetooth(bTx, bRx);

void setup() {
pinMode(led, OUTPUT);
bluetooth.begin(9600);
Serial.begin(9600);

}

void loop() {
voice();
Buttons();

}

void Buttons() {
if (bluetooth.available()) {
char comanda = (char)bluetooth.read();
switch (comanda) {
case ‘W’:
digitalWrite(led, HIGH);
break;
case ‘S’:
digitalWrite(led, LOW);
break;
}

}

}

void voice() {
String voice;
while (bluetooth.available()) { //Check if there is an available byte to read
delay(10);//Delay added to make thing stable
char c = bluetooth.read(); //Conduct a serial read
if (c == ‘#’) {
break; //Exit the loop when the # is detected after the word
}
voice = voice + c;; //Shorthand for voice = voice + c
}
if (voice.length() > 0) {
Serial.println(voice);

if (voice == “start”)
{
digitalWrite(13, HIGH);
}

else if (voice == “stop”)
{
digitalWrite(13, LOW);
}

    voice = voice + c;; //Shorthand for voice = voice + c

What a useless comment.

You are reading data from the bluetooth device in two places. DO NOT DO THAT. Read all the data in one place, and decide what to do based on what you received.

What does the serial data look like when you speak to the phone? How will you distinguish that data from the single character sent when pressing a button on the phone?

PaulS:

    voice = voice + c;; //Shorthand for voice = voice + c

What a useless comment.

You are reading data from the bluetooth device in two places. DO NOT DO THAT. Read all the data in one place, and decide what to do based on what you received.

And how i do that? Can you give me exemple ? The voice function work except sometimes when bytes are lost. And buttons function not working properly,i need to press two,three times to turn on led.

And how i do that?

Isn’t that obvious? Put ALL the code for reading from the bluetooth device in ONE function, most likely loop().

You have not answered the question about what the voice data looks like, so I’m done trying to help.

Serial monitor from voice

This is how serial monitor look like for voice.Thank you for your time for helping me.

What does your code look like, after doing all the serial reading in one place?

In one case, you send some text followed by a # symbol. In the other case, you send some text that has NO end of packet marker. How is the Arduino supposed to know when a packet is to have an end marker, and when it is not?

I know the code is a mess but i think i finally made it. I adjusted the android app to send just strings instead of chars.In the previous code i used char for buttons,now i use string for both buttons and voice.

#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BT(4, 2);//TX,RX

String data;



void setup() {
  BT.begin(9600);
  Serial.begin(9600);
  pinMode(13, OUTPUT);

}

void loop() {
  while (BT.available()) {
    delay(10);
    char c = BT.read(); 
    data=data+c;;
  }
  if (data.length() > 0) {
    Serial.println(data);
    if (data == "start")
    {
      digitalWrite(13, HIGH);
    }

    else if (data == "stop")
    {
      digitalWrite(led, LOW);
    }

  }
  data="";
  

}

And now when i say start or when i press the button serial monitor show me start and led is up.Same for stop.

I know the code is a mess

Good, because I was going to point that out.

You took code that properly received packets with end markers, and stopped sending end markers. That was the wrong approach.

You are now hoping that an entire packet, and NOTHING ELSE, will arrive between times that you read the bluetooth data. Can you see why that is a bad idea?

You incorporated a useless delay(), in the hopes that that would deal with the fact that serial communication is slow. That is NOT the proper way to do that. End of packet markers ARE.

You KNOW that you are not going to send novels, so using the String class is not necessary. You CAN use a char array of some fixed length.

I can't image why "start" means turn an LED on. I would think that "on" would mean that. YMMV.

So i need to use end of packet markers and instead sending strings i must send just chars? And how i use end of packet markers?

#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BT(4, 2);//TX,RX



void setup() {
  BT.begin(9600);
  Serial.begin(9600);
  pinMode(13, OUTPUT);

}

void loop() {
  while (BT.available()) {
    char c = BT.read(); 
    if(char=='#') break;
  }
  if (data.length() > 0) {
    Serial.println(data);
    if (data =='O')
    {
      digitalWrite(13, HIGH);
    }

    else if (data =='S')
    {
      digitalWrite(led, LOW);
    }
    
  }
  data="";
  

}

Or i can use “<” for begining of package and “>” for end of package.

#define StartPackage='<'
#define StopPackage='>'
bool start;
bool stop;

void setup(){
    BT.begin(9600);
    Serial.begin(9600);
    pinMode(13, OUTPUT);
}

void loop() 
{
  while (BT.available() > 0)
  {
    char c = BT.read();


    if (c == StartPackage)
    {  
      start = true;
      stop = false; 
    }

    else if (c ==EndPackage)
    {
       Stop= true;
      break;
    }

Can you give me an exemple?

Can you give me an exemple?

Robin2 can:

PaulS:
Robin2 can:
http://forum.arduino.cc/index.php?topic=396450.0

Thank you.