Arduino Project for Intro to Engineering question PLEASE HELP

I am trying to program my Arduino to replicate opening a door with one button and closing it with the other. When the door is opened using the first button a timer counts back from five at five seconds the light and speaker will blink and beep until the second button is pressed.

I wanted to make it so that if I press the second button before five seconds the light and speaker will not beep but I cannot figure out how to do it.

Thank you in advance to anyone who can give me a hand with this, I am not a programmer so this is all very confusing to me. Here is the code I have now, It does everything I want it to do besides not beep and blink if the second button is pressed before five seconds.

/*Dementia ward door buzzer-- When ButtonPin1 (representing opening a door) is pressed a timer counts down from 5.

  • After Five seconds a LED will blink and speaker(wired to the LED) will buzz until door is closed (by pressing Button Pin 2)
    Written by ********* 11/18/19*/
    byte LEDpin = 10;
    byte ButtonPin1 = 11;
    byte ButtonPin2 = 12;
    byte DisplayNumbers = {B11111100, B01100000, B11011010, B11110010,
    B01100110, B10110110, B10111110, B11100000,
    B11111110, B11110110};
    // The following is an array of indices to be used with the DisplayNumbers array
    byte DisplayNumberIndex = {5, 4, 3, 2, 1};
    void setup()
    {
    pinMode(LEDpin, OUTPUT);
    pinMode(ButtonPin1, INPUT);
    pinMode(ButtonPin2, INPUT);
    DDRD = B11111110; /* DDRD is the direction register for
    the Arduino’s PORT D (Digital Pins 0 - 7). DDRD = B11111110
    tells Arduino to set pin 0 for input, pins 1 through 7 for output */
    }
    void loop()
    {
    if (digitalRead(ButtonPin1) == HIGH) //Start flashing LED if 1st button is pressed
    {
    for (byte index = 0; index <= 4; index++)
    {
    PORTD = DisplayNumbers[DisplayNumberIndex[index]]; //Use the indexed array to choose the digit
    delay(1000);
    }
    PORTD = B00000000;// turn off seven segment display
    while(HIGH)
    {
    digitalWrite(LEDpin, HIGH);
    delay(250);
    if (digitalRead(ButtonPin2) == HIGH) //Checks if 2nd button is pressed.
    {
    digitalWrite(LEDpin, LOW); //If 2nd button is pressed, turn off LED
    break; //and break out of the while loop
    }
    digitalWrite(LEDpin, LOW);
    delay(250);
    if (digitalRead(ButtonPin2) == HIGH) //Checks if 2nd button is pressed.
    {
    digitalWrite(LEDpin, LOW); //If 2nd button is pressed, turn off LED
    break; //and break out of the while loop
    }
    }
    }
    }

State machines are great ways to approach problems like this. (Having said that, this compiles but I don't know if it works... :slight_smile: )

/*Dementia ward door buzzer-- When ButtonPin1 (representing opening a door) is pressed a timer counts down from 5.
 * After Five seconds a LED will blink and speaker(wired to the LED) will buzz until door is closed (by pressing Button Pin 2)
Written by ********* 11/18/19*/

const byte LEDpin = 10;
const byte ButtonPin1 = 11;
const byte ButtonPin2 = 12;

#define ALARM_FLASH_TIME    250ul
#define CDOWN_TIME          1000ul
#define BUTTON_1            0
#define BUTTON_2            1
#define BTN_PRESSED_LEVEL   HIGH

#define DOOR_CLOSED         0
#define DOOR_OPEN           1
#define DOOR_ALARM          2

typedef struct sButtonInfo
{
    byte        nowState;
    byte        lastState;
    const byte  pinButton;
};

sButtonInfo ButtonInfo[] = 
{
    {
        .nowState = 0,
        .lastState = 0,
        .pinButton = ButtonPin1
    },
    {
        .nowState = 0,
        .lastState = 0,
        .pinButton = ButtonPin2        
    }
};

//    __a
// f |g_|b
// e |__|c
//    d
byte DisplayNumbers[] = 
{
    //0..9
    //   ABCDEFG-
        B11111100, B01100000, B11011010, B11110010,
        B01100110, B10110110, B10111110, B11100000,
        B11111110, B11110110
};

byte count;
bool bLED;

void setup()
{
    pinMode( LEDpin, OUTPUT );
    pinMode( ButtonPin1, INPUT_PULLUP );
    pinMode( ButtonPin2, INPUT_PULLUP );
    //
    ButtonInfo[BUTTON_1].lastState = digitalRead( ButtonInfo[BUTTON_1].pinButton );
    ButtonInfo[BUTTON_2].lastState = digitalRead( ButtonInfo[BUTTON_2].pinButton );
    
    DDRD = B11111110; /* DDRD is the direction register for
    the Arduino's PORT D (Digital Pins 0 - 7). DDRD = B11111110
    tells Arduino to set pin 0 for input, pins 1 through 7 for output */
    
}//setup

void loop( void )
{
    static unsigned long
        timeDoor = 0;
    static byte
        stateDoor = DOOR_CLOSED;
    unsigned long
        timeNow;

    timeNow = millis();
    switch( stateDoor )
    {
        case    DOOR_CLOSED:
            if( ReadButton( BUTTON_1 ) == true )
            {
                stateDoor = DOOR_OPEN;
                count = 5;
                PORTD = DisplayNumbers[count];
                timeDoor = timeNow;
                    
            }//if
        
        break;

        case    DOOR_OPEN:
            if( ReadButton( BUTTON_2 ) == true )
            {
                PORTD = 0;
                stateDoor = DOOR_CLOSED;
                
            }//if
            else
            {
                if( (timeNow - timeDoor) >= CDOWN_TIME )
                {
                    timeDoor = timeNow;
                    count--;
                    if( count == 0 )
                    {
                        PORTD = 0;
                        digitalWrite( LEDpin, HIGH );
                        bLED = true;
                        stateDoor = DOOR_ALARM;
                        
                    }//if
                    else
                        PORTD = DisplayNumbers[count];
                    
                }//if
                
            }//else
                    
        break;

        case    DOOR_ALARM:
            if( ReadButton( BUTTON_2 ) == true )
            {
                digitalWrite( LEDpin, LOW );
                stateDoor = DOOR_ALARM;
                
            }//if
            else
            {
                if( (timeNow - timeDoor) >= ALARM_FLASH_TIME )
                {
                    timeDoor = timeNow;
                    bLED ^= true;        
                    digitalWrite( LEDpin, (bLED==true)?HIGH:LOW );
                
                }//if

            }//else
                        
        break;
        
    }//switch
    
}//loop

bool ReadButton( byte buttonNum )
{
    ButtonInfo[buttonNum].nowState = digitalRead( ButtonInfo[buttonNum].pinButton );
    if( ButtonInfo[buttonNum].nowState != ButtonInfo[buttonNum].lastState )
    {
        ButtonInfo[buttonNum].lastState = ButtonInfo[buttonNum].nowState;
        if( ButtonInfo[buttonNum].nowState == BTN_PRESSED_LEVEL )
            return true;
        else
            return false;
    }//if
    
}//ReadButton

OP, please use CODE TAGS

Ty2012:
I wanted to make it so that if I press the second button before five seconds the light and speaker will not beep but I cannot figure out how to do it.

If you want a responsive program you should not use delay() as the Arduino is blocked until the delay() completes. And for the same reason don't use WHILE or FOR unless they complete very quickly - a small number of microseconds. Generally it is better to use IF and allow loop() to do the repetition - which is also facilitated by the State Machine concept recommended by @Blackfin.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R