Want to make "This' happen if this in on for "This" long.

Hello! In my attempts at learning more coding with the Arduino I am attempting to build a mini breadboard version of the red light/ green light system that water parks use for there slides. I'm very familiar with a PLC controlled system I have been involved with in the past and want to make it do exactly what it does. I got the basic of the codes down such as person on top of slide pushes button to turn light red and person on bottom of slide turns it green. However were I am stuck is when the safety features kick on. For example their system will chirp ( I will be using a piezo element) at you after 'X" amount of seconds the red light has been on indicating that the rider had plenty of time to exit and that the light should have been turned green by now but hasn't. Also if the top operator turns the light red and the bottom operator turns it green before its even possible for the rider to get down it will go into an alarm mode. In order to clear these alarms both operators must hold their buttons down for 3 sec to over ride the alarm and turn the light back to green. I quickly realized that the delay function would not work for complex logic. I tried the millis method with no luck either. Any help will be greatly appreciated!


My current code:

const int red = 2;                     //Red light
const int green = 3;                   // Green light
const int blue = 4;                    // Blue light
const int sender = 12;                 // Button at top of slide that turns on red light
const int reciver = 13;                // Button at bottom of slide that turns light green
const int buzzer = 6;                  // Piezo element for chirps 
int dispatch = 0;
int allClear = 0;

void setup()
 pinMode(2, OUTPUT);
 pinMode (3, OUTPUT);
 pinMode (4, OUTPUT);
 pinMode (12, INPUT);
 pinMode (13, INPUT);
 pinMode (6, OUTPUT);
 digitalWrite (2, HIGH);              //Red light on for delayed amount of time to ensure riders are not in slide
 delay (5000);
 digitalWrite (3, HIGH);
 digitalWrite (2, LOW);

void loop()
  dispatch = digitalRead (12);
  allClear = digitalRead (13);
   if ((dispatch == LOW) && (allClear == HIGH))          // If bottom operator pushes but then green light illuminates
    digitalWrite (2, LOW);
    digitalWrite (3, HIGH);

  if ((dispatch == HIGH)&& (allClear == LOW))              //If top operator pushes button red light turns on
   digitalWrite (3, LOW);
   digitalWrite (2, HIGH);


You need to save the time an event happens then compare this at a later time to see if the time passed is too big or small.

Event1 = millis.

Do things

If current millis -event1 > settime Do alarm procedure