Last active
December 21, 2015 00:38
-
-
Save leifjones/6221545 to your computer and use it in GitHub Desktop.
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
/* | |
* This is the work D.B. of South Hamilton High School. | |
* It draws on the started code from SparkFun. | |
*/ | |
const int SENSOR_PIN = 0; | |
const int BUZZER_PIN = 9; | |
const int c_pin = 2; | |
const int d_pin = 3; | |
const int e_pin = 4; | |
const int f_pin = 5; | |
const int g_pin = 6; | |
const int a_pin = 7; | |
const int b_pin = 8; | |
const int C_pin = 10; | |
void setup() { | |
pinMode(BUZZER_PIN, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
int sensorValue; | |
sensorValue = analogRead(SENSOR_PIN); | |
Serial.println(sensorValue); | |
if (sensorValue > 20 && sensorValue < 129){ | |
tone(BUZZER_PIN, frequency('c')); | |
digitalWrite(c_pin, HIGH); | |
turnOffOthers(2); | |
} else if (sensorValue > 128 && sensorValue < 257){ | |
tone(BUZZER_PIN, frequency('d')); | |
digitalWrite(d_pin, HIGH); | |
turnOffOthers(3); | |
}else if (sensorValue > 256 && sensorValue < 384) { | |
tone(BUZZER_PIN, frequency('e')); | |
digitalWrite(e_pin, HIGH); | |
turnOffOthers(4); | |
}else if (sensorValue > 383 && sensorValue < 512){ | |
tone(BUZZER_PIN, frequency('f')); | |
digitalWrite(f_pin, HIGH); | |
turnOffOthers(5); | |
}else if (sensorValue > 511 && sensorValue < 640){ | |
tone(BUZZER_PIN, frequency('g')); | |
digitalWrite(g_pin, HIGH); | |
turnOffOthers(6); | |
}else if (sensorValue > 639 && sensorValue < 768){ | |
tone(BUZZER_PIN, frequency('a')); | |
digitalWrite(a_pin, HIGH); | |
turnOffOthers(7); | |
}else if (sensorValue > 767 && sensorValue < 896){ | |
tone(BUZZER_PIN, frequency('b')); | |
digitalWrite(b_pin, HIGH); | |
turnOffOthers(8); | |
}else if (sensorValue > 895 && sensorValue < 1024){ | |
tone(BUZZER_PIN, frequency('C')); | |
digitalWrite(C_pin, HIGH); | |
turnOffOthers(10); | |
}else if (sensorValue < 20){ | |
noTone(BUZZER_PIN); | |
digitalWrite(c_pin, LOW); | |
digitalWrite(d_pin, LOW); | |
digitalWrite(e_pin, LOW); | |
digitalWrite(f_pin, LOW); | |
digitalWrite(g_pin, LOW); | |
digitalWrite(a_pin, LOW); | |
digitalWrite(b_pin, LOW); | |
digitalWrite(C_pin, LOW); | |
} | |
} | |
int frequency(char note) | |
{ | |
// This function takes a note character (a-g), and returns the | |
// corresponding frequency in Hz for the tone() function. | |
int i; | |
const int numNotes = 8; // number of notes we're storing | |
// The following arrays hold the note characters and their | |
// corresponding frequencies. The last "C" note is uppercase | |
// to separate it from the first lowercase "c". If you want to | |
// add more notes, you'll need to use unique characters. | |
// For the "char" (character) type, we put single characters | |
// in single quotes. | |
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; | |
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523}; | |
// Now we'll search through the letters in the array, and if | |
// we find it, we'll return the frequency for that note. | |
for (i = 0; i < numNotes; i++) // Step through the notes | |
{ | |
if (names[i] == note) // Is this the one? | |
{ | |
return(frequencies[i]); // Yes! Return the frequency | |
} | |
} | |
return(0); // We looked through everything and didn't find it, | |
// but we still need to return a value, so return 0. | |
} | |
void turnOffOthers( int keepOn ){ | |
for(int i=2;(i<=10&&i!=9); i=i+1){ | |
if(i!=keepOn){ | |
digitalWrite( i , LOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment