Final Arduino project :: Michael White

For my final project i decided to develop my keyboard idea.

I really wanted to make something that used synthy sounds, but after poking around on forums and blogs, i found that the coding was extremely out of my league. This led me to just developing a piezo keyboard, attempting to play multiple tones at the same time.

For this I researched different libraries, the tone.h library, using this forum entry to help with code, i tried developing my own simple keyboard, using 4 separate speakers to correspond with 4 buttons.

I first included the tone.h library and defined the pins and speakers (I included a potentiometer in the variables because i wanted to develop the idea to change the scale depending on the position of the potentiometer) :

#include <Tone.h>

int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int button5 = 7;
int button6 = 8;
int potPin = A0;
Tone speaker1;
Tone speaker2;
Tone speaker3;
Tone speaker4;

In the void setup(), from looking at the tone.h wiki page and forums I found that i had to “start” the speakers using the speakerX.begin(Y); function. This declared the pin number of each speaker. essentially this is an alternative to using the tone() function, but (apparently) allowed multiple tones to be played simultaneously.
I also had to declare the pinMode() of each button.

void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);

The void loop() basically told the all the speakers to play a different tone when one button is pressed, depending on the button it played a different chord, each of which i chose by playing french/gypsy-esque chords. they are seen within the if() statments of the buttons. Each note has a name (eg. NOTE_A4) which, within the tone.h library, refers to a number representing the frequency of the note.

I also used the map() function to limit the potentiometer to [potentially] use 5 different scales.
void loop() {
int key = analogRead(potPin);
key = map(key, 0, 1023, 0, 4);
int state1 = digitalRead(button1);
int state2 = digitalRead(button2);
int state3 = digitalRead(button3);
int state4 = digitalRead(button4);
int state5 = digitalRead(button5);
int state6 = digitalRead(button6);

int note1;
int note2;
int note3;
int note4;

if(state1 == HIGH && state2 == LOW && state3 == LOW && state4 == LOW){;;;;

if(state1 == LOW && state2 == HIGH && state3 == LOW && state4 == LOW){;;;;
if(state1 == LOW && state2 == LOW && state3 == HIGH && state4 == LOW){;;;;
if(state1 == LOW && state2 == LOW && state3 == LOW && state4 == HIGH){;;;;

Unfortunately, after days of tinkering and forum posting, my code still didn’t work, up until the night prior to the deadline. I was lost. Scared even. I had nothing. I curled into a ball on my bed and wept, for what felt like an eternity. falling endlessly into a pit of self loathing. I realised i couldn’t hand in nothing so i browsed and browsed. I found a really cool piece of code. This was for a capacitive keyboard synthesizer. The hardware required 8 strips of copper tape as the keys. To adapt this i used screws as keys attached to rigid wires, allowing the screws to stick out in all direction making for a fun exciting use. I made a chord with 4 of the keys and had these keys sticking out further to when a hand is placed over the whole keyboard, only these notes play. the screws worked really well.

Instead of using a piezo speaker i used a larger speaker.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s