0

I have a problem with my Bluetooth module on my Arduino, I am trying to do a door that opens with a button in an application in the phone but when I press the button in the app the door doesn't move.

#include <SoftwareSerial.h>
#include <Servo.h>
int mover;
int mover2;
Servo servoMotor1;
Servo servoMotor2;
char rxChar;
SoftwareSerial BTserial(10, 11);
void setup(){
  Serial.begin(9600);
  Serial.println("Bluetooth test program");
pinMode (4,INPUT);//Fin de carrera 1
pinMode (2,INPUT);//Fin de carrera 2
mover=90; //0 Abrir 180 Cerrar 60-120 Velocidades
mover2=150;//motor Cerradura
servoMotor1.attach(8);//Motor Pin8
servoMotor2.attach(9);//Motor Pin9
BTserial.begin(9600);
BTserial.println("Bluetooth test program");
}
void loop() {
 int lectura; 
 int lectura2;
  servoMotor1.write(mover);
  servoMotor2.write(mover2);
  lectura=digitalRead(4);//fin de carrera
  lectura2=digitalRead(2);//fin de carrera
if (Serial.available())
   {
      BTserial.write(Serial.read());
      
   }
   if (BTserial.available())
   {
      Serial.write(BTserial.read());
      char rxChar = BTserial.read();
   }
   if(rxChar=='a'){
   Serial.print("60 Grados");
   mover=60;
   }
   if(rxChar=='b'){
   Serial.print("120 Grados");
   mover=120;
   }
  if (lectura==LOW and mover==120){
    mover=90;//motor frena
    mover2=30;//Motor Cerradura Cerrado
  }
  if (lectura2==LOW and mover==60){
    mover=90;//motor frena
    
  }
  }

when I press the button the console returns "⸮"

  • Wrong baud? You are seining a non printable character? And why are you reading 2 characters from `BTserial`, if you only guaranteed that you have at least 1 character to read? – gre_gor Nov 17 '21 at 22:08

1 Answers1

1

This line is wrong: char rxChar = BTserial.read(); because you are declaring a local rxChar and assigning the read character to it. But, because it is local (inside { } ) its value will be lost. And because you also have a global rxChar (line 7: char rxChar;) that is unassigned, you will be using that value everywhere else.

Also, as mentioned you are reading from BTSerial twice.

  • You can fix the problem by simply removing "char" in the line mentioned, so rxChar will refer to the global declaration, and
  • perform only one read: rxChar = BTserial.read(); Serial.write(rxChar);
mmixLinus
  • 1,646
  • 2
  • 11
  • 16