From 856ada13e89188bedea2c5b904f7a6e53d5434f2 Mon Sep 17 00:00:00 2001 From: mattbk Date: Sat, 26 Aug 2023 20:55:28 -0500 Subject: [PATCH] Add simple timer example. --- vulpes/platformio.ini | 1 + vulpes/src/main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/vulpes/platformio.ini b/vulpes/platformio.ini index a299f08..ed7a903 100644 --- a/vulpes/platformio.ini +++ b/vulpes/platformio.ini @@ -16,3 +16,4 @@ monitor_speed = 115200 lib_deps = me-no-dev/AsyncTCP@^1.1.1 me-no-dev/ESP Async WebServer@^1.2.3 + contrem/arduino-timer@^3.0.1 diff --git a/vulpes/src/main.cpp b/vulpes/src/main.cpp index 15bcf5d..b750e28 100644 --- a/vulpes/src/main.cpp +++ b/vulpes/src/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include // download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install. #include @@ -64,6 +65,21 @@ String output27State = "off"; const int output26 = 26; const int output27 = 27; +// Timers +auto timer = timer_create_default(); +auto time_until_start = timer_create_default(); + +// Example from https://github.com/contrem/arduino-timer#examples +bool toggle_led(void *) { + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED + return true; // keep timer active? true +} + +// defaults +// String yourInputString; +// int yourInputInt; +// float yourInputFloat; + void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } @@ -118,6 +134,11 @@ String processor(const String& var){ void setup() { Serial.begin(115200); + // Timer example, blink main LED + pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT + // call the toggle_led function every 1000 millis (1 second) + timer.every(1000, toggle_led); + // Initialize the output variables as outputs pinMode(output26, OUTPUT); pinMode(output27, OUTPUT); @@ -138,6 +159,11 @@ void setup() { } #endif + // Read in existing data +// String yourInputString = readFile(SPIFFS, "/inputString.txt"); +// int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt(); +// float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat(); + WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() != WL_CONNECTED) { @@ -176,14 +202,25 @@ void setup() { } Serial.println(inputMessage); request->send(200, "text/plain", inputMessage); + + // // Update data from files + // String yourInputString = readFile(SPIFFS, "/inputString.txt"); + // int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt(); + // float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat(); }); server.onNotFound(notFound); server.begin(); } void loop() { - // Read seconds from file + // Timers + time_until_start.tick(); + timer.tick(); + + String yourInputString = readFile(SPIFFS, "/inputString.txt"); int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt(); + float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat(); + // Blink LED according to seconds entered if (yourInputInt > 0) { Serial.println("GPIO 26 on"); @@ -195,7 +232,11 @@ void loop() { output26State = "off"; digitalWrite(output26, LOW); delay(yourInputInt * 1000); - } + } else { + output26State = "off"; + } + + // // To access your stored values on inputString, inputInt, inputFloat // String yourInputString = readFile(SPIFFS, "/inputString.txt");