Created
October 18, 2011 01:22
-
-
Save byee01/1294389 to your computer and use it in GitHub Desktop.
Music Arduino Experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Melody | |
* (cleft) 2005 D. Cuartielles for K3 | |
* | |
* This example uses a piezo speaker to play melodies. It sends | |
* a square wave of the appropriate frequency to the piezo, generating | |
* the corresponding tone. | |
* | |
* The calculation of the tones is made following the mathematical | |
* operation: | |
* | |
* timeHigh = period / 2 = 1 / (2 * toneFrequency) | |
* | |
* where the different tones are described as in the table: | |
* | |
* note frequency period 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 | |
* | |
* http://www.arduino.cc/en/Tutorial/Melody | |
*/ | |
// Set 1 | |
int note_C4 = 262; // C | |
int note_E4 = 330; // B | |
int note_G4 = 392; // A | |
int note_A4 = 440; // AB | |
int note_AS4= 466; // AC | |
int note_B4 = 494; // BC | |
int note_C5 = 523; // ABC | |
// Set 2 | |
int note_CS5= 554; // C | |
int note_D5 = 587; // B | |
int note_E5 = 659; // A | |
int note_F5 = 698; // AB | |
int note_G5 = 784; // AC | |
int note_A5 = 880; // BC | |
int note_AS5= 932; // ABC | |
// Set 3 | |
int note_B5 = 988; // C | |
int note_C6 = 1047; // B | |
int note_CS6= 1109; // A | |
int note_D6 = 1175; // AB | |
int note_E6 = 1319; // AC | |
int note_F6 = 1397; // BC | |
int note_G6 = 1568; // ABC | |
// Piezo Element | |
int speakerPin = 10; | |
// Sensor Pins | |
int sensorPinA = 0; | |
int sensorPinB = 1; | |
int sensorPinC = 2; | |
// Sensor Values (default) | |
int sensorAValue = 0; | |
int sensorBValue = 0; | |
int sensorCValue = 0; | |
// Tone Value (determines whether the light is covered) | |
int toneAValue = 0; | |
int toneBValue = 0; | |
int toneCValue = 0; | |
// Tone pressed | |
boolean toneA = false; | |
boolean toneB = false; | |
boolean toneC = false; | |
int threshold = 5; | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
pinMode(sensorPinA, INPUT); | |
pinMode(sensorPinB, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
sensorAValue = analogRead(sensorPinA); | |
sensorBValue = analogRead(sensorPinB); | |
sensorBValue = analogRead(sensorPinC); | |
/* | |
tone(speakerPin, sensorValue); | |
tone(speakerPinB, (sensorValue/2)); | |
*/ | |
toneAValue = map(sensorAValue, 20, 150, 0, 10); | |
toneBValue = map(sensorBValue, 20, 150, 0, 10); | |
toneCValue = map(sensorCValue, 20, 150, 0, 10); | |
toneAValue > threshold ? toneA = true : toneA = false; | |
toneBValue > threshold ? toneB = true : toneB = false; | |
toneCValue > threshold ? toneC = true : toneC = false; | |
Serial.println(toneBValue); | |
// Giant IF statement | |
// A, B, C | |
if(toneA && toneB && toneC) { | |
tone(speakerPin, note_A5); | |
// A, B | |
} else if (toneA && toneB) { | |
tone(speakerPin, note_G6); | |
// B, C | |
} else if (toneB && toneC) { | |
tone(speakerPin, note_C5); | |
// A, C | |
} else if (toneA && toneC) { | |
tone(speakerPin, note_D5); | |
// A | |
} else if (toneAValue > 5) { | |
tone(speakerPin, note_E5); | |
// B | |
} else if (toneBValue > 5) { | |
tone(speakerPin, note_F5); | |
// C | |
} else if (toneCValue > 5) { | |
tone(speakerPin, note_G5); | |
// Nothing covered | |
} else { | |
noTone(speakerPin); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment