Language confusion?

Hello,

Currently I am working on writing the code in order to create a Bluetooth lock for a school project, since i am in charge of the tech while my partner works on the physical, and I am having trouble with the language as I am new to coding.

In order to make it work, I am using a WiFly model Rn-Xv 171, which I have connected to the arduino via analog inputs 4 and 5, and I have a red LED to represent the mechanism of the lock connected to digital output 8. (picture of setup below)

I attached a photo of the code below, and my problem is that I cannot get the code to compare the guessed password and the real password correctly. As you can see I converted both variables, the stored "real" password and the "guessed" password, to String. Initially they were both Int, but it returned "password incorrect", even though the gpassword and the password looked exactly the same. I then switched to String, but to no avail. Any help would be greatly appreciated.

You attached nothing

In any case, please post your code here rather than a picture of it

Please see my attached comments.

DKWatson:
Please see my attached comments.

What attached comments would that be ?

I suspect a rookie mistake with string comparison - comparing strings in C is a little less intuitive than the higher level languages many of us are more used to.

Post the code (using code tags, the </> button in top left of forum editor)

You should never post pictures of text; post the text, so that we can easily read it (particularly on mobile devices, and particularly with the weird attachment handling on this forum, images are much harder to read) - especially when you are asking people you don’t know to help you with your homework for free.

Here's the code (and yes, I am an extreme noob)

#include "SoftwareSerial.h"
SoftwareSerial WiFlySerial (A4, A5);
int lock = 8;
String password = String(1234);
char incomingBytes[80];
int lf = 10;
String gpassword;

void setup() {
pinMode(8, OUTPUT);
WiFlySerial.begin(9600);
WiFlySerial.print("Input ");
WiFlySerial.print(password);
WiFlySerial.println(" to unlock lock");
while (!WiFlySerial);
}

void loop() {
if (WiFlySerial.available()) {
WiFlySerial.readBytesUntil(lf, incomingBytes, 80);
String gpassword = String(incomingBytes);
WiFlySerial.println(gpassword);
WiFlySerial.println(password);
if (gpassword.equals(password)) {
WiFlySerial.println("Password Correct");
//inserting digital.write(8, HIGH) here
}
if (!gpassword.equals(password)) {
WiFlySerial.println("Password Incorrect");
}
}
}

Try printing the length of the password and the guessed password. You may find that the guessed password is one character longer.
This would be because your serial monitor is set up to use CR+LF as a line terminator.

They both return as 6 characters long.

TomHudson:
They both return as 6 characters long.

Please tell me you didn't do "sizeof(String);"

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

When using Cstrings you must use strcmp() to compare values rather than ==

...R