/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/library/arduino-melody-example
Library References: https://arduinogetstarted.com/tutorials/arduino-buzzer-library
This example uses a piezo buzzer:
+ plays a melody once on background each time a button is pressed
+ stops playing a melody when another button is pressed
+ without using delay() function, this is a non-blocking example
*/
#include "TinyIRReceiver.hpp"
#include <LiquidCrystal_I2C.h>
#include <ezBuzzer.h> // ezBuzzer library
#include "RTClib.h"
const int BTN_PIN = 8;
//const int STOP_BUTTON_PIN = 7;
const int BUZZER_PIN = 4;
const unsigned long ONE_SEC = 1000;
const unsigned long DEBOUNCE_TIME = 20;
bool isPlaying = false;
int lastStartButtonState = HIGH; // the previous state from the input pin
int lastStopButtonState = HIGH; // the previous state from the input pin
unsigned long prevTime = 0;
ezBuzzer buzzer(BUZZER_PIN, BUZZER_TYPE_PASSIVE, HIGH); // create ezBuzzer object: pin, type, activeLevel (PASSIVE for melody)
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
// notes in the melody:
int melody[] = {
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
NOTE_E5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};
// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:
int noteDurations[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
void checkButton() {
static uint32_t lastTime = 0;
static bool lastState = HIGH;
bool currentState = digitalRead(BTN_PIN);
uint32_t now = millis();
if (currentState != lastState && now - lastTime >= DEBOUNCE_TIME) {
if (!currentState) {
Serial.println("Button pressed!");
isPlaying = !isPlaying;
} else {
Serial.println("Button released!");
}
lastTime = now;
lastState = currentState;
}
}
void playTune() {
if (buzzer.getState() == BUZZER_IDLE) {
int length = sizeof(noteDurations) / sizeof(int);
buzzer.playMelody(melody, noteDurations, length); // playing
}
}
void showTime() {
char buffer[16];
DateTime now = rtc.now();
lcd.setCursor(4, 0);
snprintf(buffer, 16, "%2d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.print(buffer);
/*
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
//Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
*/
}
void setup() {
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
for (;;);
}
initPCIInterruptForTinyIRReceiver(); // Enables the interrupt generation on change of IR input signal
lcd.init();
lcd.backlight();
//lcd.print("Hello");
pinMode(BTN_PIN, INPUT_PULLUP);
//pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
buzzer.loop(); // MUST call the buzzer.loop() function in loop()
checkButton();
if (isPlaying) {
playTune();
} else {
if (buzzer.getState() != BUZZER_IDLE) {
buzzer.stop() ; // stop
}
}
if (millis() - prevTime >= ONE_SEC) {
showTime();
prevTime = millis();
}
if (TinyIRReceiverDecode()) {
printTinyIRReceiverResultMinimal(&Serial);
}
/*
int startButtonState = digitalRead(START_BUTTON_PIN);
int stopButtonState = digitalRead(STOP_BUTTON_PIN);
if (lastStartButtonState == HIGH && startButtonState == LOW) {
Serial.println("The START button is pressed");
if (buzzer.getState() == BUZZER_IDLE) {
int length = sizeof(noteDurations) / sizeof(int);
buzzer.playMelody(melody, noteDurations, length); // playing
}
delay(20);
lastStartButtonState = startButtonState;
}
if (lastStopButtonState == HIGH && stopButtonState == LOW) {
Serial.println("The STOP button is pressed");
if (buzzer.getState() != BUZZER_IDLE) {
buzzer.stop() ; // stop
}
lastStopButtonState = stopButtonState;
delay(20);
}
*/
}