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?
/*
* 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
}