aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arduino_sketch/fuzzy_alarm_clock_esp32/config.h14
-rw-r--r--arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino82
2 files changed, 87 insertions, 9 deletions
diff --git a/arduino_sketch/fuzzy_alarm_clock_esp32/config.h b/arduino_sketch/fuzzy_alarm_clock_esp32/config.h
index 1e505b2..ae8ca57 100644
--- a/arduino_sketch/fuzzy_alarm_clock_esp32/config.h
+++ b/arduino_sketch/fuzzy_alarm_clock_esp32/config.h
@@ -10,6 +10,20 @@
// See https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
#define TZ "CET-1CEST,M3.5.0,M10.5.0/3"
+// Alarms
+#define ALARM_PIN 1
+// Sound of the alarm: frequencies, and NFREQ should be the length of freq
+#define NFREQ 9
+int freq[] = { 255, 192, 128, 96, 64, 96, 128, 192, 255 };
+
+// Button
+#define BTN_PIN 2
+
+// RGB LED
+#define LED_R_PIN 7
+#define LED_G_PIN 8
+#define LED_B_PIN 10
+
// Create the file local_config.h in this directory and override all of the
// #defines you need
#include "local_config.h"
diff --git a/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino b/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino
index 297b938..b5d725a 100644
--- a/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino
+++ b/arduino_sketch/fuzzy_alarm_clock_esp32/fuzzy_alarm_clock_esp32.ino
@@ -7,8 +7,17 @@
#include "time.h"
#include "sntp.h"
+#include <Colours.h>
+
WiFiClient wifiClient;
+Colours colours(LED_R_PIN, LED_G_PIN, LED_B_PIN);
+
+bool ringing = false; // the alarm should be ringing
+bool pressed = false; // the button has been pressed
+
+hw_timer_t * timer = NULL;
+
void connect_wifi() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("WiFi ");
@@ -26,27 +35,82 @@ void connect_wifi() {
}
}
Serial.println(" connected");
+ colours.writeHSV(180, 255, 8);
+}
+
+void ring_alarm() {
+ for (int i=0; i<NFREQ ; i++) {
+ tone(ALARM_PIN, 100000 / freq[i], 100);
+ }
+}
+
+void button_pressed() {
+ pressed = true;
+}
+
+void check_status() {
+#ifdef DEBUG
+ Serial.print("Checking the statuses: ");
+ Serial.print(ringing);
+ Serial.print(pressed);
+ Serial.println();
+#endif
+ if ( ringing ) {
+ ring_alarm();
+ }
+ if ( pressed ) {
+ ringing = false;
+ }
+ struct tm timeinfo;
+ if(!getLocalTime(&timeinfo)){
+ Serial.println("No time available (yet)");
+ return;
+ }
+ Serial.print("Time is: ");
+ Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
+ Serial.println(&timeinfo);
}
void setup() {
Serial.begin(115200);
-#if defined(DEBUG)
+#ifdef DEBUG
Serial.setDebugOutput(true);
#endif
+ Serial.println("Starting setup");
+ pinMode(ALARM_PIN, OUTPUT);
+ pinMode(BTN_PIN, INPUT_PULLUP);
+
+ colours.writeHSV(0, 255, 8);
configTzTime(TZ, NTP_SERV_1, NTP_SERV_2);
+ // Connect to the wifi and wait 5 seconds for ntp to have configured the
+ // time
connect_wifi();
+ delay(5000);
+
+ attachInterrupt(digitalPinToInterrupt(BTN_PIN), button_pressed, HIGH);
+
+#ifdef DEBUG
+ Serial.print("pin to interrupt");
+ Serial.println(digitalPinToInterrupt(BTN_PIN));
+#endif
+
+ // Enable a timer to check for the status every second
+ /*
+ timer = timerBegin(1, 80, true);
+ timerAttachInterrupt(timer, &check_status, true);
+ timerAlarmWrite(timer, 1000000, true);
+ timerAlarmEnable(timer);
+ */
+
+ // temporary set ringing to true so that we do something
+ ringing = true;
}
void loop() {
- delay(5000);
- struct tm timeinfo;
- if(!getLocalTime(&timeinfo)){
- Serial.println("No time available (yet)");
- return;
- }
- Serial.print("Time is: ");
- Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
+ check_status();
+ delay(1000);
+
}