What am I doing wrong? pt 2

I recently made a post about an error message I got while a was trying to build an alarm-system with a passcode. Thanks to you guys I got all the error-messages sorted out but it still doesn't work... now the alarm is on all the time (kinds of, the lamp is flickering and is acting weird and the buzzer doesn't make a sound at all) and nothing seems to work, I've checked all connections and they seem fine and I have no clue what's wrong.

#include <Password.h>

#include <Keypad.h>



float sinVal;
unsigned int toneVal;
int brightness;
boolean alarmState = 1;
Password password = Password( "4789" );
const byte ROWS = 4;
const byte COLS = 4;
const byte redPin = 9;
const byte buzzerPin = 10;

char keys[ROWS][COLS] = {
  {
    '1', '2', '3', 'A',
  }
  ,
  {
    '4', '5', '6', 'B',
  }
  ,
  {
    '7', '8', '9', 'C',
  }
  ,
  {
    '*', '0', '#', 'D',
  }
};

byte rowPins[ROWS] = {

  4, 3, 2, 1
};

byte colPins[COLS] = {

  13, 12, 11, 10
};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );




void setup() {
  pinMode (8, OUTPUT);
  pinMode (9, OUTPUT);
  pinMode (0, INPUT);
  boolean alarmState = 0;
  keypad.setDebounceTime(50);
  keypad.addEventListener(keypadEvent);
  
}

void loop() {
  noTone(8);

  if (alarmState == 1) {
    if (digitalRead(0) == HIGH) {

      alarm();
    }

    else {
      keypad.getKey();
    }
  }
}

  void keypadEvent(KeypadEvent eKey) {

    switch (keypad.getState()) {

      case PRESSED:
        switch (eKey) {
          case '#':
            checkPassword();
            break;


            break;
          default:
            password.append(eKey);
        }

    }

  }

  void checkPassword() {
    if (password.evaluate()) {
      if (alarmState == 0) {
        alarmState = 1;
      }
        else {
          alarmState = 0;
        }
    }
      else {
        return;
      }
    


  }


  void alarm() {

    for (int y = 0; y < 2; y++) {

      for (int x = 0; x < 180; x++) {
        sinVal = (sin(x * (3.1412 / 180)));

        toneVal = 2000 + (int(sinVal * 1000));
        brightness = int(sinVal * 5);
        tone(8, toneVal);
        analogWrite(9, brightness);
        delay(2);
      }
    }
  }

Any help is appreciated!

You can't use a switch that reads HIGH when pressed and low when not pressed on pin0, at least not easily. First, no matter what it will prevent sketches from uploading while connected, and you would also need a surprisingly low value external pulldown resistor to overpower the series resistor between pin 0 and the TX pin of the on-board serial adapter - something like 100 ohms.

Those issues do not apply if the button is wires to be LOW when pressed and HIGH when not pressed - however serial will not be usable (and anything sent over the USB serial will be interpreted as button presses).

If possible, when using the Uno, Mega, or other Arduino's without native USB and with an on-board serial adapter you should avoid using pins 0 and 1 for anything other than communiction with the built-in serial adapter.

Oh, I'm not using a button, I'm using a motion detector

Ok, I changed so nothing was on pin 0 or 1 and it seems to work much better but still the password doesn't work, Now the alarm only goes off when I activate the motion detector but the passcode doesn't deactivate it. you got any idea why that is? (btw the buzzer is working now, accidently but the ground cable in the wrong pin)

I'm a total noob at coding and I just started a couple of weeks ago so I don't really understand what you are trying to say,. The code is supposed to say: if both alarmState is 1 the motion detector reads high it should activate the alarm, if not it should read the keypad. I'm probably doing that whole thing wrong and it would be really nice if you could come up with a way that works. I still have a lot to learn about the code-language and the Arduino.

I notice you don't reset the password, ever. Character just get appended. If the password is right once it will never be right again. You should clear the password at the bottom of checkPassword() so it it ready to start over.

If the alarm is sounding you don't process keyboard input. That means to turn the alarm off you have to wait outside the range of the motion sensor until that turns off and then enter the password without triggering the motion sensor. It shouldn't hurt to process the keyboard input even when the alarm is sounding. You will have to type slowly since loop() only repeats wen the noise and light cycle has happened twice.

The "if" statement could be easily rewritten to do what you want, but calling keypad.getKey() and throwing away the result still does not make sense.

This may a stupid question, but:

How do you reset the password?

Actually I have no idea what so ever what you mean. Reset the password? why wouldn't I be able to use it again? I don't really understand how this would help me with the problem I have currently either, I can't use the password even once, it never works.