Buzzer turns on after 5 seconds of the LED being ingited

Heyy everyone, I'm kinda new to the world of Arduino and I'm currently wokring on a project in which I want a buzzer to turn on after 5 seconds of the LED being ingited. To give you an idea of my project, I want to make a hat with an LED on top. The LED gets ignited if I'm raising my hand (or an object) up in the range of an SharpIR distance sensor. The longer you hold up raise up your hand, the brighter shines the LED. I already got this part in my code covered. After 5 seconds of the LED glowing, I want the buzzer to turn on and play a melody.

Feel free to ingore the German notes in my code ^^'

tippe oder füge den Code hier ein

#include <SharpIR.h>  //Bibliothek für Sharp sensor

int ir = A0;     // Pin auf welchem der Sensor liegt
long model = 1080;


const int LED_PIN = 9; // LED Pin auf Pin 9, PWM Pins sind auf 3, 5, 6, 9, 10 und 11.
const int Buzzer = 4; //Buzzer Pin auf Pin 4

const int WAITTIME = 200; // Steigerung der Helligkeit bis 10 Sekunden
const int STEP = 5;

int dis = 0;


SharpIR SharpIR(ir, model);

#include "pitches.h"


int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {

  Serial.begin(9600);
  pinMode( LED_PIN, OUTPUT );
  pinMode(Buzzer, OUTPUT);
}

  for (int thisNote = 0; thisNote < 8; thisNote++) {

    
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }

void loop() {
  measuredistance();
  delay(20);
}



int power = 0;


void measuredistance() {

  dis = SharpIR.distance(); // Distanz (cm) zum gemessenen Objekt

  Serial.println(dis);

  bool triggered = (dis > 10 && dis < 50);


  if (triggered) {
 

    int buzzertime = power / 10;

    digitalWrite(Buzzer, HIGH);
    delay(buzzertime);
    digitalWrite(Buzzer, LOW);
    delay(25);

    power = power + 1;

  } else {


    power = power - 10;

  }

  power = constrain(power, 0, 255);


  analogWrite( LED_PIN, power );



  //THIS IS JUST A TRY 
  
  //  if (dis > 10 && dis < 80 ) {
  //    while ( dis > 10 && dis < 80 )
  //    {
  //      ledbrightness();
  //      dis = SharpIR.distance();
  //    }
  //  }
  //  digitalWrite(LED_PIN, LOW);
  //  digitalWrite(Buzzer, LOW);
}





// Erhöhung der Helligkeit bis zu 10 Sekunden

void ledbrightness() {
  int i;

  i = 0;
  while ( i <= 255 && dis > 10 && dis < 80) {   // Helligkeit wird weiter gesteigert
    analogWrite( LED_PIN, i );
    delay( WAITTIME );
    i = i + STEP;
    dis = SharpIR.distance();
  }
  if ( i > 255  && dis > 10 && dis < 80) {
    ledbuzzer();       //Wenn ein Objekt länger als 10 Sekunden hochgehalten wird
  }


}


// Buzzer wird nach 10 Sekunden aktiviert
void ledbuzzer() {
  if ( dis > 10 && dis < 80) {
    while ( dis > 10 && dis < 80) {
      digitalWrite(LED_PIN, HIGH);
      digitalWrite(Buzzer, HIGH);
      dis = SharpIR.distance();
    }

  }
}

a state machine approach would be a relevant code structure

something like this (typed here, fully untested)

enum t_state : byte {INACTIVE, HANDRAISED, GETTING_TIRED} state = INACTIVE;
unsigned long actionStartTime;
const unsigned long longTimeDuration = 5000ul;

bool actionDetected() {
  bool action = false;
  // ===> TO DO
  // ...
  return action; // return true if action is detected
}

void manageState() {
  switch (state) {
    case INACTIVE:
      if (actionDetected()) {
        actionStartTime = millis();
        state = HANDRAISED;
      }
      break;

    case HANDRAISED:
      if (actionDetected())
        if (millis() - actionStartTime >= longTimeDuration) state = GETTING_TIRED;
        else state = INACTIVE;
      break;

    case GETTING_TIRED:
      if (! actionDetected()) state = INACTIVE;
      break;
  }
}

// do something with the LED if needed (===> TO DO)
void manageLed() {
  static t_state oldState = INACTIVE; // can be used to detect a state change
  switch (state) {
    case INACTIVE:      break;
    case HANDRAISED:    break;
    case GETTING_TIRED: break;
  }
  oldState = state;
}

// do something with the buzzer if needed (===> TO DO)
void manageSound() {
  static t_state oldState = INACTIVE; // can be used to detect a state change
  switch (state) {
    case INACTIVE:      break;
    case HANDRAISED:    break;
    case GETTING_TIRED: break;
  }
  oldState = state;
}

void setup() {
  // ....
}

void loop() {
  manageState();
  manageLed();
  manageSound();
  // here you can do other stuff if not too long
}

Hey, thx for the feedback! I‘ll definitely try it out and telll you how it worked out!
At this point I tried out many other options but wasn’t able to make it work. I definitely have to invest more time to fully understand how the programming in Arduino works… . :sweat_smile: