Problem with serial monitor

So i am creating this laser security thing where there's a laser module pointing towards an ldr and if you keep your hand in between, the laser light breaks and the buzzer is activated. Now i tried to add this feature where if you enter a password in the serial monitor, the laser module stops and the sensor stops sensing light (i changed the pin number so it can't sense). But the moment i open the serial monitor the laser module shuts for a second, and activates the buzzer cause there's no light for the ldr. So why is my laser module shutting down for a second when i open the serial monitor?? And also when the beeping and stuff is done, and then i try to input the password, it doesn't allow me to! It just keeps on printing "Access Denied"!!

The code btw:

int buzzer = 5;
int laser = 3;
int sensor = 7;
void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(laser, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(laser, HIGH);

  if(digitalRead(sensor) == HIGH){
    digitalWrite(buzzer, HIGH);
    delay(1500);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(1500);
    digitalWrite(buzzer, LOW);
    delay(1000);
    digitalWrite(buzzer, HIGH);
    delay(1500);
    digitalWrite(buzzer, LOW);
    delay(1000);
  }
  if(digitalRead(sensor) == LOW){
    digitalWrite(buzzer, LOW);
  }

  int password = Serial.read();

  if(password == 1234){
    Serial.println("Access Granted");
    sensor = 8;
  }
  if(password != 1234){
    Serial.println("Access Denied");
  }

  String oncommand = Serial.readString();

  if(oncommand == "ON" || oncommand == "on"){
    Serial.println("Laser Security Initiated");
    sensor = 7;
  }
}

Hi @mrcubez_96. Which Arduino board are you using?

it's a clone. Arduino UNO SMD

int password = Serial.read();

Read only one character from the serial port.
Following your example, entering "1234" only reads the '1' and saves its ascii code. Then password will have the value 48, so

if(password != 1234){

First try this sketch to learn how to obtain a password (say: A123) from Serial Monitor and then interface your Laser/Buzzer. Select Newline option in the Line ending box of Serial Monitor. Avoid using String data type which is no good for AVR.

char passWord[20];

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

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    byte m = Serial.readBytesUntil('\n', passWord, sizeof passWord - 1);
    passWord[m] = '\0';    //null-character
    Serial.println(passWord);
    if (strcmp(passWord, "A123") == 0)
    {
      digitalWrite(13, HIGH);   //Correct Password built-in L is ON;
    }
  }
}

Hello mrcubez_96

Welcome to the worldbest Arduino forum ever.

Take some time read and study the Serial() basics.

Have a nice day and enjoy coding in C++.

The UNO derivative board you are using has an "auto-reset" circuit that causes it to reset when you open Serial Monitor. I believe this reset is the cause of the behavior you described in the quote above.

There are several ways to prevent this reset:

The first of these is a bit inconvenient because you would need to remember to always close the port in the external terminal before uploading to the board (otherwise the upload will fail).

The second of these is fairly advanced and it is generally useful to have the reset when opening Serial Monitor so you can get debug output from the very start of the program execution.

The third is a bit inconvenient because you must remove the capacitor before each upload because the auto-reset circuit is necessary to activate the bootloader at the right time during the upload process.

For most projects, we don't find the auto-reset behavior of Serial Monitor to be disruptive. So now that you know why the reset occurs you might take the time to evaluate whether the reset is really a problem, and perhaps whether the project can be adjusted for the expectation of this reset.

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