Piezo making different sounds when it should be sounding the same.

Hi folks, looking for some insight.

I don't have nay more details on the piezo as it came in a kit (The oomlout/ardx/solarrobotics Experimenter's Kit).

However, the buzzer is making a different sound depending on what push button I hit.

Also, if I hold one of the push buttons, then push the other one, the 2nd press will over-ride the sound. However, if I do it with the other button, it will not over-ride the sound.

I've got a 560 ohm resistor each to ground out my 2 buttons. 2200 ohm resistors on either LED, and a 560 in series with my piezo.

Also, any critiquing of the coding is definitely welcome as this is my first project!

Thanks in advance!

int pb1Pin = 2;
int pb2Pin = 4;
int redLedPin = 8;
int buzzer = 9;
int greenLedPin = 13;
int counter = 0;


void setup() {
  //Serial.begin(9600);
  pinMode(redLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(pb1Pin, INPUT);
  pinMode(pb2Pin, INPUT);

  digitalWrite(greenLedPin, HIGH);
  delay(1000);
  digitalWrite(greenLedPin, LOW);
}

void loop() {
  if (digitalRead(pb1Pin) == HIGH || digitalRead(pb2Pin) == HIGH)
  {
        digitalWrite(redLedPin, HIGH);
        tone(buzzer, 10000);
        noTone(buzzer);
        
        if (digitalRead(pb1Pin) == HIGH){
            //Serial.print(counter);
            //Serial.println(": PLAYER1");
        }else{
          //Serial.print(counter);
          //Serial.println(": PLAYER2");    
        }
  }else{
        digitalWrite(redLedPin, LOW);
  }
}

The tone() function returns immediately so you are starting a tone and then immediately turning it off

As for the code, you can declare your pins using the 'const' keyword, e.g. 'const int pb1Pin = 2;' which tells the compiler that the variable will never change (and you get an error if you try to).

You can also simplify your wiring by connecting one side of your pushbutton to ground and the other to your pin. Then declare it as INPUT_PULLUP rather than PULLUP. It will reverse the logic (LOW == pressed, HIGH == not pressed) but eliminates the need for external resistors.

Nice job using code tags!

Oh my god.
That makes a lot of sense. Completely skipped my mind. I guess the difference in voltages/resistance, even slightly could be causing the code loop to run at different speeds, essentially making the output operate as a PWM even though it already is, correct?

At either rate, moving the no-tone out is what I will try tonight to see if that makes it work properly.

Also, I don't understand the part about removing the resistors - isn't it necessary to create the voltage divider as to minimize the effects of stray voltages?

I've also completely ignored denouncing seeing as it only impacts the very start of the tone, but I was considering adding that in too just be thorough.

I've been following Jeremy Blum's youtube videos and have a Learn to Arduino in 24 hours book, and the experimenter kit's guide book that came with it so far, which all seems to help! And forum life (Autodesk Inventor) means I love the code tags!

void loop() {
  if (digitalRead(pb1Pin) == HIGH || digitalRead(pb2Pin) == HIGH)
  {
        digitalWrite(redLedPin, HIGH);
        tone(buzzer, 10000);
       
        if (digitalRead(pb1Pin) == HIGH){
            //Serial.print(counter);
            //Serial.println(": PLAYER1");
        }else{
          //Serial.print(counter);
          //Serial.println(": PLAYER2");   
        }
  }else{
        digitalWrite(redLedPin, LOW);
        [b]noTone(buzzer);[/b]
  }
}

If you declare a pin as INPUT_PULLUP, you are connecting an internal pull-up resistor to the pin that is somewhere between 20K-50K. When the button is not pressed, it is not connected to anything but the pin is connected through the pullup resistor to +5V. Since the pin is an input, it draws negligible current. When the button is pressed, you are shorting the pin to ground but still through the pullup so only a very small amount of current flows and the pin reads LOW.

If you want to debounce your buttons, look at the StateChangeDetection example in the IDE (Files->Examples->02.Digital->StateChangeDetection) since you really only have to do things when buttons change state, not when they are in a given state.