// Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pins
const int buzzerPin = 14;
// Frequencies of musical notes
const int c = 262;
const int cs = 277;
const int d = 294;
const int ds = 311;
const int e = 330;
const int f = 349;
const int fs = 370;
const int g = 392;
const int gs = 415;
const int a = 440;
const int as = 466;
const int b = 494;
const int c_high = 523;
const int cs_high = 554;
const int d_high = 587;
const int ds_high = 622;
const int e_high = 659;
const int f_high = 698;
const int fs_high = 740;
const int g_high = 784;
const int gs_high = 831;
const int a_high = 880;
const int as_high = 932;
const int b_high = 988;
int count = 0;
LiquidCrystal_I2C lcd(0x27,16,2);
// left = 4, right = 5, select = 13, cycle = 16
const byte buttonPins[] = {4, 5, 13, 16};
// Function to play a frequency on the buzzer for a given duration
void playFrequency(int frequency, int duration) {
ledcWriteTone(buzzerPin, frequency); // Start the tone
delay(duration); // Wait for the specified duration
ledcWriteTone(buzzerPin, 0); // Stop the buzzer
}
void whole_note(int frequency){
playFrequency(frequency, 1200);
delay(50);
}
void half_note(int frequency){
playFrequency(frequency, 600);
delay(50);
}
void note(int frequency){
playFrequency(frequency, 300);
delay(50);
}
void eight_note(int frequency){
playFrequency(frequency, 150);
delay(50);
}
void sixteen_note(int frequency){
playFrequency(frequency, 75);
delay(50);
}
void song(){
note(e);
note(f);
note(g);
note(c);
note(d);
note(e);
note(g);
note(a);
note(b);
note(f);
note(a);
note(b);
note(c);
delay(100);
note(e);
note(f);
note(g);
note(c);
note(d);
note(e);
note(g);
note(g);
note(g);
note(e);
note(d);
note(g);
note(e);
note(d);
note(g);
note(e);
note(d);
}
/**
Waits until the user pressed one of the buttons,
and returns the index of that button
*/
byte readButtons() {
for (byte i = 0; i < 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
Serial.print(buttonPin);
Serial.println(" = LOW");
note(fs);
}
}
}
void setup() {
for (byte i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
Serial.begin(115200);
Serial.println("Setup Stage");
readButtons();
ledcAttach(buzzerPin, 2000, 8); // Set up the PWM pin
lcd.init();
lcd.backlight();
lcd.print("Hello, world!");
delay(3000);
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0); // Sets the cursor position to the first row and first column (0, 0).
lcd.print("COUNT: ");
lcd.print(count); // Prints the current value of the count variable.
readButtons();
delay(1000);
readButtons();
count++; // Increments the counter by 1.
//song();
//delay(1000);
}
◀️
▶️
Select
Cycle Screen