From 0e30ac9d5ab23e599e381888bb1fb2a9ac01013e Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Tue, 20 Sep 2022 13:09:20 -0500 Subject: [PATCH] Touch sensor use version works --- src/main.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 006e501..dc4bb40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,9 @@ const char* ssid = "JoNelsDarlings"; const char* password = "Paris2016"; const int BUBBLE_GPIO = 4; boolean isBubbleFirstContact = true; -unsigned long bubbleFreqSec = 0; +double bubbleFreqSec = 0; unsigned long lastMillis = millis(); +unsigned int bVal; AsyncWebServer server(80); @@ -65,7 +66,7 @@ void setup(void) { "Hi! This is the Wine-Sensor. Last bubble frequency: every " + String(bubbleFreqSec) + " Seconds. [" + String(millis()) + "/" + String(lastMillis) + "/" + String(isBubbleFirstContact) + "/" + - String(digitalRead(BUBBLE_GPIO)) + + String(bVal) + "]" ""); @@ -74,25 +75,30 @@ void setup(void) { AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA server.begin(); Serial.println("HTTP server started"); - - pinMode(BUBBLE_GPIO, INPUT_PULLUP); } void loop(void) { - digitalWrite(LED_BUILTIN, digitalRead(BUBBLE_GPIO)); + bVal = touchRead(BUBBLE_GPIO); + if (bVal == 0) { + digitalWrite(BUILTIN_LED, HIGH); + } else { + digitalWrite(BUILTIN_LED, LOW); + } + // writeToOled("* " + String(bVal)); // This runs real fast, so make sure that only the "first contact" is measured - if ((digitalRead(BUBBLE_GPIO) == LOW) && isBubbleFirstContact) { + if ((bVal == 0) && (isBubbleFirstContact)) { if (millis() > lastMillis) { // don't measure if we had an overflow... - bubbleFreqSec = (millis() - lastMillis) / 1000; - writeToOled("Freq: " + String(bubbleFreqSec)); + bubbleFreqSec = (double)(millis() - lastMillis) / 1000; } lastMillis = millis(); isBubbleFirstContact = false; - } else if ((digitalRead(BUBBLE_GPIO) == HIGH)) { + } else if (0 != bVal) { isBubbleFirstContact = true; } + writeToOled("F:" + String(bubbleFreqSec, 2) + "\n[" + String(bVal) + "]"); + // need to slow it down a bit to prevent fluctuating state cahange - delay(100); + delay(10); }