How to turn of buzzer alarm, until it has been dark once more

I am programming an alarm clock, using a grove board, a light sensor, a buzzer and a button.

I want it to work this way:

If it is dark, then no sound
If it is light, then buzzer sounds.
If buzzer sounds, then the buzzer stops, if I press button.
By pressing the button 1 time, the buzzer stops until it has been dark 1 more time in the room.

I dont know how to code the last thing: By pressing the button 1 time, the buzzer stops until it has been dark 1 more time in the room.

I have put my code below:

int lightSensorPin = A0;
int analogValue = 0;
int Buzzer = 3;
int Button = 5;
bool Pressed;

void setup()
{
pinMode(Buzzer, OUTPUT);
pinMode(Button, INPUT);
Serial.begin(9600);

}

void loop()
{
Pressed = digitalRead(Button);
analogValue = analogRead(lightSensorPin);
Serial.println(" ");
Serial.println(Pressed);
Serial.println(analogValue);
delay (1000);

if(analogValue < 100)

{
digitalWrite(Buzzer, LOW);
Serial.println("Det er mørkt"); // if buzzer is low, monitor writes that it is dark
}

if(analogValue > 100)
{

digitalWrite(Buzzer, HIGH);
Serial.println("Det er lyst"); // if buzzer is high, monitor writes that it is light

}

if (Pressed)
{
digitalWrite(Buzzer, LOW);
Serial.println("Der er trykket sluk"); // if button is pressed, monitor writes that button has been pressed.

}

}

You need to keep track of what state your system is in, look up state machine. What that means in practice here is that you need a byte that you store one of three values in which represent:

  • Waiting for dark
  • Alarm
  • Waiting for light

Use a switch statement to pick code that handles each state. In your case, the code for each should be pretty simple.

Frankie777:
If it is dark, then no sound
If it is light, then buzzer sounds.
If buzzer sounds, then the buzzer stops, if I press button.
By pressing the button 1 time, the buzzer stops until it has been dark 1 more time in the room.

To me, that translates to:

When the light changes from DARK to LIGHT, turn on the buzzer.
When the button is pushed, turn off the buzzer.

Although you didn't specify it, I added:
When the light changes from LIGHT to DARK, turn off the buzzer.

const byte lightSensorAIPin = A0;
const byte BuzzerDOPin = 3;
const byte ButtonDIPin = 5;

boolean WasLight = true;

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  pinMode(BuzzerDOPin, OUTPUT);
  pinMode(ButtonDIPin, INPUT_PULLUP);  // Wire button between pin and Ground
}

void loop()
{
  if (analogRead(lightSensorAIPin) < 100)
  {
    // Now dark
    if (WasLight)
    {
      digitalWrite(BuzzerDOPin, LOW);  // Turn buzzer off
      Serial.println("Det er mørkt");
    }
    WasLight = false;
  }
  else
  {
    // Now light
    if (!WasLight)
    {
      digitalWrite(BuzzerDOPin, HIGH);  // Turn buzzer on
      Serial.println("Det er lyst");
    }
    WasLight = true;
  }

  if (digitalRead(ButtonDIPin) == LOW)  // INPUT_PULLUP = Active Low
  {
    digitalWrite(BuzzerDOPin, LOW);
    Serial.println("Der er trykket sluk");
  }
}

wildbill:
You need to keep track of what state your system is in, look up state machine. What that means in practice here is that you need a byte that you store one of three values in which represent:

  • Waiting for dark
  • Alarm
  • Waiting for light

I would rearrange them thus:

  • Waiting for dark
  • Waiting for light
  • Alarm

because that is the actual sequence in which they will be used.