Jaws Theme

Submitted by jmscott212 on Wed, 03/06/2013 - 00:01

Description:

The 2-note Jaws Theme plays on the piezo speaker; the temp is controlled by the POT in an attempt to simulate Jaws getting closer.  I tried to get it to where  when the tempo got really fast, a sound file of a scream would play, and an image of Anne Hathaway getting eaten by a shark would pop up on my screen.  However, I couldn't quite figure out how to do this.  

Parts:

Breadboard

Piezo Speaker

Pot

Image

Code

(adapted from M-Craig)

// sharks are coming

int speakerPin = 7;
int potPin = A0;

int length = 2; // the number of notes
char notes[] = "cd"; // jaws
                                                                             
int beats[] = { 1, 1};  // number of beats per note
                                                                                          
int potVal = 0;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B'};  
  int tones[] = { 3830, 3400, 3038, 2864, 2550, 2272, 2028, 1915, 1700, 1519, 1430, 1275, 1136, 1014};

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

void setup() {
  Serial.begin(9600);
  pinMode(speakerPin, OUTPUT);
}

void loop() {    // plays the melody, setting the tempo to potVal
  Serial.println(potVal);
  potVal = analogRead(potPin);
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * potVal / 5); // rest
    } else {
      playNote(notes[i], beats[i] * potVal);
    }

    // pause between notes
    delay(potVal / 10);
  }
}

 

 

jaws copy.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.