Button to stop passive buzzer (or reset the arduino)

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.

void setup() {
  pinMode(4, OUTPUT); // Speaker pin
  pinMode(7, INPUT_PULLUP); // Button connected to pin 7
  pinMode(8, INPUT_PULLUP); // Button connected to pin 8
}

void loop() {
  static boolean alarmPlaying = false;

  if (digitalRead(7) == LOW && !alarmPlaying) {
    alarmPlaying = true;
    while (digitalRead(8) == HIGH) {
      tone(4, 3150);
      delay(500);
      noTone(4);
      delay(500);
    }
    alarmPlaying = false;
  }
}

your code kinda works if you hold de stop button for more than one second because your while loop

only evaluates the state of the button every second due to the presence of delay() inside the while loop

test your code here:

if you want immediate reaction, you need a small state machine for example and using millis

For extra information and examples look at

(instead of flashing the leds you would turn the sound on or off)


if you can't make it and want a ready made solution

try this

nonBlockingSoundPlayStop - Wokwi ESP32, STM32, Arduino Simulator

the code:

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;
  }
}

1 Like

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

Why don’t you put the AI stuff aside and invest a bit in acquiring skills ?

3 Likes

I tried but it didnt work.

.................. :rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl:

Do you know how to learn?

It's like a control loop:

  1. read
  2. understanding
  3. doing

and then start again at 1.

1 Like

Fantastic! @paulpaulson's relentless promotion of the IPO model in a new context!

a7

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.