#include <Talkie.h>
// Define the pins
#define BUTTON_PIN 2
#define SPEAKER_PIN 9
// Initialize Talkie object
Talkie voice;
// Function to speak the time
void speakTime() {
// Get current time
unsigned long currentTime = millis();
unsigned long seconds = currentTime / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
// Convert time to 12-hour format
hours = hours % 12;
if (hours == 0) {
hours = 12;
}
// Speak the time
voice.say("The time is");
voice.sayNumber(hours);
voice.say("hours");
voice.sayNumber(minutes % 60);
voice.say("minutes");
}
void setup() {
// Initialize the button pin as input
pinMode(BUTTON_PIN, INPUT);
// Initialize the speaker pin as output
pinMode(SPEAKER_PIN, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
// Initialize the Talkie library
voice.begin(SPEAKER_PIN);
}
void loop() {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
// Call the function to speak the time
speakTime();
// Add a delay to avoid multiple readings due to button bouncing
delay(1000);
}
}