## Toccata CalaBach

The concept behind this pumpkin was inspired by the Toccata in Fugue D Minor by Bach. The teeth of the pumpkin are white keys/ switches, that when pressed to the bottom surface close the circuit and play a note through a piezo buzzer. If the first and the last key are both pressed at the same time, a short introduction of the Toccata in Fugue plays. The light of the pumpkin turns on at night, once a photoresistor reads values that are low enough to mean darkness. A little stuffed dead guy accompanies Toccata CalaBach, representing Johann Sebastian.

// CODE:

int photo= 0;
int led = 2;
int key1 = 3;
int key2 = 4;
int key3 = 5;
int key4 = 6;
int key5 = 7;
int key6 = 8;
int speakerOut = 9;

int debounce = 10;

int state= LOW;
int lastkeyvalue = LOW; // we start, assuming no motion detected
int val= 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;
// variable for reading the key status
//******************************************************************************
// TONES ==========================================
// Start by defining the relationship between
// note, period, & frequency.
int c= 3830; // 261 Hz
int d= 3400; // 294 Hz
int e= 3038; // 329 Hz
int f= 2864; // 349 Hz
int g= 2550; // 392 Hz
int a= 2272; // 440 Hz
int b= 2028; // 493 Hz
int C= 1912; // 523 Hz
// Define a special note, ‘R’, to represent a rest
int O= 0;

// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[],
// which sets each note’s relative length (higher #, longer note)
int melody[] = {
a, g, a, O, g, f, e, d, 3615, d };
int beats[] = {
8, 8, 64, 64, 16, 16, 16, 16, 64, 64 };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;
//******************************************************************************

void setup() {
pinMode(speakerOut, OUTPUT);
pinMode(photo, INPUT); //photoresistor Analog
pinMode(key1, INPUT);
pinMode(key2, INPUT);
pinMode(key3, INPUT);
pinMode(key4, INPUT);
pinMode(key5, INPUT);
pinMode(key6, INPUT);
pinMode(led, OUTPUT);

Serial.begin(9600);
}

void loop(){
if (val1 == HIGH && val6 == HIGH){
playSong();
}else{
keyTones();
}
nightLight();

}

void keyTones(){
Serial.println(“val1”);
if (val1 == HIGH){
tone_=c;
duration= 640000;
playTone();
Serial.println(“YES = 1”);
}
if (val2 == HIGH){
tone_=d;
duration= 640000;
playTone();
Serial.println(“YES = 2”);
}
if (val3 == HIGH){
tone_=e;
duration= 640000;
playTone();
Serial.println(“YES = 3”);
}

if (val4 == HIGH){
tone_=f;
duration= 640000;
playTone();
Serial.println(“YES = 4”);
}
if (val5 == HIGH){
tone_=g;
duration= 640000;
playTone();
Serial.println(“YES = 5”);
}
if (val6 == HIGH){
tone_=a;
duration= 640000;
playTone();
Serial.println(“YES = 6”);
}
else {
digitalWrite(speakerOut, LOW);
}
if (val1 != lastkeyvalue ){
delay(debounce);
val1 = key1;
}
lastkeyvalue= val1, val2, val3, val4, val5, val6;

}

void nightLight(){
// Serial.print(val);
Serial.print(10, BYTE);
delay(10);

if (val<400){
digitalWrite(led, HIGH);
}else{
digitalWrite(led, LOW);
}

}

void playSong() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i 0) { // if this isn’t a Rest beat, while the tone has
// played less long than ‘duration’, pulse speaker HIGH and LOW
while (elapsed_time < duration) {

digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone_ / 2);

// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone_ / 2);

// Keep track of how long we pulsed
elapsed_time += (tone_);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}