// Author: Nien Lam // Date: 10-06-2009 // Title: Digital Photo Sensor Tone Generator // Descr: Using a 4 photocells to capture a digital like signal, play tones within 2 octaves of the C major scale. #include // Pins intputs for photocells int p0Pin = 2; int p1Pin = 4; int p2Pin = 3; int p3Pin = 5; // Threshold of photocells to interpret as 0 or 1 int threshold0 = 150; int threshold1 = 180; int threshold2 = 160; int threshold3 = 160; // Duration of note int duration = 500; // Temp vairables int reading[4] = {0}; int readingVal = 0; int lastVal = 0; // Tone object Tone tone; // Notes for 2 octaves of the C major scale int notesC_Maj[] = { NOTE_C4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6 }; void setup() { Serial.begin(9600); tone.begin(8); } void loop() { Serial.println(analogRead(p0Pin)); Serial.println(analogRead(p1Pin)); Serial.println(analogRead(p2Pin)); Serial.println(analogRead(p3Pin)); reading[0] = (analogRead(p0Pin) > threshold0) ? 0 : 1; reading[1] = (analogRead(p1Pin) > threshold1) ? 0 : 1; reading[2] = (analogRead(p2Pin) > threshold2) ? 0 : 1; reading[3] = (analogRead(p3Pin) > threshold3) ? 0 : 1; Serial.print(reading[3]); Serial.print(reading[2]); Serial.print(reading[1]); Serial.println(reading[0]); // Convert binary to decimcal value readingVal = (reading[0]) + (reading[1]*2) + (reading[2]*4) + (reading[3]*8); Serial.println(readingVal); // Don't play any note for 0 if (readingVal == 0) { delay(duration); return; } lastVal = readingVal; // Play note tone.play(notesC_Maj[readingVal]); delay(duration); tone.stop(); delay(50); }