Go Down

Topic: Arduino robot remote control buttons and voice (Read 1 time) previous topic - next topic

Sirverian

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

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.
"Success is not built on success. It's built on failure. It's built on frustration. Sometimes its built on catastrophe."

Sirverian

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.

Sirverian

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?

PaulS

Quote
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?
The art of getting good answers lies in asking good questions.

Sirverian

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 :)



PaulS

Quote
You didn't even read the code
I looked at the code you posted. THEN, you said:
Quote
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.
The art of getting good answers lies in asking good questions.

Sirverian

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);
    }

PaulS

Code: [Select]
    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.
The art of getting good answers lies in asking good questions.

PaulS

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?
The art of getting good answers lies in asking good questions.

Sirverian

Code: [Select]
    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.

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

Sirverian

Serial monitor from voice

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

PaulS

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?
The art of getting good answers lies in asking good questions.

Sirverian

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.

Code: [Select]
#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.

Go Up