Description
For this week's lab I used one piezo speaker to act as a singing coach, controlled by a potentiometer. The first exercise is the "c, d, e, f, g, f, e, d, c". It keep playing until the singer is confident that he can sing the melody correctly. He/she then increased the "volume" of the pot in order to move to the next half tone, c# in our example. The melody follows the same pattern: tone, tone, half tone, tone, (backwards) half tone, tone, tone. The pot has 8 different scales, with the last melody for this example starting from g and the highest key being D.
Here you can see the "singing coach" in action. I shortened the pause between the melodies for the sake of the video duration:
http://youtu.be/PM3h9Cyj59o
Components
1 Arduino Uno
1 Breadboard
1 potentiometer
1 10k resistor
1 piezo speaker
cables
Code
/* Singing Coach
* ------------
*
* Program to play melody depending on the value of the pot.
*
* The calculation of the tones is made following the mathematical
* operation:
*
* timeHigh = 1/(2 * toneFrequency) = period / 2
*
* where the different tones are described as in the table:
*
* note frequency period PW (timeHigh)
* c 261 Hz 3830 1915
* c# 277 Hz 1805
* d 294 Hz 3400 1700
* d# 311 Hz 1607
* e 329 Hz 3038 1519
* f 349 Hz 2864 1432
* f# 369 Hz 1355
* g 392 Hz 2550 1275
* g# 415 Hz 1204
* a 440 Hz 2272 1136
* a# 466 Hz 1072
* b 493 Hz 2028 1014
* C 523 Hz 1912 956
* C# 554 Hz 902
* D 587 Hz 851
* D# 622 Hz 803
* E 659 Hz 758
* F 698 Hz 716
* F# 739 Hz 676
* G 783 Hz 638
*
* Created by ploumou for TUI
*
*/
int sensorPin = 0; // select the input pin for the potentiometer
int sensor_val = 0; // variable to store the value coming from the sensor
int speakerOut = 7;
// note names and their corresponding half-periods
byte names[] ={ 'c', 'h', 'd', 'i', 'e', 'f', 'j', 'g', 'k', 'a', 'l', 'b', 'C', 'H', 'D', 'I', 'E', 'F', 'J', 'G'};
int tones[] = { 1915, 1805, 1700, 1607, 1519, 1432, 1355, 1275, 1204, 1136, 1072, 1014, 956, 902, 851, 803, 758, 716, 676, 638};
byte mel1[] = {"2c2d2e2f2g2f2e2d2c8p8p8p"};
byte mel2[] = {"2h2i2f2j2k2j2f2i2h8p8p8p"};
byte mel3[] = {"2d2e2j2g2a2g2j2e2d8p8p8p"};
byte mel4[] = {"2i2f2g2k2l2k2g2f2i8p8p8p"};
byte mel5[] = {"2e2j2k2a2b2a2k2j2e8p8p8p"};
byte mel6[] = {"2f2g2a2l2C2l2a2g2f8p8p8p"};
byte mel7[] = {"2j2k2l2b2H2b2l2k2j8p8p8p"};
byte mel8[] = {"2g2a2b2C2D2C2b2a2g8p8p8p"};
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 12;
int statePin = LOW;
void setup() {
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerOut, LOW);
sensor_val = analogRead(sensorPin); // read the value from the sensor, between 0 - 1024
for (count = 0; count < MAX_COUNT; count++) {
//statePin = !statePin;
//digitalWrite(ledPin, statePin);
for (count3 = 0; count3 <= (mel2[count*2] - 24) * 10; count3++) {
for (count2=0;count2<20;count2++) {
// start with c
if (sensor_val < 128){
if (names[count2] == mel1[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel1[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to c#
else if (sensor_val < 256){
if (names[count2] == mel2[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel2[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to d
else if (sensor_val < 384){
if (names[count2] == mel3[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel3[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to d#
else if (sensor_val < 512){
if (names[count2] == mel4[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel4[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to e
else if (sensor_val < 640){
if (names[count2] == mel5[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel5[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to f
else if (sensor_val < 768){
if (names[count2] == mel6[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel6[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to f#
else if (sensor_val < 896){
if (names[count2] == mel7[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel7[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
//go to g
else if (sensor_val < 1024){
if (names[count2] == mel8[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (mel8[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(300);
}
}
}
}
}
}
- Login to post comments