I`m coding to make sure that the piezo buzzer makes sounds(do, re, me, fa...) by controlling the IR remote controller. However, it only works when I press the button 1(Do) first. The other buttons do not work...
What I want is to make the piezo buzzer to make Do, Re, Me.....Si sounds sequentially, pressing the button 1 to 7. And the buzzer must make the sound for 1 second.
Can someone help me please?
This code uses an IR remote to play musical notes (C5 to B5) on a piezo speaker, corresponding to buttons pressed on the remote. It is designed to receive signals via an IR receiver, identify the button pressed, and map it to a specific musical note frequency.
You need to install the library "IRremote" by Armin 4.4.1
#include <IRremote.h>
#define IR_RECEIVE_PIN 2 // IR remote receiver pin
#define SPEAKER_PIN 8 // Piezo speaker pin
// Frequencies for C5, D5, E5, F5, G5, A5, B5 notes
const int notes[] = {523, 587, 659, 698, 784, 880, 988}; // C5, D5, E5, F5, G5, A5, B5
// Signal values for remote control buttons (as obtained from the serial monitor)
unsigned long buttonValues[] = {
0xBA45FF00, // Button '1' -> C5 (Do)
0xB946FF00, // Button '2' -> D5 (Re)
0xB847FF00, // Button '3' -> E5 (Mi)
0xBB44FF00, // Button '4' -> F5 (Fa)
0xBF40FF00, // Button '5' -> G5 (Sol)
0xBC43FF00, // Button '6' -> A5 (La)
0xF807FF00 // Button '7' -> B5 (Si)
};
void setup() {
pinMode(SPEAKER_PIN, OUTPUT); // Set the piezo speaker pin as output
Serial.begin(9600); // Start serial communication
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Initialize the IR remote receiver
Serial.println("Ready to receive IR signals...");
}
void loop() {
if (IrReceiver.decode()) { // IR remote signal detected
unsigned long value = IrReceiver.decodedIRData.decodedRawData; // Store the received signal value
Serial.print("Received: ");
Serial.println(value, HEX); // Print the received signal value in hexadecimal format
// Check the button value and play the corresponding note
for (int i = 0; i < 7; i++) {
if (value == buttonValues[i]) { // If the received signal matches a value in the array
playTone(i); // Play the corresponding note
break; // Stop searching after finding a match
}
}
IrReceiver.resume(); // Prepare the IR receiver for the next signal
}
}
// Function to play a specific note
void playTone(int index) {
tone(SPEAKER_PIN, notes[index], 500); // Play the frequency for 500ms
delay(500); // Wait for 0.5 seconds
noTone(SPEAKER_PIN); // Stop the sound
}