Trouble Coding Buzzer to Stop - NEWBIE

The purpose of this code is for a green LED (led1) to be HIGH when the program starts and for a red LED (led2) to be HIGH when a button is pressed. When either LED is HIGH, the other should be LOW.

When the Red LED is HIGH, I require a two second delay, followed by a buzzer sound for 1 second.

I would then like the RED to stay HIGH and the Buzzer to be LOW (or the equivalent in audio terms) when I complete the next lot of code.

My code

const int led1= 12;
const int led2 = 13;
const int button = 2;
const int buzzer = 7;


int previous = LOW;
int initial = HIGH;
long time =0;
long debounce = 200;

int buttonState = 0;
int buzzerState = 0;
int lastBuzzerState =0;

void setup() {
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (button, INPUT);
pinMode (buzzer, OUTPUT);
Serial.begin (9600);




}

void loop() {
buttonState = digitalRead(button);

if (buttonState == HIGH && previous == LOW && millis() - time > debounce) {
    if (initial == HIGH)
      initial = LOW;
    else
      initial = HIGH;

    time = millis();    
  }

  digitalWrite(led1, initial);
  digitalWrite (led2, !initial);

  previous = buttonState;


buzzerState = digitalRead (led2);

if (buzzerState == HIGH){

Serial.print ("Stand back shock coming in...");

delay (2000);

tone (buzzer, 100);

delay (1000);

noTone (buzzer);

lastBuzzerState = buzzerState;
}

}

I have two issues,

a) The buzzer currently loops, I have tried creating a new function, but the buzzer would not sound at all. I also created a new variable LastBuzzerState and set buzzerState to equal that after the buzzer sounded.

b) Re pressing the button does not revert the program back to the Green LED HIGH and RED LED LOW, how would I go about this?

Many Thanks

buzzerState = digitalRead (led2);

That code would make as much sense it it looked like this:

pizza = digitalRead (moldyBanana);

Why the hell does buzzerState get a value from reading an LED? Why are you reading from an OUTPUT, anyway? You KNOW what state you set the output pins to.

I really despair sometimes with this forum, I've posted now for two days and I do not know why I bother.

As the topic states, I am new to this.

Which I am sure at one point you were, your sarcastic tone may please yourself, but it really does nothing for me mate.

For clarity, I want the buzzer to make a tone only once when the Red LED is first HIGH. I am unsure how to do this.

I am unsure how to do this.

bool buzzingAlreadyDone = false;
void loop()
{
   digitalWrite(redLEDPin, HIGH);
   if(!buzzingAlreadyDone)
   {
       doSomeBuzzing();
       buzzingAlreadyDone = true;
   }
}

Now, you may want to put all the code I put in loop() in some conditional block, but how you decide that the red LED should be lit is irrelevant to making the buzzer buzz only once.

You may want to have some condition where buzzingAlreadyDone gets set back to false.

You need a variable to keep track of if the buzzer has already sounded, or keep good track on the red led time.
If (red led on time is >2 second and red led on time is <4 second) buzzer on.

Your information is also missing vital information. You want red led to be active while button is pressed, what about if button is released after 2.1 second? Do you immediately want buzzer to be quiet and green led to come on? Using delay in the code you can release the button and press it again without detecting a new press.