Pages: [1]   Go Down
Author Topic: Close Encounters Door Bell  (Read 258 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All,

First post on these forums and new to the Arduino so please be kind.

I'm making a door bell as a simple first project that plays the notes from close encounters.

I've got it working functionally, but musically it sounds wrong.

Anyone willing to test out my code and tell me what I'm doing wrong?

Code:
/*
 *  Alternating switch
 */

int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the last button state

 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_G4, NOTE_A4,NOTE_F4, NOTE_F3, NOTE_C3};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  8, 4, 2, 4, 1 };

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}


void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val

  if (val != buttonState) {          // the button state has changed!
    if (val == HIGH) {                // check if the button is pressed
      Serial.println("Button just pressed");
        for (int thisNote = 0; thisNote < 5; thisNote++) {
   
int noteDuration = 1000/noteDurations[thisNote];
int pauseBetweenNotes = noteDuration * 1.30;
   
    tone(8, melody[thisNote],noteDuration);
    delay(pauseBetweenNotes);
    noTone(8);
  }
    } else {                         // the button is -not- pressed...
      Serial.println("Button just released");
    }
  }

  buttonState = val;                 // save the new state in our variable
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

NOTE_G4, NOTE_A4,NOTE_F4, NOTE_F3, NOTE_C3

Those don't look like the right notes.

Google "close encounters theme" or similar to find the correct notes.
Find a clip of where the guy in charge is telling the guy on the keyboard what to play, I recall him saying "play (whatever the first note was, now up a major third, down a major fifth, down a major third, up 2 steps", something along those lines.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For example, this
http://www.musicnotes.com/sheetmusic/mtdFPE.asp?ppn=MN0017597
shows them as
A-B-G-G (octave lower) -D
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23597
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Which sounds correct using this
http://virtualpiano.net/
or
http://www.play-piano.org/play_online_piano_piano.html
as examples.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: