/*
Mega_Radon — Discord | Arduino | #coding-help 12/31/23 at 4:51 AM
*/
#include "pitches.h"
const int BTN_PIN = 4;
const int LED_PINS[] = {
11, 10, 9, 8, 7, 6, 5
};
const int BUZZ_PIN = 12;
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
int noteLedPin[] = {
0, 4, 4, 5, 4, -1, 6, 0
};
void playTune() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; 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 (noteLedPin[thisNote] < 0) {
// turn all the LEDs off
for (int i = 0; i < 7; i++) {
digitalWrite(LED_PINS[i], LOW);
}
tone(BUZZ_PIN, melody[thisNote], noteDuration);
} else {
digitalWrite(LED_PINS[noteLedPin[thisNote]], HIGH);
tone(BUZZ_PIN, melody[thisNote], noteDuration);
digitalWrite(LED_PINS[noteLedPin[thisNote]], LOW);
}
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;// was 1.3
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(BUZZ_PIN);
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(BUZZ_PIN, OUTPUT);
for (int leds = 0; leds < 7; leds++) {
pinMode(LED_PINS[leds], OUTPUT);
}
Serial.println("Ready...");
}
void loop() {
if (digitalRead(BTN_PIN) == LOW) {
Serial.println("Playing tune");
playTune();
}
}
C __ D __ E __ F __ G __ A __ B