user74131: I want to stop the sound of a buzzer after I push the push button. I tried a code, but when the button is not pushed, the buzzer continue to make sound. How can I fixed it?
const int pin_contact= 6;
const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  pinMode(pin_contact, INPUT);
  Serial.begin(9600);
}

void loop() {
  tone(buzzer, 200);
 
  int stare_contact = digitalRead(pin_contact);
  if (stare_contact == HIGH) {
    noTone(buzzer); 
  }
}


user68988: Try this
int LEDState = 1;
int SwitchPin = 6;
int LEDPIN = 9;

void setup()
{
  pinMode(LEDPIN,OUTPUT);
  pinMode(SwitchPin,INPUT);
  digitalWrite(LEDPIN,LEDState);
}

void loop()
{

  int SwitchState = digitalRead(SwitchPin);

  if(LEDState==1 && SwitchState==HIGH){
   LEDState = 0; 
  }

  digitalWrite(LEDPIN,LEDState);
    delay(10); 
}


user74141: Is your buzzer active low? try this code below.
it will toggle buzzer pin output.
const int buzzerPin = 9;
const int buttonPin = 6; 
int buzzerState = 1;
int buttonState;
int buttonTemp = 0;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() { 
  buttonState = digitalRead(buttonPin); 
  digitalWrite(buzzerPin,buzzerState);
  if (buttonState == HIGH && buttonTemp == 0){
    Serial.print("Buzzer toggle to : ");
    if (buzzerState) {
        Serial.println("LOW");
        buzzerState = 0;
    } else {
        Serial.println("HIGH");
        buzzerState = 1;
    }
    buttonTemp = 1;
  } else if (buttonState == LOW) {
    buttonTemp = 0;
  }
}


74131: <p>I want to stop the sound of a buzzer after I push the push button. I tried a code, but when the button is not pushed, the buzzer continue to make sound. How can I fixed it?</p>
<pre class="lang-cpp prettyprint-override"><code>const int pin_contact= 6;
const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  pinMode(pin_contact, INPUT);
  Serial.begin(9600);
}

void loop() {
  tone(buzzer, 200);
 
  int stare_contact = digitalRead(pin_contact);
  if (stare_contact == HIGH) {
    noTone(buzzer); 
  }
}
</code></pre>
 68988: <p>Try this</p>
<pre><code>int LEDState = 1;
int SwitchPin = 6;
int LEDPIN = 9;

void setup()
{
  pinMode(LEDPIN,OUTPUT);
  pinMode(SwitchPin,INPUT);
  digitalWrite(LEDPIN,LEDState);
}

void loop()
{

  int SwitchState = digitalRead(SwitchPin);

  if(LEDState==1 &amp;&amp; SwitchState==HIGH){
   LEDState = 0; 
  }

  digitalWrite(LEDPIN,LEDState);
    delay(10); 
}
</code></pre>
 74141: <p>Is your buzzer active low? try this code below.
it will toggle buzzer pin output.</p>
<pre><code>const int buzzerPin = 9;
const int buttonPin = 6; 
int buzzerState = 1;
int buttonState;
int buttonTemp = 0;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() { 
  buttonState = digitalRead(buttonPin); 
  digitalWrite(buzzerPin,buzzerState);
  if (buttonState == HIGH &amp;&amp; buttonTemp == 0){
    Serial.print(&quot;Buzzer toggle to : &quot;);
    if (buzzerState) {
        Serial.println(&quot;LOW&quot;);
        buzzerState = 0;
    } else {
        Serial.println(&quot;HIGH&quot;);
        buzzerState = 1;
    }
    buttonTemp = 1;
  } else if (buttonState == LOW) {
    buttonTemp = 0;
  }
}
</code></pre>
