Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Lab 9

Project Members: 
Ethan DeYoung
Hsin-Hsien Chiu
Kathleen Lu
Srikanth Narayan

Description

We built a piano-like instrument that helps you learn how to play a song. When the program is run, the notes of a song are displayed through 4 LEDs, the user then presses the keys that correspond to the LEDs and the correct note is emitted from the speaker.

Components

Arduino

Breadboard

4 LEDs (red, green, blue, yellow)

4 FSRs

4 220 resistors

4 10K resistors 

1 Potentiometer

1 Piezo speaker

Material to construct the piano

wiring

 

Arduino Code

/*
Some of the code was adapted from Wesley Willett of this class! 
*/
//The four input pins
int inputPin1 = 0; 
int inputPin2 = 1;
int inputPin3 = 2;
int inputPin4 = 3;

//Speaker Pin
int speakerPin = 6;

//The four colours
int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledPin4 = 11;

//The potentiometer pin
int potPin = 5;

//Variables to store the pressures on the FSR without any explicit pressures being applied to it
int valDefault1 = 0;     
int valDefault2 = 0;
int valDefault3 = 0;
int valDefault4 = 0;

//Variables to read/store the user input through the FSR
int valRead1 = 0;
int valRead2 = 0;
int valRead3 = 0;
int valRead4 = 0;

int i = 0;
int index = 0;

//THE TUNE COUNT
int MAX_COUNT = 24;
int SENSITIVITY = 20;
int count = 0;


//Narrow down to the required four
byte names[] = {'c', 'd', 'e', 'f' /*, 'g', 'a', 'b', 'C'*/};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};

//The musical note (From a file?)
byte melody[] = "2d2c1f2c2d2e2d2c2f2d2c2c2d2e1f2c2f";

//byte hello[] = "1c1p1d1p1e1p1f1p1g1p1a1p1b1p1C1p";
byte hello[] = "2c1p";
byte goodbye[] = "2d1p";
//byte goodbye[] = "1C1p1b1p1a1p1g1p1f1p1e1p1d1p1c1p";
byte wave[] = "2e1p";
byte wave2[] = "2f1p";
int count2 = 0;
int count3 = 0;
int statePin = LOW;
boolean isOpen = true;
int waveWait = 0;


void setup() {
  pinMode(ledPin1, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(ledPin2, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(ledPin3, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(ledPin4, OUTPUT);  // declare the ledPin as an OUTPUT
  pinMode(speakerPin, OUTPUT);  // declare the ledPin as an OUTPUT
  
  valDefault1 = analogRead(inputPin1);
  valDefault2 = analogRead(inputPin2);
  valDefault3 = analogRead(inputPin3);
  valDefault4 = analogRead(inputPin4);
  
  Serial.println("Ready");
}

void loop() {
  //Narrow down to the required four
  //byte names[] = {'c', 'd', 'e', 'f' /*, 'g', 'a', 'b', 'C'*/};
  //int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
  
  //The musical note (From a file?)
  //byte melody[] = "2d2c1f2c2d2e2d2c2f2d2c2c2d2e1f2c2f";

  //Sound output note
  /*
   * note 	frequency 	period 	PW (timeHigh)	
   * c 	        261 Hz 	        3830 	1915 	
   * d 	        294 Hz 	        3400 	1700 	
   * e 	        329 Hz 	        3038 	1519 	
   * f 	        349 Hz 	        2864 	1432 	
   * g 	        392 Hz 	        2550 	1275 	
   * a 	        440 Hz 	        2272 	1136 	
   * b 	        493 Hz 	        2028	1014	
   * C	        523 Hz	        1912 	956
  */
  
  //read note
  int potVal = analogRead(potPin);
  float potInducedDelay = potVal/512;
  int noteDelay = melody[index*2] - 48;
  int totalDelay = (int)(200 * potInducedDelay * noteDelay);
  
  for(i=0; i < 4/*Change*/; i++) {
    if(melody[index*2 + 1] == 'c') {
      analogWrite(ledPin1, 255);
      //Continuation of the note
      delay(totalDelay);
      analogWrite(ledPin1, 0);        
    }
    if(melody[index*2 + 1] == 'd') {
      analogWrite(ledPin2, 255);
      //Continuation of the note
      delay(totalDelay);
      analogWrite(ledPin2, 0);        
    }
    if(melody[index*2 + 1] == 'e') {
      analogWrite(ledPin3, 255);
      //Continuation of the note
      delay(totalDelay);
      analogWrite(ledPin3, 0);        
    }
	if(melody[index*2 + 1] == 'f') {
      analogWrite(ledPin4, 255);
      //Continuation of the note
      delay(totalDelay);
      analogWrite(ledPin4, 0);        
    }
  }

  //Read values from sensors
  valRead1 = analogRead(inputPin1);
  valRead2 = analogRead(inputPin2);
  valRead3 = analogRead(inputPin3);
  valRead4 = analogRead(inputPin4);
  
  if(valRead1 - valDefault1 > SENSITIVITY) {
	playHello();
  }
  
  if(valRead2 - valDefault2 > SENSITIVITY) {
	playGoodbye();
  }
  
  if(valRead3 - valDefault3 > SENSITIVITY) {
	playWave();
  }
  
  if(valRead4 - valDefault4 > SENSITIVITY) {
	playWave2();
  }
  
Serial.println(valRead1);
Serial.println(valRead2);
Serial.println(valRead3);
Serial.println(valRead4);

  index++;
  if(index == MAX_COUNT) {
    index = 0;
  }
}

void playHello(){
	playJams(hello,2);
}
void playGoodbye(){
	playJams(goodbye,2);
}
void playWave(){
	playJams(wave,2);
}
void playWave2(){
	playJams(wave2,2);
}


//plays the a series of notes defined in an array
void playJams(byte jams[], int length){
	for (count = 0; count < length; count++) {
		statePin = !statePin;
		for (count3 = 0; count3 <= (jams[count*2] - 48) * 30; count3++) {
			for (count2=0;count2<8;count2++) {
				if (names[count2] == jams[count*2 + 1]) {
					digitalWrite(speakerPin,HIGH);
					delayMicroseconds(tones[count2]);
					digitalWrite(speakerPin, LOW);
					delayMicroseconds(tones[count2]);
				}
				if (jams[count*2 + 1] == 'p') {
					// make a pause of a certain size
					digitalWrite(speakerPin, 0);
					delayMicroseconds(500);
				}
			}
		}
	}
}

 

Images

 

http://photo.xuite.net/berkeleychiu/2113602/1.jpg

http://photo.xuite.net/berkeleychiu/2113602/2.jpg

http://photo.xuite.net/berkeleychiu/2113602/3.jpg

http://photo.xuite.net/berkeleychiu/2113602/4.jpg

 

Video 

http://www.youtube.com/watch?v=p6NiDoXdgCI

 


AttachmentSize
PB070018-2.jpg90.67 KB
PB070017-2.jpg100.31 KB
PB070013-2.jpg99.32 KB
PB070011-2.jpg101.23 KB

Powered by Drupal - Design by Artinet