0

i have one input and buzzeer output and reset pushbutton if the input is high the buzzer will be on and i want to turn off the buzzer by the pushbutton even if the input still high any suggestions to do this

in my code the buzzer is not working at all

int in1 =     2; //input
int in26 =   8;// for buzzer reset
int BUZZER_PIN = 52   ; // output for buzzer out 25
int buttonState1 =  0;  //state for input1
int buttonState26 =  0; //for buzzer reset
bool buzzer_is_on;  // state variable
void setup() {
  pinMode(in1 , INPUT);
  pinMode(in26 , INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW); // buzzer LOW

}

void loop() {

  buttonState1 = digitalRead(in1);
  buttonState26 = digitalRead(in26);


  bool input_rises = (buttonState1 == HIGH);
  bool reset_pressed = (buttonState26 == HIGH);


  if (buzzer_is_on) {
    if (reset_pressed) {
      digitalWrite(BUZZER_PIN, LOW);
      buzzer_is_on = false;
    }
   
  }
  else {  // buzzer is off
    if (input_rises) {
      digitalWrite(BUZZER_PIN, HIGH);
      buzzer_is_on = true;
    }
  }
}



6
  • put a layer of separation between the button press and the activation of the buzzer .... use a flag variable .... right now you have button pressed ==> buzz ... that is very inflexible .... instead do button pressed ==> raise flag ... flag raised ==> buzz .... then it is easy to do button2 pressed ==> clear flag .... if you decide to use timer timer expired ==> clear flag ... the flag can be raised or cleared by any number of actions – jsotola Mar 15 at 23:01
  • how to use flags – eng.hamza Mar 16 at 4:20
  • it is just any variable that can be used as true/false or hogh/low or 1/0 ... buttonState1 is one – jsotola Mar 16 at 4:56
  • can you help me with the code – eng.hamza Mar 18 at 20:56
1

I may have a personal bias for the concept of finite state machine. I find that some many problems become so easy when ween approached from this perspective...

Let me rephrase your requirements, just to make sure I understood correctly:

  • If the buzzer is OFF:
    • it should turn ON if the input rises (goes from LOW to HIGH)
  • If the buzzer is ON:
    • it should turn OFF if the input falls (goes from HIGH to LOW)
    • it should also turn OFF if the reset button is pressed

Or, as a state diagram:

state diagram

Or, as code:

bool buzzer_is_on;  // state variable

void loop() {
    bool input_rises = ...:
    bool input_falls = ...;
    bool reset_pressed = ...;
    if (buzzer_is_on) {
        if (input_falls || reset_pressed) {
            digitalWrite(BUZZER_PIN, LOW);
            buzzer_is_on = false;
        }
    } else {  // buzzer is off
        if (input_rises) {
            digitalWrite(BUZZER_PIN, HIGH);
            buzzer_is_on = true;
        }
    }
}

Now, the trick (which I leave as an exercise), is to properly detect when the input rises. This is not the same as the input being HIGH: if the user presses reset, the buzzer should turn OFF, but it should also stay OFF, even though the input remains HIGH. Hint: you not only have to read the input, you also have to remember the state you saw the previous time you read it. Or you can search “edge detection”.

Note that you do not need edge detection for the other transitions: you can turn the buzzer OFF when the input goes LOW, but you can also turn it OFF when the input is LOW. It should not make a difference.

9
  • i want the buzzer stay on even if the input goes off and reset it by the pushbutton – eng.hamza Mar 18 at 20:54
  • and the second condition is if the input is high the buzzer will be on and i can turn if off by the pushbutton – eng.hamza Mar 18 at 20:56
  • @eng.hamza: OK, sorry, but the description of the desired behavior was not very clear from your original question. If you can now more clearly formulate the conditions for the transitions, then you can adapt this code and make it work. The important thing I want to show you is not the code: it is the approach that goes from the specification (on what conditions should the transitions be made) to the code. – Edgar Bonet Mar 18 at 21:13
  • i appreciate your answer put im newbie in arduino and can't understand the steps you wrote above ill be glad if you can edit the original code in the post – eng.hamza Mar 19 at 13:13
  • @eng.hamza: Take the section I wrote in bullets (from “• If the buzzer is OFF”), fix it so that it matches your requirements, and add it to the question. For each possible state, you have to tell under what conditions the system switches to another state. Once this is done, converting to code is almost trivial, and I can do it for you. – Edgar Bonet Mar 19 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.