Unit 16 – One-Button-Piano

Arduino project one button piaano

Do you want to build a real musical instrument? Then let’s go!

Parts

Circuit

Now, we will create a piano with only one button. The piano should always play a tone when the button is pressed. If it is released the tone should mute.

int speakerPin = 9;
int buttonPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
}

void loop() {
  if (digitalRead(buttonPin)==HIGH){
    tone(speakerPin, 440);
  } else {
    noTone(speakerPin);
  }
}

But how can we play a melody with it? In the last unit we almost had a solution. Do you remember? We used the for-loop to play sounds out of an array. What if the button would do the counting, not the for-loop? But we need to use a trick to do it. It is not enough to ask:

if (digitalRead(buttonPin)==HIGH){
 ton = ton + 1;
}

If we would do that, the if statement would be true as long as the button is pressed. It would count much to fast. So we need a construction to detect if the button is pressed and just count up by one.

Boolean variables

Variables of the boolean type only know two conditions: true and false. If we would have a variable named buttonPressed, it could store the current state of the button.

  1. With the programming start the button is not pressed. The variable buttonPressed is set to false.
  2. Now, we don’t just ask if digitalRead(buttonPin) == HIGH, but even if buttonPressed == false. This can be done with the double et-character (&&).
  3. If both conditions are true, buttonPressed is set to true.
  4. If the button is not pressed, buttonPressed is set to false.

Our program looks like that:

boolean buttonPressed = false;

int speakerPin = 9;
int buttonPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
}

void loop() {
  if ((digitalRead(buttonPin)==HIGH)&&(buttonPressed==false)){
    buttonPressed=true;


  } 
  if (digitalRead(buttonPin)==LOW){
    buttonPressed=false;


  }
}

Great! Now we can use the counter variable, too. If it get’s to large, we have to reset it back to 0. This is done by the third if-statement:

int tonesTotal = 27;
int theTone = 0;

boolean buttonPressed = false;

int speakerPin = 9;
int buttonPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
}

void loop() {
  if ((digitalRead(buttonPin)==HIGH)&&(buttonPressed==false)){
    buttonPressed=true;

    theTone = theTone +1;
  } 
  if (digitalRead(buttonPin)==LOW){
    buttonPressed=false;

  }
  if (theTone>= tonesTotal){
    ton=0;
  }
}

Und nun brauchen wir nur noch die Tonausgabe hinzuzufügen:

int frequencies[] = {
  262, 294, 330, 349, 392, 392, 440, 440, 440, 440, 392, 440, 440, 440, 440, 392, 349, 349, 349, 349, 330, 330, 392, 392, 392, 392, 262};

int tonesTotal = 27;
int theTone = 0;

boolean buttonPressed = false;

int speakerPin = 9;
int buttonPin = 11;

void setup() {
  pinMode(buttonPin, INPUT);
}

void loop() {
  if ((digitalRead(buttonPin)==HIGH)&&(buttonPressed==false)){
    buttonPressed=true;
    tone(speakerPin, frequencies[theTone]);
    theTone = theTone +1;
  } 
  if (digitalRead(buttonPin)==LOW){
    buttonPressed=false;
    noTone(speakerPin);
  }
  if (theTone >= tonesTotal){
    theTone = 0;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *