SBR
Published

Keyboard

Keyboard instrument using Piezo and Push Buttons

BeginnerProtip201
Keyboard

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Buzzer, Piezo
Buzzer, Piezo
×1
Push Button
×4
Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 1M ohm
Resistor 1M ohm
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Keyboard

Code

Keyboard

Arduino
/*
Keyboard
*/

// create an array of notes
// the numbers below correspond to the frequencies of middle C, D, E, and F
int notes[] = {262, 294, 330, 349};

void setup() {
//start serial communication
Serial.begin(9600);
}

void loop() {
// create a local variable to hold the input on pin A0
int keyVal = analogRead(A0);
// send the value from A0 to the Serial Monitor
Serial.println(keyVal);

// play the note corresponding to each value on A0
if (keyVal == 1023) {
// play the first frequency in the array on pin 8
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
// play the second frequency in the array on pin 8
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
// play the third frequency in the array on pin 8
tone(8, notes[2]);
} else if (keyVal >= 5 && keyVal <= 10) {
// play the fourth frequency in the array on pin 8
tone(8, notes[3]);
} else {
// if the value is out of range, play no tone
noTone(8);
}
}

Credits

SBR

SBR

25 projects • 14 followers
Mechanical Engineer

Comments