Skip to content

Instantly share code, notes, and snippets.

@venetanji
Created December 16, 2021 12:03
Show Gist options
  • Save venetanji/975253eeef7c1a3beada97bf38223fc2 to your computer and use it in GitHub Desktop.
Save venetanji/975253eeef7c1a3beada97bf38223fc2 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include "SPIFFS.h"
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
int mticks = 1; // ticks per minute
int fmticks = mticks * 5; //ticks per 5 minutes
int hticks = fmticks * 12; //ticks per hour
int lticks = hticks * 12; //ticks per loop 12 hours
String inputString = ""; // a String to hold incoming data
AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2S *out;
int clockh = 6;
int clockm = 30;
int ticks = 390;
int lightonfor = 1;
char filen[10];
const int buttonPin = 5;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long lastHalftick = 0;
const unsigned long debounceDelay = 200;
const unsigned long halfticktime = 30000;
bool halftick = false;
void setup()
{
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
pinMode(buttonPin, INPUT);
delay(1000);
SPIFFS.begin();
audioLogger = &Serial;
file = new AudioFileSourceSPIFFS("/tp00.mp3");
mp3 = new AudioGeneratorMP3();
out = new AudioOutputI2S();
mp3->begin(file, out);
}
void loop()
{
if (mp3->isRunning()) {
if (!mp3->loop()) {
mp3->stop();
}
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ticks++;
Serial.print("TICK ");
Serial.println(ticks);
updateClock();
lastHalftick = millis();
halftick = true;
}
}
}
if (halftick && ((millis() - lastHalftick) > halfticktime)) {
ticks++;
Serial.print("TACK ");
Serial.println(ticks);
updateClock();
halftick = false;
}
lastButtonState = reading;
}
void updateClock() {
if (ticks % mticks == 0) { // if a minute has passed
clockm++; // add a minute
if (clockm == 60) { // an hour has passed
clockm = 0; //
clockh--; // clock hand hour goes backwards
clockh = (clockh + 12) % 12; // fold hours
// play audio file
sprintf(filen, "/tp%02d.mp3", clockh);
file->open(filen);
mp3->begin(file, out);
Serial.print("on ");
Serial.println(clockh);
}
}
if (ticks % fmticks == 0) { //five minutes
Serial.print("on ");
Serial.println(clockm/5); // turn on light
}
if (ticks % fmticks == lightonfor) {
Serial.println("off");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment