/*FAB TINY-PIANO
Wilhelm Schütze - Fab Academy 2015 */

 

#define keyboard_AnalogInput 2 //INPUT PIN//

#define btn1 1
#define btn2 2
#define btn3 3
#define btn4 4
#define btnNONE 0

int variable= 0;
int buttonPressed;
const int speakerPin = 8;
const int ledPin = 3; // LED PIN //

void playTone(int tono, int duration) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tono);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tono);
}

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 23, 20, 17, 15, 12, 11, 10, 9 };

// Plays the note corresponding to its name //
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}

void loop() {

buttonPressed = read_keyboard();
if (buttonPressed != btnNONE)
{
}
}
int read_keyboard(){

variable = analogRead(keyboard_AnalogInput);

if (variable > 1000) return btnNONE;

/*NOTE CONFIGURATION
PlayNote(Note,Duration)
*/


playNote('c',100); digitalWrite(ledPin, HIGH);digitalWrite(ledPin, LOW); if (variable < 194) return btn1;
playNote('d',100); digitalWrite(ledPin, HIGH);digitalWrite(ledPin, LOW); if (variable < 380) return btn2;
playNote('e',100); digitalWrite(ledPin, HIGH);digitalWrite(ledPin, LOW); if (variable < 555) return btn3;
playNote('f',100); digitalWrite(ledPin, HIGH);digitalWrite(ledPin, LOW); if (variable < 790) return btn4;

}