rudbeckianska_wep
Published © GPL3+

Buzzer Piano

Buttons that plays the notes C-A with a buzzer.

BeginnerShowcase (no instructions)1,407
Buzzer Piano

Things used in this project

Story

Read more

Schematics

Circuit schematic

Each button is connected to a 10k ohm resistor. The buzzer is connected to input 13 and ground.

Code

Code

C/C++
// defines buttonPins
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;

const int buzzer =  13;      // This is the pin connected to the buzzer

// defines the states of the buttons
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;

void setup() {
  // Set buzzer-pin as output:
  pinMode(buzzer, OUTPUT);
  // Set button-pins as input:
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);

}

void loop() {
  // reads button value:
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
  buttonState7 = digitalRead(buttonPin7);
  buttonState8 = digitalRead(buttonPin8);

  // Plays a specific frequency depending on which button are pressed
  if (buttonState2 == HIGH) {
    tone(buzzer, 440.000);  // sends a frequency of 440Hz
  }
  else if (buttonState3 == HIGH) {
    tone(buzzer, 391.995);
  }
  else if (buttonState4 == HIGH) {
    tone(buzzer, 349.228);
  }
  else if (buttonState5 == HIGH) {
    tone(buzzer, 329.628);
  }
  else if (buttonState6 == HIGH) {
    tone(buzzer, 293.665);
  }
  else if (buttonState7 == HIGH) {
    tone(buzzer, 261.626);
  }
  else if (buttonState8 == HIGH) {
    
    // Plays a series of tones when the last button is pressed

    tone(buzzer, 329.628);
    noTone(buzzer);
    delay(100);
    tone(buzzer, 329.628);
    delay(100);
    tone(buzzer, 329.628);
    delay(200);
    tone(buzzer, 261.626);
    delay(200);
    noTone(buzzer);
    delay(2000);
    tone(buzzer, 329.628);
    delay(200);
    tone(buzzer, 97.9989);
    delay(100);
  } 
  else {
    noTone(buzzer); // stops sending frequencies to the buzzer
  }
  
}

Credits

rudbeckianska_wep
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.