Hi, I am currently making a project for school that involves the HM10 BLE. I am just trying to send over 4 digits over the bluetooth and then either if the 4 digit pin is correct or not, the RED/GREEN LED will turn on.
For some reason it looks like my input array is not getting inputted properly. I would use a for loop but I need to use it like this otherwise it wouldn't work for what I am trying to do.
void loop() {
while(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
int i = 0;
if(i < 4) {
if(toSend == 1) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 2) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 3) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 4) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 5) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 6) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 7) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 8) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 9) {
Serial.println(toSend);
input[i] = toSend;
i++;
}
}
if(input == password) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
}
if(input != password) {
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
}
}
Thank you,I fixed that and I added it to the end of my last if statement, though after upload still having the same issue. RED LED stays on even after the correct password was sent
#include "SoftwareSerial.h"
int rled = 4;
int gled = 5;
int bluetoothTx = 1;
int bluetoothRx = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] = {4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
int i = 0;
char toSend = (char)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());
}
delay(100);
Serial.println("");
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
Serial.println("");
}
void testAllBaudRates() {
for(int 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() {
while(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
if(i < 4) {
if(toSend == 1) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 2) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 3) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 4) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 5) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 6) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 7) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 8) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 9) {
Serial.println(toSend);
input[i] = toSend;
i++;
}
}
if(input == password) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
}
if(input != password) {
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
}
}
Delta_G:
if(input == password) {
You can't compare two arrays that way. The array name without any index is a pointer to the first element. So you are asking here if they are in the same place in memory which is obviously false.
You can use memcmp or you can loop through with a for loop comparing each element one at a time.
Would it be possible to make it if(input[0] == 1 && input[1] == 2 && input[2] == 3 && input[3] == 4) ?
I know it isn't the proper way to do it but I don't know this well enough to make a for loop to check both arrays.
Delta_G:
That will work if the password is 1234.
The for loop is easy peasy, what do you mean you can't:
Assuming it's a 4 digit password
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
break; // don't need to search anymore once we find one mismatch
}
}
if (gotaMatch) {
// password was right
}
even with your for loop the light still doesnt switch to green and red just stays on, any ideas?
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
break; // don't need to search anymore once we find one mismatch
}
}
//for (int i = 0; i < 4; i++){
//if (input[i] == password[i]){
if (gotaMatch) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
Serial.println("PASSWORD CORRECT");
}
// }
// }
I also removed if(gotaMatch) and added the for loop to test, but no luck.
#include "SoftwareSerial.h"
int rled = 4;
int gled = 5;
int bluetoothTx = 1;
int bluetoothRx = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] = {4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
int i = 0;
char toSend = (char)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");;
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
Serial.println("");
}
void testAllBaudRates() {
for(int 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() {
while(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
if(i < 4) {
if(toSend == 1) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 2) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 3) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 4) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 5) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 6) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 7) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 8) {
Serial.println(toSend);
input[i] = toSend;
i++;
} if (toSend == 9) {
Serial.println(toSend);
input[i] = toSend;
i++;
}
}
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
break; // don't need to search anymore once we find one mismatch
}
}
//for (int i = 0; i < 4; i++){
//if (input[i] == password[i]){
if (gotaMatch) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
Serial.println("PASSWORD CORRECT");
}
// }
// }
}
I fixed what you told me and here's my code:
#include "SoftwareSerial.h"
int rled = 4;
int gled = 5;
int bluetoothTx = 1;
int bluetoothRx = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] = {4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
int i = 0;
char toSend = (char)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");;
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
Serial.println("");
}
void testAllBaudRates() {
for(int 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(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
for(i = 0; i < 4; i++) {
toSend = input[i];
}
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
break; // don't need to search anymore once we find one mismatch
}
}
for (int i = 0; i < 4; i++){
if (input[i] == password[i]){
// if (gotaMatch) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
Serial.println("PASSWORD CORRECT");
}
}
// }
}
Still doesn't work though, I send 1, after it appears in the serial monitor, then 2, etc. But the red LED just stays on and green LED doesn't turn on.
I'm trying to make input equal to toSend. I edited my code again but from what I changed now, whenever I enter a number the whole thing resets and it just runs setup again.
for(int j = 0; j < 4; j++) {
input[j] = toSend;
}
The reason behind this statement is to get one by one digits into input, so I get all 4 that I need.
#include "SoftwareSerial.h"
int rled = 4;
int gled = 5;
int bluetoothTx = 1;
int bluetoothRx = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] = {4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
int i = 0;
char toSend = (char)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");;
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
Serial.println("");
}
void testAllBaudRates() {
for(int 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(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
for(int j = 0; j < 4; j++) {
input[j] = toSend;
}
boolean gotaMatch = true;
for (int i = 0; i < 4; i++){
if (input[i] != password[i]){
gotaMatch = false;
digitalWrite(rled, HIGH);
delay(3000);
digitalWrite(rled, LOW);
break; // don't need to search anymore once we find one mismatch
}
if (gotaMatch) {
digitalWrite(rled, LOW);
delay(500);
digitalWrite(gled, HIGH);
delay(3000);
digitalWrite(gled, LOW);
Serial.println("PASSWORD CORRECT");
}
}
}
Delta_G:
So when the first character arrives do you want to put it in all four spots or just the first one?
When the first character arrives I want to put it in just the first spot. So input[0]
Delta_G:
Oh hey I spot a big bug. Why do you have the software serial running on the hardware serial pins? And you're using hardware serial at the same time. BIG problem. You can't use 0 and 1 for your Bluetooth unless you're using hardware serial on it. Pick two different pins.
I switched to 8 and 9 pins but now I am getting nothing through except the setup commands AT+POWE3 and AT+CHAR?. The numbers I input through my phone aren't being sent.
So do you mean to remove all the if (toSend == 1) { and just have it like:
if(i < 4) {
Serial.println(toSend);
input = toSend;
- i++;*
- }*
I'll double check the bluetooth connections.
Thank you for everything, really want this done.
With this code I have now, it just repeats going red for a few seconds, turning off, turning on green for about 2 seconds and then turning on red again. Thought it was working at first when I entered it through my phone but I noticed when I unplugged the arduino and put it back in it would just continue doing this loop.
#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;
char toSend = (char)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);
}
void testAllBaudRates() {
for(int 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(Serial.available()) {
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
if(i < 4) {
Serial.println(toSend);
input[i] = toSend;
i++;
}
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");
}
}
}
Delta_G:
Check your braces on that for loop at the end. You've got the code to react to having a match inside the for loop.
Okay I fixed that, now it just stays on red again but in the Serial Monitor it just shows a backward question mark popping up. None of my numbers that I input.
When I call the variables in my software code, do I reverse the pins? Because RX -> TX and TX -> RX but right now I have the TX in 8 and RX in 9 and also the same thing in my code.
When I had it connected to pins 0, 1 it was flipped but since I switched to pins that don't have the RX/TX labelled on them do I switch it in the software?
Delta_G:
int bluetoothTx = 8;
int bluetoothRx = 9;
You wrote this in your code and you're telling me you can't tell which one is Rx and which one is Tx? The Tx pin on the bluetooth should connect to pin 9. The Rx pin on the bluetooth should connect to pin 8.
I was asking whether I should switch them or not.
Anyways still the same issue, now my numbers that I send don't show up on my phone or the Serial Monitor. On the serial monitor I see this: Screenshot - f22484672a71fe9605b8955a4f93e1c5 - Gyazo
#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;
char toSend = (char)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);
}
void testAllBaudRates() {
for(int 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(Serial.available()) {
char toSend = (char)Serial.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");
}
}