Use Arduino to create music playback + flash instruments with the rhythm
1. Experimental Equipment
Arduino development board, a breadboard, some Dupont wires, some LED lights, some 220 ohm resistors, and a buzzer.
2. Experimental operation
- Connect the positive poles of the six LED lights to ports 2-7 of the I/O interface of the Arduino board in sequence,
- Connect the negative pole of the power supply to a 220 ohm resistor.
- The overall negative electrode is connected to the GND interface of the arduino board.
- The positive pole of the buzzer is connected to port 8 of the I/O interface of the arduino board, and the negative pole is connected to GND.
3. Code implementation
int buzzer=8; //Set the number that controls the buzzer. Set the number that controls the buzzer. IO pin
int BASE = 2; //The I/O pin connected to the first LED
int NUM = 6; //Total number of LEDs
int song[] = {
/* Children's song "Little Star" */
277,277,415,415,466,466,415,
370,370,330,330,311,311,277,
415,415,370,370,330,330,311,
415,415,370,370,330,330,311,
277,277,415,415,466,466,415,
370,370,330,330,311,311,277,
415,415,370,370,330,330,311,
415,415,370,370,330,330,311,
277,277,415,415,466,466,415,
370,370,330,330,311,311,277,
};
void setup() {
for (int thisNote = 0; thisNote <154; thisNote++)
{
int noteDuration = 1000/noteDurations[thisNote]; // Calculate the time of each beat. Taking one beat as one second as an example, a quarter beat is 1000/4 milliseconds and an eighth beat is 1000/8 milliseconds.
tone(8, song[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.10; // The pause interval between each note is preferably 130% of the note
delay(pauseBetweenNotes);
noTone(8);
}
for (int i = BASE;i < BASE + NUM; i ++)
{
pinMode(i, OUTPUT); //Set the number. Set the number. The I/O pin is output. The pin is output.
}
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, LOW); //Set the number. Set the digital I/O pin output to "Low", that is, gradually turn off the light.
delay(200); //delay
}
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, HIGH); //Set the digital I/O pin output to "high", that is, gradually turn on the light
delay(200); //delay
}
}
void loop() {
setup();
}