How do I add 2 buttons with example sketch?

Hello!
How can I have 2 different buttons make a single buzzer do one beep every 500ms and an other beep every 1000ms (using the arduino example sketch twice)?
I have this for now, but it doesn't work correctly.

int buttonState = 0;         // variable for reading the pushbutton status
int armedswitchPin = 1;     // the number of the pushbutton pin
int buzzerPin = 10;
int testswitchPin = 2; 

void setup() {
  pinMode(buzzerPin, OUTPUT);      
  pinMode(armedswitchPin, INPUT);  
  pinMode(testswitchPin, INPUT);  
}

void loop() {
  // put your main code here, to run repeatedly: 
  // read the state of the pushbutton value:
  buttonState = digitalRead(armedswitchPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(buzzerPin, LOW); 
  delay(300);               // wait for a second
  digitalWrite(buzzerPin, HIGH);    // turn the LED off by making the voltage LOW
  delay(300);               // wait for a second

  } 
  else {
    // turn LED off:
    digitalWrite(buzzerPin, LOW); 
  }
  
    // read the state of the pushbutton value:
  buttonState = digitalRead(testswitchPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(buzzerPin, LOW); 
  delay(1000);               // wait for a second
  digitalWrite(buzzerPin, HIGH);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

  } 
  else {
    // turn LED off:
    digitalWrite(buzzerPin, LOW); 
  }
  
  
}

Thanks!

What does your code do? and how does that differ from what you want?

You need to edit your comments - some of them make no sense.

...R

Have two variables for the buttonState called buttonState1 and buttonState2
Then:-

int buttonState1 = 0, buttonState2 = 0 ;
int armedswitchPin1 = 4, armedswitchPin2 = 5; // do not use pins 0 or 1 those arre for the serial port

then

buttonState1 = digitalRead(armedswitchPin1);
buttonState2 = digitalRead(armedswitchPin2);

then

if (buttonState1 == LOW || buttonState2 == LOW) {

I will try that later. Thanks! By the way, the I left the comments from the example code, that's why there not making sense.

It's not working...
Restart the question:

Could someone help me add the second switch (switch2 with buttonstate2) because I really can't manage to do it...
Also, how could I arrange this code so it always checks if the buttons are pressed (in the void loop, I guess), but if it detects a button is pressed, it sounds the buzzer a specific amount of times (like I put in the void loop, except not continuously).

Here is the code. (I am using pin 13 for the buzzer so I can check if it works without hearing the buzzer all the time by using the on-board led)

int switchState1 = 0;         
int switchState2 = 0;         

int switch1 = 1;     
int switch2 = 2;   
int buzzer = 13;

void setup() {
  pinMode(buzzer, OUTPUT);      
  pinMode(switch1, INPUT);  
  pinMode(switch2, INPUT);    
}

void loop() {
  
  switchState1 = digitalRead(switch1);

  if (switchState1 == LOW) {        
  digitalWrite(buzzer, LOW); 
  delay(300);               
  digitalWrite(buzzer, HIGH);    
  delay(300);               

  } 
  else {
    
    digitalWrite(buzzer, LOW); 
  }
}

It's not working...
Restart the question:

That is not how we play things here.

It pisses people off especially me.
Your on your own mate.
Bye.

Hello!
How could I arrange this code so it always checks if the buttons are pressed (in the void loop, I guess), but if it detects a button is pressed, it sounds the buzzer a specific amount of times (like I put in the void loop, except not continuously).

Here is the code. (I am using pin 13 for the buzzer so I can check if it works without hearing the buzzer all the time by using the on-board led)

int buttonState1 = 0;         
       

int switch1 = 1;       
int buzzer = 13;

void setup() {
  pinMode(buzzer, OUTPUT);      
  pinMode(switch1, INPUT);  
 
}

void loop() {
  
  buttonState1 = digitalRead(switch1);

  if (buttonState1 == LOW) {     
      
    digitalWrite(buzzer, LOW); 
  delay(300);               
  digitalWrite(buzzer, HIGH); 
  delay(300);              
  

  } 
  else {
   
    digitalWrite(buzzer, LOW); 
  }
}

The first thing to do is to stop using pin 1 as your input because it is used by the serial port. This will prevent you printing any debugging information if/when you need to.

You need to refine what you mean by

but if it detects a button is pressed

Do you mean when the button is held down or do you you mean when the button is first pressed ? If the latter, then look at the StateChangeDetection example in the IDE. Basically you remember the previous state of the button and only act if has changed.

As to turning the buzzer on/off a number of times, look at for loops in the Arduino reference. They allow you to do repetitive things a number of times without repeating code. Putting the buzzer code in a function would be a good exercise, particularly if you pass it the number of times to sound the buzzer.

Have you got a pull up resistor in your button circuit ?
If not then use  pinMode(switch1, INPUT_PULLUP);so that the button pin is in a known state at all times.

Cross-posting is also something that pisses people off mightily too.

Topics merged DO NOT CROSS-POST - IT WASTES TIME

It is actually a switch, so I guess it's when it's held down. No, i do not have a pull up resistor. And how could I make a buzzer function?

Oh and please reply to this with an answer or something helpful and constructive, not negative comments or other things like that.

I didn't ban you.

Is that constructive enough?

I managed to fix the 2 button problem. The 2 buttons work fine. But I still would like to know how I can put the buzzer delays in a function.

Frank-duino:
It is actually a switch, so I guess it's when it's held down. No, i do not have a pull up resistor. And how could I make a buzzer function?

Oh and please reply to this with an answer or something helpful and constructive, not negative comments or other things like that.

okay, why is it "not working" in Reply #4.

do NOT start again - a solution has been given, if it doesn't work - (trust me) the problem is at YOUR end, not that the solution was mistaken.

OK fine, fine! Just never-mind what I said. But how can I make a "buzzer function"?

Frank-duino:
OK fine, fine! Just never-mind what I said. But how can I make a "buzzer function"?

a)

 void buzzer() {
  digitalWrite(<<pin of the buzzer>>, HIGH);
}

or

b) "by connecting it to some electric current"

And I put this in the void setup?

No. Like this

byte buttonState1 = 0;         
const byte switch1Pin = 7;       
const byte buzzerPin = 13;

void setup() 
{
  pinMode(buzzerPin, OUTPUT);      
  pinMode(switch1Pin, INPUT_PULLUP);   
}

void loop() 
{
  buttonState1 = digitalRead(switch1Pin);

  if (buttonState1 == LOW) 
  {         
    buzz(5);         
  } 
}

void buzz(int numberOfTimesToBuzz)
{
  for (int i = 0; i < numberOfTimesToBuzz; i++)
  {
    digitalWrite(buzzerPin, HIGH); 
    delay(300);               
    digitalWrite(buzzerPin, LOW); 
    delay(300);      
  }
}

Ah ok, I will try that! Thanks a lot!

I added it to my code, and I now have this:

byte buttonState1 = 0;
const byte switch1 = 2;
const byte buzzer = 10;
int led = 13;
  


void setup() {                
  
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);      
  pinMode(switch1, INPUT_PULLUP);  
  pinMode(switch2, INPUT_PULLUP); 
  
  void buzzer() {
  digitalWrite(10, HIGH);
}

  
  digitalWrite(buzzer, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(250);               // wait for a second  
  digitalWrite(buzzer, LOW);   // turn the LED on (HIGH is the voltage level)  // wait for a second  
 
  
}


void loop() {

  
 buttonState1 = digitalRead(switch1);

  if (buttonState1 == LOW) 
  {     
       buzz(5);         
  } 
}

void buzz(int 2)
{
  for (int i = 0; i < 2; i++)
  {
    digitalWrite(buzzerPin, HIGH); 
    delay(300);               
    digitalWrite(buzzerPin, LOW); 
    delay(300);      
  }
}


}

It gives me the following error concerning "int led = 13":

expected ',' or '...' before numeric constant

How do I fix this?

Try Auto Format on the code. It will give you a clue. The error is not actually on the line that compiling the code reports.

Putting each curly brace on its own line would help prevent the mistake that you have made.