3 buttons creates 4 sounds with piezo buzzer

Hello I am trying to create 4 different tones with three buttons. I am having trouble with the "&&" statements. Well, I believe so.

I specifically need help with the last two else if statements.

no error appears, but I am not getting my 4th sound.

SIK_Circuit_2B_trumpet

void setup() {
  //set the button pins as inputs
  pinMode(firstKeyPin, INPUT_PULLUP);
  pinMode(secondKeyPin, INPUT_PULLUP);
  pinMode(thirdKeyPin, INPUT_PULLUP);


  //set the buzzer pin as an output
  pinMode(buzzerPin, OUTPUT);
}


void loop() {


  if(digitalRead(firstKeyPin) == LOW){        //if the first key is pressed
    tone(buzzerPin, 262);                     //play the frequency for c
  }
  else if(digitalRead(secondKeyPin) == LOW){  //if the second key is pressed
    tone(buzzerPin, 330);                     //play the frequency for e
  }
  else if(digitalRead(thirdKeyPin) == LOW){   //if the third key is pressed
    tone(buzzerPin, 392);    }
    
//___________________________________________________

    **//else if((digitalRead(secondKeyPin) == LOW)&& (digitalRead(firstKeyPin) == LOW)){  //if the second key is pressed**
**    //tone(buzzerPin, 400); //play the frequency for g**
** // }**

  else if((digitalRead(firstKeyPin) == LOW)&& (digitalRead(secondKeyPin) == LOW)&& (digitalRead(thirdKeyPin) == HIGH)){
        tone(buzzerPin, 450);    }

  
  // ______________________________________________________
  
  else{
    noTone(buzzerPin);                        //if no key is pressed turn the buzzer off
  }
}

Your program never makes it to the 4th if-statement because by that time, either of the other 3 will already be evaluated as true.

Look at your first 3 conditions. If any of them are true then the fourth test will never be executed

Move your fourth test to the start of the if/else sequence so that the combination is tested for first

@acobi001, your topic has been moved to a more suitable location on the forum. Introductory tutorials is for tutorials, not for questions :wink:

@sterretje Thank you! I am still getting used to this forum.

@anon35827816 and @UKHeliBob I tried moving the && statement to the top and it still is not producing a 4th sound.
I merely play the tone for the second button. I added a fourth button to make the sound of the combined press of buttons 1 and 2. This is to double check the sounds.

Thoughts?


//set the pins for the button and buzzer
int firstKeyPin = 2;
int secondKeyPin = 3;
int thirdKeyPin = 4;
int fourthKeyPin = 5;



int buzzerPin = 10;




void setup() {
  //set the button pins as inputs
  pinMode(firstKeyPin, INPUT_PULLUP);
  pinMode(secondKeyPin, INPUT_PULLUP);
  pinMode(thirdKeyPin, INPUT_PULLUP);
  pinMode(fourthKeyPin, INPUT_PULLUP);


  //set the buzzer pin as an output
  pinMode(buzzerPin, OUTPUT);
}


void loop() {
if((digitalRead(firstKeyPin) == LOW)&& (digitalRead(secondKeyPin) == LOW)){
        tone(buzzerPin, 450);    }


  else if(digitalRead(firstKeyPin) == LOW){        //if the first key is pressed
    tone(buzzerPin, 262);                     //play the frequency for c
  }
  else if(digitalRead(secondKeyPin) == LOW){  //if the second key is pressed
    tone(buzzerPin, 330);                     //play the frequency for e
  }
  else if(digitalRead(thirdKeyPin) == LOW){   //if the third key is pressed
    tone(buzzerPin, 392);    }
  
  else if(digitalRead(fourthKeyPin) == LOW){  //if the second key is pressed
    tone(buzzerPin, 450);                     //play the frequency for e
  }
    //else if((digitalRead(secondKeyPin) == LOW)&& (digitalRead(firstKeyPin) == LOW)){  //if the second key is pressed
    //tone(buzzerPin, 392); //play the frequency for g
 // }

  
  
  
  
  else{
    noTone(buzzerPin);                        //if no key is pressed turn the buzzer off
  }
}


  /*
  note  frequency
  c     262 Hz
  d     294 Hz
  e     330 Hz
  f     349 Hz
  g     392 Hz
  a     440 Hz
  b     494 Hz
  C     523 Hz
  */

Have you taken into account that you cannot actually push both buttons at exactly the same time ? One of them will always be before the other ?

No, I have never taken that into account.

I was asked to attempt a challenge to create 6 sounds with three keys.
1 tone for each individual button and 3 other sounds when I push buttons 1 &2, buttons 2 &3, and then when I push all buttons at the same time.

Does this make sense?

It does, but it also means that whenever a button is pressed, you will have to make the program wait in case a second button is pressed along with it within a predetermined period of time. So you'll have to get creative with millis().

1 Like

This sketch prints a value from 0 to 7 each of which represents a particular combination of buttons pressed. Use that value to make the appropriate tone

const byte buttonPins[] = {A3, A2, A1};
const byte values[] = {1, 2, 4};
byte total;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int p = 0; p < 3; p++)
  {
    pinMode(buttonPins[p], INPUT_PULLUP);
  }
}

void loop()
{
  total = 0;
  for (int p = 0; p < 3; p++)
  {
    if (digitalRead(buttonPins[p]) == LOW)
    {
      total += values[p];
    }
  }
  Serial.println(total);
  delay(100);
}

If you put the tones in an array then all you need to do is to add one line of code

1 Like

What is a good course or reference to learn to code simply as you do. I went through the Sparkfun inventor's guide and I still have trouble coming up with my own code from scratch.

Thank you for this code by the way.

They key thing is to recognize redundancy, and whittle it away. I know that's not a direct answer to your question, but it's the insight I took away from it. Concise code can be really elegant.

Sorry, but I have no idea. Writing the code is actually near the end of the process. Thinking about the logic of what you are trying to do is far more important.

Take the requirements of your project. The idea of assigning a power of 2 to each pin state just popped into my head as I was writing a reply to this thread suggesting another way of doing it. I have no idea where the idea came from, it just popped into my mind. Originally I was going to suggest a series of if statements, but that felt clumsy, so the idea of a for looped occurred to me, but how to derive the powers of 2 based on the for loop variable ? It would be possible to calculate them but an array seemed more natural, was easy to implement and would be fast in execution

I was as pleased as Punch when I put it together and it all worked and it gave me the same sort of satisfaction as doing a crossword

I have been writing programs in one form or another since 1974 and one of the aspects of programming that I enjoy the most is making code as elegant as possible to do what is required. I am not sure that you can learn that from a book, course or even another person. It comes with experience and you can't learn that

Good luck with your future projects

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.