I'm trying to connect my BLE to my phone but in Serial Monitor it repeats -1 for a bit, then also sends AT Sent like it restarts the program. Sometimes the numbers I input through my phone show up on the Serial Monitor. On my phone, it just pops up with AT+NAME? and AT+CHAR?
Serial Monitor - Screenshot - b38c2d831cf604c5b98ea7b2a6bd1751 - Gyazo
Hardware:
RX - Pin 9
TX - Pin 8
All help is greatly appreciated as I need this to be working.
#include "SoftwareSerial.h"
int rled = 4;
int gled = 5;
int bluetoothTx = 8;
int bluetoothRx = 9;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] = {4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
int i = 0;
int j = 0;
int toSend = (int)bluetooth.read(); //char
int password[] = {1, 2, 3, 4};
int input[] = {};
void setup() {
pinMode(rled, OUTPUT);
pinMode(gled, OUTPUT);
Serial.begin(9600);
bluetooth.begin(9600);
while(!Serial){}
Serial.write("AT Sent");
delay(500);
bluetooth.write("AT+NAME?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
}
void testAllBaudRates() {
for(j = 0; j < 8; j++) {
bluetooth.begin(baudrate[j]);
delay(100);
Serial.println("baud rate " + String(baudrate[j], DEC) + " i-> " + String(j, DEC));
//Serial.println("");
bluetooth.write("AT");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
Serial.println();
}
delay(100);
}
}
void loop() {
if(bluetooth.available()) {
int toSend = (int)bluetooth.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
if(j < 4) {
Serial.println(toSend);
input[j] = toSend;
j++;
}
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
digitalWrite(gled, LOW);
digitalWrite(rled, HIGH);
delay(1000);
digitalWrite(rled, LOW);
}
}
if (gotaMatch) {
digitalWrite(rled, LOW);
delay(1000);
digitalWrite(gled, HIGH);
delay(2000);
Serial.println("PASSWORD CORRECT");
}
}