Can't troubleshoot this problem.

My code is to have two sensors. one connected to a door and one is a bypass switch. when the door is open and in 5 seconds the bypass button is pressed or door gets closed,it loops without having the alarm off.

#define Sensor 12
#define bypass 11
#define RedLED 7
#define Buzzer 4

int SensorState;
int bypassState;
int LoopedSuccess;

void setup() {
  // put your setup code here, to run once:
  pinMode(Sensor,INPUT);
  pinMode(bypass,INPUT);
  pinMode(RedLED,OUTPUT);
  pinMode(Buzzer,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("I am stating 1");
  SensorState = digitalRead(Sensor);
  bypassState = digitalRead(bypassState);

  Serial.println();
  Serial.println();
  Serial.print("My Sensor is  ");
  Serial.println(SensorState);
  Serial.print("My Bypass State is   ");
  Serial.println(bypassState);
  Serial.print("My Looped Count is   ");
  Serial.println(LoopedSuccess);
  Serial.println();
  Serial.println();
  Serial.println("Test 1 =");
  if (LoopedSuccess < 50){
    Buzzing();
    Serial.println("Passed");
  }
  Serial.println("Test 2 =");
  if (SensorState == 1){
    Serial.println("Passed");
    return;
  }
  Serial.println("Test 3 =");
  if (SensorState == 0 && bypassState == 1){
    Serial.println("Passed");
    return;
  }
  Serial.println("Test 4 =");
  if (SensorState == 0 && bypassState == 0){ 
    Serial.println("Passed");
    digitalWrite(RedLED,HIGH);
    Serial.println("Passed 1");
    digitalWrite(RedLED,LOW);
    SensorState = digitalRead(Sensor);
    bypassState = digitalRead(bypassState);
    Serial.println();
  Serial.println();
  Serial.print("My Sensor is  ");
  Serial.println(SensorState);
  Serial.print("My Bypass State is   ");
  Serial.println(bypassState);
  Serial.print("My Looped Count is   ");
  Serial.println(LoopedSuccess);
  Serial.println();
  Serial.println();
    Serial.println("Passed 2");
    if (bypassState == HIGH){
      Serial.println("bypassed?");
      return;
      Serial.println("bypassed NO!?");
      }
      Serial.println("Passed 3");
    if (SensorState == 1){
      return;
    }
    Serial.println("Passed 4");
    Serial.println("I wasn't stopped");
    delay(100);
    Serial.println("Passed 5");
    LoopedSuccess++;
  }
    Buzzing();
    Serial.println("I FINISHED?");
 
}

void Buzzing(){
  if (LoopedSuccess == 40){
    Serial.println("OMG it's BUZZING!!!!!");
     digitalWrite(RedLED,HIGH);
      tone(Buzzer,100);
      
      SensorState = digitalRead(Sensor);
      bypassState = digitalRead(bypassState);
    Serial.println("Testing in Buzz line");
      if (SensorState == 1 || bypassState == 1){
        LoopedSuccess = 0; 
        digitalWrite(RedLED,LOW);
      }
      
    }
}

It printed

My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1


My Sensor is  0
My Bypass State is   0
My Looped Count is   0


Test 1 =
Passed
Test 2 =
Test 3 =
Test 4 =
Passed
Passed 1


My Sensor is  0
My Bypass State is   1
My Looped Count is   0


Passed 2
bypassed?
I am stating 1

I checked the connection and did another prototype again and it still did not work…

ImJustChew:
My code is to have two sensors. one connected to a door and one is a bypass switch. when the door is open and in 5 seconds the bypass button is pressed or door gets closed,it loops without having the alarm off.

It looks like your counter "LoopedSuccess" is supposed to mean something but you never get to the "Passed 5" message so you never increment it. I suspect this is caused by:

    Serial.println("Passed 3");
    if (SensorState == 1) {
      return;
    }

Your description of exactly what you want the sketch to do is very sparse. Could you describe in detail what you want the sketch to do?

Could you describe in detail what you want the sketch to do?

And post a schematic showing how the switches are wired. "Incorrectly" is the impression I get.

So you describe your problem as

My code is to have two sensors. one connected to a door and one is a bypass switch. when the door is open and in 5 seconds the bypass button is pressed or door gets closed,it loops without having the alarm off.

I would be tempted to described that with a state based system

I had done a while back a very similar system - have a look may be that will help you get ideas on how to architect your code.

I replaced my SMS & web notification by just serial printing, so this will compile on any Arduino and can be demoed with the use of only two jumper wires.

One Jumper in pin D12 for the door, One in Pin D11 for the bypass.

to simulate door opening, connect Jumper wire from D12 to GND
to simulate pressing the bypass, connect Jumper wire from D11 to GND

It’s using the built in LED (the one on pin 13 on your UNO) for demo purpose.

if you have a buzzer, then correctly connect the buzzer to D4 and ground.

While ticking before the alarm you will get a multiple tone buzz and then a constant frequency on/off buzz when Alarm is triggered (if you did not close the door or use the bypass button)

have fun & hope this helps

/* ********************************************************************* */
/*                                                                       */
/*                   DEMO INTRUSION ALERT SYSTEM WITH BYPASS             */
/*                                                                       */
/* ********************************************************************* */

const byte doorSensorPin = 12;    // the door detector, with a PULL UP
const byte bypassButtonPin = 11;  // the alarm deactivate button, with a PULL UP
const byte  RedLightPin = LED_BUILTIN;
const byte hornAlarmPin = 4;

byte sensorState, previousSensorState, bypassState, previousBypassState;

enum {doorClosed, doorOpenClockTicking, doorOpenAlarmON, doorOpenClockBypass} systemState;

unsigned long int doorOpenTime;
const unsigned long int alarmTriggerDelay = 5000UL; // Alarm will trigger after alarmTriggerDelay ms

boolean soundON;
unsigned long int startSoundTime;
const unsigned long int alarmTempo = 150UL; // LED and sound will be ON/OFF for that duration if Alarm is ON

const unsigned int alarmFrequency = 4500U; // that's the sound when Alarm is on

const unsigned int alarmTickFrequency[] = {250U, 500U}; // that's the sounds during clock Ticking. enter as many frequencies in array as suitable
byte currentAlarmTickFrequency; // where we are in the array

// print our status once in a while as a heartbeat for remote notification or log system for example
unsigned long int startHeartBeatTime;
const unsigned long int heartBeatTimeDelay = 60000UL; // heartbeat 1 per minute


/* ********************************************************************* */
// check what is the current status of our system and update status accordingly
/* ********************************************************************* */

void checkSystemstate()
{
  // check current state of the door
  sensorState = digitalRead(doorSensorPin);

  if (sensorState != previousSensorState) {
    if ((systemState == doorClosed) && (sensorState == LOW)) {
      // we just opened the door. record opening time and start ticking
      doorOpenTime = millis();
      systemState = doorOpenClockTicking;
      displaySystemState(false); // new event, update our status display / notification system
    } else if (sensorState == HIGH) {
      systemState = doorClosed;
      displaySystemState(false); // new event, update our status display / notification system
    }
    previousSensorState = sensorState;
    delay(15); // debounce
  }

  // check current state of the bypass when we start the Arduino
  bypassState = digitalRead(bypassButtonPin);
  if (bypassState != previousBypassState) {
    if (( systemState == doorOpenClockTicking) || (systemState == doorOpenAlarmON)) {
      systemState = doorOpenClockBypass;
      displaySystemState(false); // new event, update our status display / notification system
    }
    previousBypassState = bypassState;
    delay(15); // debounce
  }
}

/* ********************************************************************* */
// call at every loop, does the right thing if Alarm needs to bip
/* ********************************************************************* */

void updateAlarm()
{
  if ((systemState == doorOpenAlarmON) || (systemState == doorOpenClockTicking)) {
    // is it time to flip sound/light status?
    if (millis() - startSoundTime >= alarmTempo) {
      if (soundON) {
        noTone(hornAlarmPin);
        digitalWrite(RedLightPin, LOW);
        soundON = false;
      } else {
        digitalWrite(RedLightPin, HIGH);
        if (systemState == doorOpenAlarmON) {
          tone(hornAlarmPin, alarmFrequency);  // better if we could set volume of alarm HIGH
        } else {
          tone(hornAlarmPin, alarmTickFrequency[currentAlarmTickFrequency]); // better if we could set volume of tick LOW
          currentAlarmTickFrequency = (currentAlarmTickFrequency + 1) % (sizeof(alarmTickFrequency) / sizeof(alarmTickFrequency[0]));
        }
        soundON = true;
      }
      startSoundTime = millis();
    }
  } else {
    if (soundON) {
      noTone(hornAlarmPin);
      digitalWrite(RedLightPin, LOW);
      currentAlarmTickFrequency = 0;
      soundON = false;
    }
  }
}


/* ********************************************************************* */
// here we just display the information
// but could send a SMS, trigger a web action etc
// if heartbeat is true, then it's just the heartbeat trigger otherwise it's an event
/* ********************************************************************* */

void displaySystemState(boolean heartbeat)
{
  Serial.print(heartbeat ? '<' : '[');

  switch (systemState) {
    case doorClosed:
      Serial.print("doorClosed");
      break;
    case doorOpenClockTicking:
      Serial.print("doorOpenClockTicking");
      break;
    case  doorOpenAlarmON:
      Serial.print("doorOpenAlarmON");
      break;
    case doorOpenClockBypass:
      Serial.print("doorOpenClockBypass");
      break;
    default:
      Serial.print("Code error...");
      break;
  }

  Serial.println(heartbeat ? '>' : ']');
}

/* ********************************************************************* */
void setup() {
  pinMode(doorSensorPin, INPUT_PULLUP);   // Using switch, HIGH is Door closed, LOW is Door open
  pinMode(bypassButtonPin, INPUT_PULLUP); // Using switch, HIGH is bypass Button not pressed
  pinMode(RedLightPin, OUTPUT);           // The blinking red Alarm light relay
  pinMode(hornAlarmPin, OUTPUT);          // The trigger pin for the horn (very loud buzzer, tone will work)

  previousSensorState = HIGH; // door closed
  previousBypassState = HIGH; // not pressed
  systemState = doorClosed;

  soundON = false;
  currentAlarmTickFrequency = 0;
  noTone(hornAlarmPin);

  digitalWrite(RedLightPin, LOW);

  startHeartBeatTime = millis();
  Serial.begin(115200);
  displaySystemState(true); // this is the heartbeat, not a event.
}


/* ********************************************************************* */
void loop() {
  checkSystemstate();

  if (systemState == doorOpenClockTicking) {
    if (millis() - doorOpenTime >= alarmTriggerDelay) {
      // TRIGGER ALARM !!!
      startSoundTime = 0; // will trigger sound right away
      systemState = doorOpenAlarmON;
      displaySystemState(false); // new event, update our status display / notification system
    }
  }
  
  updateAlarm();

  if (millis() - startHeartBeatTime >= heartBeatTimeDelay) {
    // update our status display
    displaySystemState(true); // this is the heartbeat, not a event.
    startHeartBeatTime = millis(); // will trigger sound right away
  }
}