Hello, I am Kristian a 16 year old student and could need some help with my school project. I am building a wireless doorbell and I am a little bit stuck right now. I have made a circuit which doesn't work as intended. I am using a IR Receiver and an IR Diode and to make a sender and receiver. The Arduino is used to make the doorbell play a melody and to register the signal from the IR Receiver. I am using this code to make a melody but my in my final code I added some few more lines.
The problem I have is that the doorbell plays the melody instantly without me pressing the button.
A little bit of help with my circuit and code would be very much appreciated.
Here is my code
#include "pitches.h"
int IrReceiver = A1;
int melody[ ] = {
NOTE_E4, NOTE_C4, NOTE_D4, NOTE_G3, 0, NOTE_G3, NOTE_D4, NOTE_E4, NOTE_C4,
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
2, 2, 2, 2, 1, 2, 2, 2, 2,
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 9; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
if (digitalRead(IrReceiver) == HIGH) {
tone(8, melody[thisNote], noteDuration);
}
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}