LED will not turn on. Please send help

Hello there :). We have no idea why our code isn't working. Please send help it is for a school project that is due soon. If you need the schematic please comment and I will upload. The goal of this code is that if the approved message is typed the LED will turn on. But unfortunatly, it is not. Please help us.

Code:

int accessLight = 2;
String password = "1234";



void setup() {
  Serial.begin(9600);
  pinMode (accessLight, OUTPUT);


}

void loop() {

if (accessLight !=HIGH) {
Serial.print (" Type Password ");

String input = Serial.readString();
delay (1000);
if (input = password) {
  Serial.println("APPROVED. WELCOME.");
  digitalWrite(accessLight, HIGH);
}
else if (input != password) {
  Serial.println("INCORRECT. TRY AGAIN.");
  digitalWrite(accessLight, LOW);
}
}
}

This isn’t the way to read a pin. Maybe you want to look into digitalRead()?

This is an assignment = not a comparison ==

If you fix those, Try using the built-in LED_BUILTIN to see if the code works versus a wiring/hardware problem with your LED.

accessLight indicates a pin number, not the status of that pin.
You must use digitalRead(), e.g.

byte ledStatus = digitalRead(accessLight);

and then

if (ledStatus != HIGH) {
2 Likes

Posting your schematic will not hurt especially if @MaximoEsfuerzo solution does not work.

1 Like

You think digitalRead() won't work to read a pin?
:interrobang:

What I indicated is not a "solution", it is a correction to a misspelled code, which is not the same thing.

In addition to all that's gone before:

Serial.readString is likely not going to do what you want here. It does not return when the Enter key is pressed. It returns when it times out (which, by default, is set to 1000 mSec). If you don't type fast enough, you'll only get part of your input. If you do type fast enough, you'll likely get the character representing the Enter key if you press that out of habit at the end of typing your password.

Slightly better would be to use Serial.setTimeout to give yourself much more time between keystrokes, and to use Serial.readStringUntil to read characters up to the Enter key being pressed. (I leave it to you to discover what that character is.)

Much better would be to come up with your own input routine. Something that assembled keystroke characters into a String w/o timing out, and finishing only when it detected the character from the Enter key.

Reading @Robin2's Serial Input Basics tutorial is highly recommended.

1 Like

Always include a theory of operation, the sketch and a wiring diagram.

Thank you all so much for your recommendations. We finally got to work! Our grade thanks you all tremendously. Have a great day everyone!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.