Musical Instrument

This page will tell you about the musical instrument I created!!!

How It Works

My musical instrument is like a mini version of a piano, (only 5 buttons). To control the musical instrument allow it to make a sound, the player must press on the button. The duration of the notes will be controlled by how long the player presses on the button. Each button will cue a different note ranging from C, D, E, F, G. When the button is pressed, the note will be projected through the speakers. If the player wants the note to stop playing, all they have to do it let go of the button.

Code

This is the code I used for this musical instrument assignment.

	/*Musical Instrument Assignment
	2017-05-23
	Jieru*/
 
	//variables
  	int key1 = 2; 
  	int key2 = 3; //key2 is wired to port 3
  	int key3 = 4;  //key3 is wired to port 4
  	int key4 = 5;  //key4 is wired to port 5
  	int key5 = 6;  //key5 is wired to port 6  
  	int speaker = 13; //the speaker is wired to port 13
 
	void setup() {
	pinMode(key1, INPUT);
	pinMode(key2, INPUT);
	pinMode(key3, INPUT);
	pinMode(key4, INPUT);
	pinMode(key5, INPUT);
	speaker(speaker, OUTPUT);
	)
 
	void loop() {
	    //if key1 is pressed, note C will be played
	    if (digitalRead(key1)==HIGH) {
		//note C
	   	c ();
	    } 
            //if key2 is pressed, note D will be played
	    if (digitalRead(key2)==HIGH) {
		//note D
	   	d ();
	    }
	    //if key3 is pressed, note E will be played
	    if (digitalRead(key3)==HIGH) {
		//note E
	    	e ();
	    } 
	    //if key4 is pressed, note F will be played
	    if (digitalRead(key4)==HIGH) {
		//note F
    		f ();
	    }
	    //if key5 is pressed, note G will be played
    	    if (digitalRead(key5)==HIGH) {
		//note G
    		g ();
    	    } 
	}	
 
	//note C
	void c () { 
	  tone(speaker, 262, 10);
	}

	//note D
	void d () {
	  tone(speaker, 294, 30);
	}

	//note E
	void e () { 
	  tone(speaker, 330, 30);
	}

	//note F
	void f () { 
	  tone(speaker, 349, 30);
	}

	//note G
	void g () {
	  tone(speaker, 392, 30);
	}
	

Diagram

Musical Instrument Diagram