Skip to main content
Arduino

Return to Revisions

1 of 4

Serial comunication with HC-05 - Arduino UNO

I am new to the site and this is my first question. I'm going to try to describe my issue the best I can.

I got the following code for my project which consists basically of getting a car to have 2 driving mode, automatic and manual.

As far as I can tell, I got it all working except it's not. I got an HC-05 connected to my L293D motor shield (attached to an Arduino UNO), as well as 2 dc motors, an ultrasonic sensor HC-SR04 and a LED. The circuit part is not the problem, I’ve managed to connect it all and it's working (I tested the obstacle avoiding part by itself, before adding the mode selection code, so I know the motors and sensor do their job). I should also mention that I’m sending the information through the BT from an app I made in APP Inventor. The app is working fine, and I do receive the correct data in the Serial Monitor.

What’s puzzling me is, why can’t I get to assign the Serial.read(); value to my response variable (resp). I’m going to try to explain this a bit further. In my serial monitor I get the intended chars from the app buttons, however, my "resp" variable doesn’t seem to be getting any value from Serial.read(); for some reason, because when it gets to the switch statement, even though the char is printed on the monitor the cases don’t start, for instance, and I might be completely off here, once I get ‘1’ on the monitor I should also start getting the distance measurement printed in the monitor and it’s not.

For what I gather, I’m pretty sure the issue is between the Serial.write() and the Serial.println()/.read(); since the serial.write() seems to be taking priority over all others, since setting this lines as comments don’t affect the writing in the monitor:

if(Serial.available()>0){
//resp = Serial.read();
//Serial.println(resp);

I’m out of ideas so, any help or guidance towards the right direction will be greatly appreciated.

//Incluir librerias para controlar motor shield y sensor ultrasonico.
#include <AFMotor.h>
#include <NewPing.h>
#include <SoftwareSerial.h>
//Definir constantes y variables.
#define TRIG_P A4 
#define ECHO_P A5
#define MAX_DIST 300
#define MAX_Vel 200
int led = A0;
int speedSet;
int iteraciones = 5;
float distancia, duracion;
char resp;
//Inicializar objetos del sensor y motores.
NewPing sonar(TRIG_P, ECHO_P, MAX_DIST); 
AF_DCMotor motorD(2); 
AF_DCMotor motorI(1);
SoftwareSerial BTSerial(A1, A2); // RX | TX
void setup() {
 Serial.begin(9600);
 pinMode(led, OUTPUT);
 BTSerial.begin(9600);
}
void loop() {
 //Lee información del BT y la escribe en el monitor serial.
 if (BTSerial.available()){
 Serial.write(BTSerial.read());
 Serial.write('\n');
 } 
 delay(24); //Tiempo maximo que tarda en recibir una medición.
 /*Calcular la distancia frente al sensor utilizando la duración de la señal, 
 la cual mide ida y vuelva por lo cual se divide entre 2 
 y se multiplica por la velocidad del sonido (343 m/s).
 */
 
 duracion = sonar.ping_median(iteraciones);
 distancia = (duracion/2)*0.0343;
 if(Serial.available()>0){ 
 resp = Serial.read();
 Serial.println(resp);
 
 switch(resp){
 case '1':
 delay(24); //Tiempo maximo que tarda en recibir una medición.
 /*Calcular la distancia frente al sensor utilizando la duración de la señal, 
 la cual mide ida y vuelva por lo cual se divide entre 2 
 y se multiplica por la velocidad del sonido (343 m/s).
 */
 
 duracion = sonar.ping_median(iteraciones);
 distancia = (duracion/2)*0.0343;
 
 Serial.print("Distancia: ");
 Serial.print(distancia);
 Serial.println(" cm"); 
 
 if(distancia!=0 && distancia<=20){
 digitalWrite(led,HIGH);
 ALTO();
 delay(100);
 REVERSA();
 delay(400);
 IZQUIERDA();
 delay(100);
 }else 
 digitalWrite(led,LOW);
 ADELANTE();
 break;
 
 case '2':
 ALTO();
 
 switch(resp){
 case 'A':
 ADELANTE();
 break;
 case 'R':
 REVERSA();
 break;
 case 'I':
 IZQUIERDA();
 break;
 case 'D':
 DERECHA();
 break;
 case 'O':
 ALTO();
 break;
 }
 break;
 } 
 }
 }
 // Inician metodos que definen las acciones del carro.
 
void ADELANTE(){
 motorD.run(FORWARD); 
 motorI.run(FORWARD);
 for (speedSet = 0; speedSet < MAX_Vel; speedSet +=2){
 motorD.setSpeed(speedSet + 20);
 motorI.setSpeed(speedSet - 20);
 }
 }
void REVERSA(){
 motorD.run(BACKWARD); 
 motorI.run(BACKWARD); 
 for (speedSet = 0; speedSet < MAX_Vel; speedSet +=2){
 motorD.setSpeed(speedSet + 20);
 motorI.setSpeed(speedSet - 20);
 }
 } 
void DERECHA() {
 motorD.run(RELEASE);
 motorI.run(FORWARD); 
 delay(300);
 motorD.run(FORWARD); 
 motorI.run(FORWARD); 
 } 
 
void IZQUIERDA() {
 motorD.run(FORWARD); 
 motorI.run(RELEASE); 
 delay(300);
 motorD.run(FORWARD); 
 motorI.run(FORWARD);
 } 
void ALTO(){
 motorD.run(RELEASE); 
 motorI.run(RELEASE);
 } 

Here is an image of what I get on my serial monitor. Serial Monitor with BT readings.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /