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;
int pinBluetoothTX=10;
int pinBluetoothRX=11;
Servo servoMotor1;
Servo servoMotor2;
char rxChar;
SoftwareSerial bluetooth(pinBluetoothTX, pinBluetoothRX);
void setup(){
Serial.begin(9600);
pinMode (4,INPUT);//Fin de carrera 1
pinMode (2,INPUT);//Fin de carrera 2
pinMode(pinBluetoothRX, INPUT);
pinMode(pinBluetoothTX, OUTPUT);
mover=90; //0 Abrir 180 Cerrar 60-120 Velocidades
mover2=150;//motor Cerradura
servoMotor1.attach(8);//Motor Pin8
servoMotor2.attach(9);//Motor Pin9
bluetooth.begin(9600);
}
void loop() {
int lectura;
int lectura2;
servoMotor1.write(mover);
servoMotor2.write(mover2);
lectura=digitalRead(4);//fin de carrera
lectura2=digitalRead(2);//fin de carrera
bluetooth.available() ; //Espera un carácter
char rxChar = bluetooth.read(); //Lee carácter y lo borra del buffer
Serial.println(rxChar); //Imprime código ASCCI de caracter
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
}
}
Here is my test code for a HC05 Bluetooth module and the Bluetooth serial terminal. Adjust the pins as necessary for your setup. Use this simple code to make sure the Bluetooth module is connected and working.
//Sends from serial monitor to the Bluetooth device app
// receives from the Bluetooth device app
#include <SoftwareSerial.h>
SoftwareSerial BTserial(4, 7); // pin 4 to BT TX, pin 7 to BT RX
// through voltage divider
void setup()
{
Serial.begin(9600);
Serial.println("Bluetooth test program");
BTserial.begin(9600);
BTserial.println("Bluetooth test program");
}
void loop()
{
if (Serial.available())
{
BTserial.write(Serial.read());
}
if (BTserial.available())
{
Serial.write(BTserial.read());
}
}
bluetooth.available() ; //Espera un carácter
char rxChar = bluetooth.read(); //Lee carácter y lo borra del buffer
Serial.println(rxChar);
You are not doing serial input correctly. Your bluetooth.available() is doing nothing. I would suggest that you have a look at the serial input basics tutorial to see how to use the avaiable() function and how to receive serial data. Example 1 - Receiving single characters does just what you need.
Calls for help like this one are becoming more frequent. Someone builds a phone/Arduino project based on a tutorial or kit. It doesn't work and they come to the Arduino or MIT AI forum asking "what's wrong?".
All the elements to make such a project work are well described in many places.
The discussion we are in right now encompasses pretty well all the key points in troubleshooting an app/Arduino problem.
I would like to suggest that anyone wanting to know why their MIT App Inventor app isn't working correctly with their Arduino kit are referred to this discussion. They should only call for help after they have checked off everything in this discussion and still can't get things right.
What do others think about that? Do you not get tired of asking the same questions, making the same suggestions?
Sounds like a good suggestion. But it´s not that easy to find this exact topic when looking for it in Google.
Maybe it should be better if someone try to do a specific topic that everybody references, just as what has been doing with this "Serial Imput Basics" that @groundFungus mentioned. I´ve seen many many references to this topic everytime that someone asks about dealing with received messages or exchanging information through Serial Monitor.
Agree, the topic name is not the best description.
I feel all the components of an Arduino/app project are well described in many places. I wonder if a simple list of the components is all that's needed, at least in the short term.