I wanted to build upon the piano I 3D printed during Week 5. In particular, I integrated buttons as an input device to a piezo buzzer as the output device to create functional piano. Although I do prefer acoustic pianos, I wanted to see what it would be like to build and play my own electric one.
In a similar model to the piano I printed during Week 5, I designed a smaller keyboard for this week. I knew that the number of keys on the piano would compound the amount of coding I would have to do later. The figure to the right shows a large version of the key bed with buttons inserted into the 3D printed slots. There are also holes in the underside of the grid that allow the prongs of the buttons to protude to be soldered to wires. The Fusion 360 Model below reflects the piano I used for this week's assignment.
I soldered the button ends to wires in order to better connect them to the circuit with the buzzer. The circuit features 1kΩ resistors connecting one side of the buttons to ground. The other side of the buttons goes directly to pins 2, 3, and 4 on the Arduino Uno. The circuit for the piano is shown below.
Using a file that defines the pitches for each of the tones on a keyboard (pitches.h), I was able to program the piano to play the notes C, C#, and D. I referenced this tutorial to help design the circuit. With this limited chromatic scale of notes, I played the theme from Jaws in the Demo video below! The Arduino Code used to program the piano is at the bottom of the page.
#include "pitches.h"
#define ACTIVATED LOW
const int PIEZO = 11;
const int LED = 13;
const int BUTTON_D = 4;
const int BUTTON_CS = 3;
const int BUTTON_C = 2;
void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(BUTTON_C, INPUT);
digitalWrite(BUTTON_C,HIGH);
pinMode(BUTTON_CS, INPUT);
digitalWrite(BUTTON_CS,HIGH);
pinMode(BUTTON_D, INPUT);
digitalWrite(BUTTON_D,HIGH);
digitalWrite(LED,LOW);
}
void loop()
{
while(digitalRead(BUTTON_C) == ACTIVATED)
{
tone(PIEZO,NOTE_C4);
digitalWrite(LED,HIGH);
}
while(digitalRead(BUTTON_CS) == ACTIVATED)
{
tone(PIEZO,NOTE_CS4);
digitalWrite(LED,HIGH);
}
while(digitalRead(BUTTON_D) == ACTIVATED)
{
tone(PIEZO,NOTE_D4);
digitalWrite(LED,HIGH);
}
noTone(PIEZO);
digitalWrite(LED,LOW);
}