Saturday, August 5, 2017

Tap for Tune

Tap for Tune

Play a tune every time the speaker is tapped. Uses analog input A0 to both detect and play the tune.



--------------------------- Copy Code Below
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330

// notes in the melody: shave and and a haircut
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
byte tapCount = 0;
long int tapTime = millis();
void setup()
{
}
void loop()
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);

 //detect a tap
    if(sensorValue > 10)
    {
    delay(100);
    pinMode(A0, OUTPUT);
    playTune();
    pinMode(A0, INPUT);
    delay(100);
    }
}
void playTune()
{
for (int Note = 0; Note < 8; Note++)
{
int noteDuration = 1000 / noteDurations[Note];
tone(A0, melody[Note], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(A0);}
}

--------------------------- Copy Code Below
Things to do:

1) Use the tap detection portion of the program to turn devices on and off
2) Create a combination lock using tap detection
3) Build a reaction detector that measure time between taps
4) Use the detector as a target game for airsoft gun or ping pong balls
5) Use speaker, with small weight attached to cone, to detect acceleration by measuring max signal from speaker
6) Use as a user interface for menu of options by tapping menu item number

No comments:

Post a Comment