So I want my "button 8" to stop the alarm or the buzzer or reset the arduino so the alarm stops.
My coding skills are bad so I don't know what to do here, because other codes have some songs or much longer non-understandable code, I would be happy for my project to finally work.
const byte playPin = 7;
const byte stopPin = 8;
const byte buzzerPin = 4;
enum state {SILENT, BEEPING_ON, BEEPING_OFF} state = SILENT;
unsigned long chrono;
const unsigned long soundOnDuration = 500; // ms
const unsigned long soundOffDuration = 500; // ms
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(playPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
}
void loop() {
switch (state) {
case SILENT:
// we are silent, if we press the play button, then start playing the sound
if (digitalRead(playPin) == LOW) {
tone(buzzerPin, 3150); // start the sound
chrono = millis(); // note the start time
state = BEEPING_ON;
}
break;
case BEEPING_ON:
// the beeping is on, check if we need to stop
if (digitalRead(stopPin) == LOW) {
noTone(buzzerPin);
state = SILENT;
} else // check if ON duration has elapsed
if (millis() - chrono >= soundOnDuration) {
noTone(buzzerPin);
chrono = millis(); // note the start time
state = BEEPING_OFF;
}
break;
case BEEPING_OFF:
// the beeping is off check if we need to stop
if (digitalRead(stopPin) == LOW) {
noTone(buzzerPin);
state = SILENT;
} else // check if OFF duration has elapsed
if (millis() - chrono >= soundOffDuration) {
tone(buzzerPin, 3150); // start the sound again
chrono = millis(); // note the start time
state = BEEPING_ON;
}
break;
}
}
Ahh, thank you, but how about this version of code
int buzzer = 4;//the pin of the active buzzer
void setup()
{
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
unsigned char i;
while(1)
{
//output an frequency
for(i=0;i<80;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}
Because I tried bing ai and chatgpt in previous one but now I ran out of messages o them