#define IR_SEND_PIN D10 // Has to be before the include #include #define RECEIVER_PIN D6 #define BUZZER_PIN D8 //#define IR_PHOTOTRANSISTOR_PIN D5 // Not used //#define EXTRA_PIN D3 // Not used #include "pitches.h" // Has the pitches for the notes #define NOTE_COUNT 41 // Notes in the melody (it's a small world): int melody[NOTE_COUNT] = { NOTE_E3, NOTE_F3, NOTE_G3, NOTE_E4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_C4, NOTE_B3, 0, NOTE_B3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_D4, NOTE_B3, NOTE_C4, NOTE_B3, NOTE_A3, NOTE_G3, 0, NOTE_G3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_A3, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_G3, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4 }; // Note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[NOTE_COUNT] = { 8, 8, 4, 4, 4, 8, 8, 4, 4, 32, 4, 8, 8, 4, 4, 4, 8, 8, 4, 4, 32, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 4, 4, 4, 4, 1 }; const uint32_t check_signal = 0xE3694208; // Just some arbitrary signal const uint8_t count_threshold = 5; // How many failed signals mean button press uint8_t confirm_count = 0; // How many failed signals have happened recently void setup() { IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK); // Start the receiver Serial.begin(9600); } void loop() { Serial.println("Observing..."); Serial.print(confirm_count); Serial.print(count_threshold); IrSender.sendNECRaw(check_signal); // send an IR signal delay(10); if (IrReceiver.decode()) { // Something received Serial.println("Received"); if (IrReceiver.decodedIRData.decodedRawData == check_signal) { Serial.println("Signal through"); // Signal received so the button wasn't pressed confirm_count = 0; } IrReceiver.resume(); // Enable receiving of the next value } confirm_count++; if (confirm_count == count_threshold) { // There has been enough failed signals to conclude button press playSound(); confirm_count = 0; // reset the counter } } void playSound() { // From https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody // which is in the public domain. for (int thisNote = 0; thisNote < NOTE_COUNT; 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]; tone(BUZZER_PIN, 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.3; delay(pauseBetweenNotes); // Stop the tone playing: noTone(BUZZER_PIN); } Serial.println("Knock knock"); }