0

Sorry for any grammar mistakes. It's my second language.

I had a little test with my Arduino Uno R3 I connected 2 buttons to power , GND using 10k resistors and pins 2 and 4. I connected piezo to power and ground.

When you pressed a button1 then you should hear 'c' tone, but when I pressed the second button nothing happend and I don't understand why?

Here is my code below:

const int buttonPin2 = 4;
const int buttonPin1 = 2;   
const int piezopin =  6;      


int buttonState1 = 0;
int buttonState2 = 0;        

void setup() {

  pinMode(piezopin, OUTPUT);
  pinMode(buttonPin2,INPUT);
  pinMode(buttonPin1, INPUT);
}

void loop() {

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);



  if (buttonState1 == HIGH) {
    tone(6,261.63,500);

  } else {



  }
  if (buttonState2 == HIGH) {
    tone(6,392.00,500);
  } else {

  }

migrated from electronics.stackexchange.com Feb 8 '17 at 2:24

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    What happens when you press the first button? I assume you have a final closing brace not shown in your code above. Perhaps try writing messages to the console in each of the button pressed cases to see if it is reading the inputs correctly. – AngeloQ Feb 7 '17 at 21:14
  • 4
    There is a free schematic tool when posting. Use it please. – Bort Feb 7 '17 at 21:23
  • 1
    You should add a debounce in the code for the buttons. And this is a state change code. You should try going to the Arduino website for an example code as a reference. I'm a bit rusty, but I dont think the if/else statements are right. But this is relatively straight forward basic code, lots of help around the web if you look for it. – Rayray Feb 7 '17 at 22:12
  • 1
    @Rayray Nothing technically wrong with the if/else statements but the else clauses are empty so they could be removed. – AngeloQ Feb 7 '17 at 23:19
  • What is your wiring? Do you have any pull-down resistors? – Nick Gammon Feb 8 '17 at 9:05
1

Try calling noTone() after the tone is played. This is a kind of "reset" for the pin.

Also, you forgot a } at the end of your code.

If these didn't solve your code, then it most probably is an electric problem.

  • I know what was my problem. i was thinking my buttons are upright but they are not.my program is working now.thank you for your help.:-) – Konrad5623 Mar 11 '17 at 20:31

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.