/* PIANO */
// Import the CapacitiveSensor Library.
#include <CapacitiveSensor.h>
int speaker = 10;
int LED[6] = {A5, A4, A3, A2, A1, A0};
//int song[49] = {1,2,3,1,3,1,3,2,3,4,3,2,4,3,4,5,3,5,3,5,4,5,6,5,4,6,5,1,2,3,4,5,6,2,3,4,5,6,7,3,4,5,6,7,6,4,7,5,1};
//int song[24] = {5, 3, 3, 4, 2, 2, 1, 2, 3, 4, 5, 5, 5, 5, 3, 3, 4, 2, 2, 1, 3, 5, 5, 1};
int song[24] = {5,6,5,4,3,4,5,2,3,4,3,4,5,5,6,5,4,3,4,5,2,5,3,1};
int state = 0;
int currNote = 0;
int cek = 0;
// Set the Send Pin & Receive Pin.
CapacitiveSensor cs_2_3 = CapacitiveSensor(2,3); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_5 = CapacitiveSensor(2,5); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_6 = CapacitiveSensor(2,6); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_7 = CapacitiveSensor(2,7); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_8 = CapacitiveSensor(2,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_2_9 = CapacitiveSensor(2,9); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
void setup(){
cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF); //turn off autocalibrate on channel 1 - just as an example
// Arduino start communicate with computer.
Serial.begin(9600);
pinMode(serPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
for(int i=0; i<6; i++)
pinMode(LED[i], OUTPUT);
ledCheck();
}
void loop(){
// Set a timer.
long start = millis();
// Set the sensitivity of the sensors.
long total1 = cs_2_3.capacitiveSensor(30);
long total2 = cs_2_4.capacitiveSensor(30);
long total3 = cs_2_5.capacitiveSensor(30);
long total4 = cs_2_6.capacitiveSensor(30);
long total5 = cs_2_7.capacitiveSensor(30);
long total6 = cs_2_8.capacitiveSensor(30);
long total7 = cs_2_9.capacitiveSensor(30);
//Turn on desired output on LED
turnLED(state);
// When hand is touched the sensor, the speaker will produce a tone.
// I set a threshold for it, so that the sensor won't be too sensitive.
if (total1 > 150) currNote = 1;
if (total2 > 150) currNote = 2;
if (total3 > 150) currNote = 3;
if (total4 > 150) currNote = 4;
if (total5 > 150) currNote = 5;
if (total6 > 150) currNote = 6;
if (total7 > 150) currNote = 7;
if (total1<=150 & total2<=150 & total3<=150 & total4<=150 & total5<=150 & total6<=150 & total7<=150)currNote = 8;
// When hand didn't touch on it, no tone is produced.
if (total1<=150 & total2<=150 & total3<=150 & total4<=150 & total5<=150 & total6<=150 & total7<=150)
noTone(speaker);
if (checkSong(state)==1) {
state++;
}
if (state == 24) {
makeSound(currNote);
state = 0;
Serial.print("WIN");
delay (500);
ledCheck();
}
delay(10); // arbitrary delay to limit data to serial port
makeSound(currNote);
}
void ledCheck(){
int counter = 0;
while(counter!=6){
digitalWrite(LED[counter], HIGH);
for(int j=0; j<6; j++){
if(counter != j)
digitalWrite(LED[j], LOW);
}
counter++;
delay(100);
}
counter--;
while(counter!=-1){
digitalWrite(LED[counter], HIGH);
for(int j=0; j<6; j++){
if(counter != j)
digitalWrite(LED[j], LOW);
}
counter--;
delay(100);
}
for(int i=0; i<3; i++){
for(int i=0; i<6; i++){
digitalWrite(LED[i], HIGH);
}
delay(200);
for(int i=0; i<6; i++){
digitalWrite(LED[i], LOW);
}
delay(200);
}
}
void makeSound(int note) {
// When hand is touched the sensor, the speaker will produce a tone.
// I set a threshold for it, so that the sensor won't be too sensitive.
if (note == 1) tone(speaker,523);
if (note == 2) tone(speaker,587);
if (note == 3) tone(speaker,659);
if (note == 4) tone(speaker,698);
if (note == 5) tone(speaker,784);
if (note == 6) tone(speaker,880);
if (note == 7) tone(speaker,988);
if (note == 8) noTone(speaker);
}
int checkSong (int state) {
if (currNote == song[state]) {
return 1;
}
else {
return 0;
}
}
void turnLED (int state) {
for(int i=0; i<6; i++){
if(i == (song[state]-1))
digitalWrite(LED[i], HIGH);
else
digitalWrite(LED[i],LOW);
}
}
Tidak ada komentar:
Posting Komentar