Skip to content


Arduino keyboard

My first Arduino project, using 8 buton, 8 leds, and 16 resistors. Latest addition is a pitch bend knob. The tones are generated by changing the delay times set to turn on and off the piezo buzzer simulating a square wave. I got the delay setting for each note from this melody tutorial.

On my first rev I used if/else if commands for the notes, but realised the delay on each iteration is so small that polyphony could be simulated with just a series of ifs.

code:

int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int a = 9;
int b = 10;
int c2 = 11;
int buzzerPin = 2;
int val=0;
int potPin=2;
void setup () {
Serial.begin(9600);
pinMode(c,INPUT);
pinMode(d,INPUT);
pinMode(e,INPUT);
pinMode(f,INPUT);
pinMode(g,INPUT);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(c2,INPUT);
pinMode(buzzerPin,OUTPUT);
}
void loop () {
val=map(analogRead(potPin),0,1024,0,500);
if (digitalRead(c)==1){
snd(1915,val);
Serial.println('c');
}
if (digitalRead(d)==1){
snd(1700,val);
Serial.println('d');
}
if (digitalRead(e)==1){
snd(1519,val);
Serial.println('e');
}
if (digitalRead(f)==1){
snd(1432,val);
Serial.println('f');
}
if (digitalRead(g)==1){
snd(1275,val);
Serial.println('g');
}
if (digitalRead(a)==1){
snd(1136,val);
Serial.println('a');
}
if (digitalRead(b)==1){
snd(1014,val);
Serial.println('b');
}
if (digitalRead(c2)==1){
snd(956,val);
Serial.println('h');
}

}

void snd (int inp, int vib) {
digitalWrite(buzzerPin,HIGH);
delayMicroseconds(inp+vib);
digitalWrite(buzzerPin,LOW);
delayMicroseconds(inp);
}

Posted in arduino, geek.

Tagged with .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.