trigger flashlight with button

Hello

I have project to do.

I need to design button which trigger flashlight with delay and sound (countdown).

After pressing the button I need about 5 sec delay with sound countdown and then flashlight to go.

I was looking in to some other projects, but didn’t find anything close.

I manage to trigger flashlight but struggle with countdown “beep”.

Here is code I copied from different projects.

const int ledPin = 2;

const int buttonPin = 4;

int buttonState = 0;

//buzzer

#include “pitches.h” //add note library

//notes in the melody
int melody={NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};

//note durations. 4=quarter note / 8=eighth note
int noteDurations={4, 8, 8, 4, 4, 4, 4, 4};

void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);
delay(500);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

Serial.println(“LED ON +++++++”);

}
else {

digitalWrite(ledPin, LOW);

Serial.println(“LED OFF -------”);

}

//read the input pin
int buttonState = digitalRead(buttonPin);

//if the button is pressed
if (buttonState == 1){

//iterate over the notes of the melody
for (int thisNote=0; thisNote <8; thisNote++){

//to calculate the note duration, take one second. Divided by the note type
int noteDuration = 1000 / noteDurations [thisNote];
tone(8, melody [thisNote], noteDuration);

//to distinguish the notes, set a minimum time between them
//the note’s duration +30% seems to work well
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);

//stop the tone playing
noTone(8);
}
}

}

Attaching photo of design Led diode is test for flashlight.

Will appreciate any help.

Thanks

"Here is code I copied from different projects." Not to be rude, but try to write your own code instead of blindly copy something and expect the arduino forum to fix it for you :)

Danois90: "Here is code I copied from different projects." Not to be rude, but try to write your own code instead of blindly copy something and expect the arduino forum to fix it for you :)

Give that member some karma!

What do you think happens during this:

delay(500);

Nothing. Nada, Zip. Zilch.

And, yet, you do that on EVERY pass through loop(). Why?

Instead, once the switch has become pressed (see the damned state change detection example), you should be making some noise, and then twiddling your thumbs until it is time ti turn the pin on.