From 3b37b04678dbd2b5bc5a836456f75f22374e3d5d Mon Sep 17 00:00:00 2001 From: Brandon Satrom Date: Tue, 26 Feb 2019 16:00:16 -0600 Subject: [PATCH 1/2] added fermentation variables; closes #21 --- .../src/brew-buddy-firmware.cpp | 62 ++++++++++++++----- .../src/brew-buddy-firmware.ino | 61 +++++++++++++----- 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/brew-buddy-firmware/src/brew-buddy-firmware.cpp b/brew-buddy-firmware/src/brew-buddy-firmware.cpp index 995aabe..fda3043 100644 --- a/brew-buddy-firmware/src/brew-buddy-firmware.cpp +++ b/brew-buddy-firmware/src/brew-buddy-firmware.cpp @@ -17,6 +17,7 @@ // App Version Constant void setup(); void loop(); +void resetFermentationVariables(); void activateBrewStage(); int setBrewMode(String command); void printSplash(); @@ -89,8 +90,18 @@ const uint8_t lowTemp = 70; const uint8_t highTemp = 220; //Brew Stage Variables -bool isBrewing = false; +bool isBrewingMode = false; +bool isFermentationMode = false; + +//Variables for fermentation detection bool isFermenting = false; +unsigned long fermentationModeStartTime = 0; +unsigned long fermentationStartTime = 0; + +// Variables for fermentation rate +long fermentationRate = 0; // knocks per ms +int knocksDetected = 0; +unsigned long lastKnock = 0; String brewStage; String brewId; @@ -133,21 +144,33 @@ void setup() void loop() { - if (isFermenting) + if (isFermentationMode) { int16_t knockVal = analogRead(KNOCK_PIN) / 16; if (knockVal >= 6) { Serial.printlnf("Knock Val: %d", knockVal); + lastKnock = millis(); - printSubheadingLine("Fermentation detected!"); - - waitUntil(Particle.connected); - Particle.publish("fermentation-value", String(knockVal)); + if (!isFermenting) + { + isFermenting = true; + + clearScreen(); + tft.setCursor(0, 140); + printSubheadingLine("Fermentation started..."); + // TODO: print delta btw mode start and fermentation start + waitUntil(Particle.connected); + Particle.publish("fermentation/state", "start"); + } + else + { + // TODO: Display fermentation rate + } } } - else if (isBrewing) + else if (isBrewingMode) { unsigned long currentMillis = millis(); @@ -183,6 +206,15 @@ void loop() } } +void resetFermentationVariables() +{ + isFermenting = false; + fermentationModeStartTime = 0; + fermentationStartTime = 0; + fermentationRate = 0; + lastKnock = 0; +} + void activateBrewStage() { startTime = millis(); @@ -214,10 +246,10 @@ int setBrewMode(String command) brewStage = commands[2]; brewId = commands[1]; - if (commands[0] == "brew" && !isBrewing) + if (commands[0] == "brew" && !isBrewingMode) { - isBrewing = true; - isFermenting = false; + isBrewingMode = true; + isFermentationMode = false; clearScreen(); activateBrewStage(); @@ -226,8 +258,9 @@ int setBrewMode(String command) } else if (commands[0] == "ferment") { - isBrewing = false; - isFermenting = true; + isBrewingMode = false; + isFermentationMode = true; + fermentationModeStartTime = millis(); clearScreen(); tft.setCursor(0, 140); @@ -240,8 +273,9 @@ int setBrewMode(String command) } else if (commands[0] == "stop") { - isBrewing = false; - isFermenting = false; + isBrewingMode = false; + isFermentationMode = false; + resetFermentationVariables(); clearScreen(); tft.setCursor(0, 140); diff --git a/brew-buddy-firmware/src/brew-buddy-firmware.ino b/brew-buddy-firmware/src/brew-buddy-firmware.ino index e019fa9..68bdc32 100644 --- a/brew-buddy-firmware/src/brew-buddy-firmware.ino +++ b/brew-buddy-firmware/src/brew-buddy-firmware.ino @@ -68,8 +68,18 @@ const uint8_t lowTemp = 70; const uint8_t highTemp = 220; //Brew Stage Variables -bool isBrewing = false; +bool isBrewingMode = false; +bool isFermentationMode = false; + +//Variables for fermentation detection bool isFermenting = false; +unsigned long fermentationModeStartTime = 0; +unsigned long fermentationStartTime = 0; + +// Variables for fermentation rate +long fermentationRate = 0; // knocks per ms +int knocksDetected = 0; +unsigned long lastKnock = 0; String brewStage; String brewId; @@ -112,21 +122,33 @@ void setup() void loop() { - if (isFermenting) + if (isFermentationMode) { int16_t knockVal = analogRead(KNOCK_PIN) / 16; if (knockVal >= 6) { Serial.printlnf("Knock Val: %d", knockVal); + lastKnock = millis(); - printSubheadingLine("Fermentation detected!"); - - waitUntil(Particle.connected); - Particle.publish("fermentation-value", String(knockVal)); + if (!isFermenting) + { + isFermenting = true; + + clearScreen(); + tft.setCursor(0, 140); + printSubheadingLine("Fermentation started..."); + // TODO: print delta btw mode start and fermentation start + waitUntil(Particle.connected); + Particle.publish("fermentation/state", "start"); + } + else + { + // TODO: Display fermentation rate + } } } - else if (isBrewing) + else if (isBrewingMode) { unsigned long currentMillis = millis(); @@ -162,6 +184,15 @@ void loop() } } +void resetFermentationVariables() +{ + isFermenting = false; + fermentationModeStartTime = 0; + fermentationStartTime = 0; + fermentationRate = 0; + lastKnock = 0; +} + void activateBrewStage() { startTime = millis(); @@ -193,10 +224,10 @@ int setBrewMode(String command) brewStage = commands[2]; brewId = commands[1]; - if (commands[0] == "brew" && !isBrewing) + if (commands[0] == "brew" && !isBrewingMode) { - isBrewing = true; - isFermenting = false; + isBrewingMode = true; + isFermentationMode = false; clearScreen(); activateBrewStage(); @@ -205,8 +236,9 @@ int setBrewMode(String command) } else if (commands[0] == "ferment") { - isBrewing = false; - isFermenting = true; + isBrewingMode = false; + isFermentationMode = true; + fermentationModeStartTime = millis(); clearScreen(); tft.setCursor(0, 140); @@ -219,8 +251,9 @@ int setBrewMode(String command) } else if (commands[0] == "stop") { - isBrewing = false; - isFermenting = false; + isBrewingMode = false; + isFermentationMode = false; + resetFermentationVariables(); clearScreen(); tft.setCursor(0, 140); From 39de364c6f74ecd6c22595b6de9823def6dcf7c1 Mon Sep 17 00:00:00 2001 From: Brandon Satrom Date: Tue, 26 Feb 2019 16:50:37 -0600 Subject: [PATCH 2/2] added fermentation rate logic; closes #23 --- .../src/brew-buddy-firmware.cpp | 64 ++++++++++++++++--- .../src/brew-buddy-firmware.ino | 61 +++++++++++++++--- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/brew-buddy-firmware/src/brew-buddy-firmware.cpp b/brew-buddy-firmware/src/brew-buddy-firmware.cpp index fda3043..fa3d63d 100644 --- a/brew-buddy-firmware/src/brew-buddy-firmware.cpp +++ b/brew-buddy-firmware/src/brew-buddy-firmware.cpp @@ -18,6 +18,7 @@ void setup(); void loop(); void resetFermentationVariables(); +long getFermentationRate(); void activateBrewStage(); int setBrewMode(String command); void printSplash(); @@ -28,9 +29,11 @@ float readTemp(); void postTemp(float temp); void printReading(float reading); void displayStageName(String stagename); +void displayFermentationHeading(); void displayTimeHeading(); void displayTempHeading(); void displayTempHistoryHeading(); +void displayFermentationRate(long rate); void displayTime(float elapsedTime); String calcTimeToDisplay(float elapsedTime); void updateChart(float temp); @@ -82,6 +85,7 @@ const uint8_t headingTextSize = 4; const uint8_t subheadTextSize = 2; const uint8_t tempTextSize = 5; const uint8_t elapsedTimeSize = 3; +const uint8_t fermentationRateSize = 5; const uint8_t pixelMultiplier = 7; //Used to clear text portions of the screen //QueueArray for the last 22 temperature readings for the TFT Graph @@ -99,9 +103,9 @@ unsigned long fermentationModeStartTime = 0; unsigned long fermentationStartTime = 0; // Variables for fermentation rate +QueueArray knockArray; long fermentationRate = 0; // knocks per ms -int knocksDetected = 0; -unsigned long lastKnock = 0; +unsigned long lastKnock; String brewStage; String brewId; @@ -151,22 +155,33 @@ void loop() if (knockVal >= 6) { Serial.printlnf("Knock Val: %d", knockVal); - lastKnock = millis(); if (!isFermenting) { isFermenting = true; + fermentationStartTime = millis(); + lastKnock = fermentationStartTime; clearScreen(); - tft.setCursor(0, 140); - printSubheadingLine("Fermentation started..."); + tft.setCursor(0, 10); + tft.setTextColor(ILI9341_YELLOW); + tft.setTextSize(2); + tft.print("Fermentation started"); + tft.setTextColor(ILI9341_WHITE); + + displayFermentationHeading(); // TODO: print delta btw mode start and fermentation start + waitUntil(Particle.connected); Particle.publish("fermentation/state", "start"); } else { - // TODO: Display fermentation rate + knockArray.push(millis() - lastKnock); + lastKnock = millis(); + fermentationRate = getFermentationRate() / 1000.00; // # of ms between knocks + + displayFermentationRate(fermentationRate); } } } @@ -212,7 +227,24 @@ void resetFermentationVariables() fermentationModeStartTime = 0; fermentationStartTime = 0; fermentationRate = 0; - lastKnock = 0; +} + +long getFermentationRate() +{ + long rate = 0.0; + uint16_t arrayCount = knockArray.count(); + uint16_t sum = 0; + + for (int i = 0; i < arrayCount; i++) + { + uint16_t currentVal = knockArray.dequeue(); + sum += currentVal; + knockArray.enqueue(currentVal); + } + + rate = sum / arrayCount; + + return rate; } void activateBrewStage() @@ -265,7 +297,7 @@ int setBrewMode(String command) clearScreen(); tft.setCursor(0, 140); printSubheadingLine("Waiting for"); - printSubheadingLine("Fermentation to begin..."); + printSubheadingLine("Fermentation..."); System.sleep(KNOCK_PIN, CHANGE); @@ -384,6 +416,14 @@ void displayStageName(String stagename) tft.setTextColor(ILI9341_WHITE); } +void displayFermentationHeading() +{ + tft.setCursor(0, 40); + tft.setTextSize(2); + tft.println("Fermentation Rate"); + tft.println("(in seconds)"); +} + void displayTimeHeading() { tft.setCursor(0, 120); @@ -405,6 +445,14 @@ void displayTempHistoryHeading() tft.println("Temp History"); } +void displayFermentationRate(long rate) +{ + tft.fillRect(0, 80, 240, fermentationRateSize * pixelMultiplier, ILI9341_BLACK); + tft.setCursor(0, 80); + tft.setTextSize(fermentationRateSize); + tft.println(rate); +} + void displayTime(float elapsedTime) { String timeString = calcTimeToDisplay(elapsedTime); diff --git a/brew-buddy-firmware/src/brew-buddy-firmware.ino b/brew-buddy-firmware/src/brew-buddy-firmware.ino index 68bdc32..b9e13d7 100644 --- a/brew-buddy-firmware/src/brew-buddy-firmware.ino +++ b/brew-buddy-firmware/src/brew-buddy-firmware.ino @@ -60,6 +60,7 @@ const uint8_t headingTextSize = 4; const uint8_t subheadTextSize = 2; const uint8_t tempTextSize = 5; const uint8_t elapsedTimeSize = 3; +const uint8_t fermentationRateSize = 5; const uint8_t pixelMultiplier = 7; //Used to clear text portions of the screen //QueueArray for the last 22 temperature readings for the TFT Graph @@ -77,9 +78,9 @@ unsigned long fermentationModeStartTime = 0; unsigned long fermentationStartTime = 0; // Variables for fermentation rate +QueueArray knockArray; long fermentationRate = 0; // knocks per ms -int knocksDetected = 0; -unsigned long lastKnock = 0; +unsigned long lastKnock; String brewStage; String brewId; @@ -129,22 +130,33 @@ void loop() if (knockVal >= 6) { Serial.printlnf("Knock Val: %d", knockVal); - lastKnock = millis(); if (!isFermenting) { isFermenting = true; + fermentationStartTime = millis(); + lastKnock = fermentationStartTime; clearScreen(); - tft.setCursor(0, 140); - printSubheadingLine("Fermentation started..."); + tft.setCursor(0, 10); + tft.setTextColor(ILI9341_YELLOW); + tft.setTextSize(2); + tft.print("Fermentation started"); + tft.setTextColor(ILI9341_WHITE); + + displayFermentationHeading(); // TODO: print delta btw mode start and fermentation start + waitUntil(Particle.connected); Particle.publish("fermentation/state", "start"); } else { - // TODO: Display fermentation rate + knockArray.push(millis() - lastKnock); + lastKnock = millis(); + fermentationRate = getFermentationRate() / 1000.00; // # of ms between knocks + + displayFermentationRate(fermentationRate); } } } @@ -190,7 +202,24 @@ void resetFermentationVariables() fermentationModeStartTime = 0; fermentationStartTime = 0; fermentationRate = 0; - lastKnock = 0; +} + +long getFermentationRate() +{ + long rate = 0.0; + uint16_t arrayCount = knockArray.count(); + uint16_t sum = 0; + + for (int i = 0; i < arrayCount; i++) + { + uint16_t currentVal = knockArray.dequeue(); + sum += currentVal; + knockArray.enqueue(currentVal); + } + + rate = sum / arrayCount; + + return rate; } void activateBrewStage() @@ -243,7 +272,7 @@ int setBrewMode(String command) clearScreen(); tft.setCursor(0, 140); printSubheadingLine("Waiting for"); - printSubheadingLine("Fermentation to begin..."); + printSubheadingLine("Fermentation..."); System.sleep(KNOCK_PIN, CHANGE); @@ -362,6 +391,14 @@ void displayStageName(String stagename) tft.setTextColor(ILI9341_WHITE); } +void displayFermentationHeading() +{ + tft.setCursor(0, 40); + tft.setTextSize(2); + tft.println("Fermentation Rate"); + tft.println("(in seconds)"); +} + void displayTimeHeading() { tft.setCursor(0, 120); @@ -383,6 +420,14 @@ void displayTempHistoryHeading() tft.println("Temp History"); } +void displayFermentationRate(long rate) +{ + tft.fillRect(0, 80, 240, fermentationRateSize * pixelMultiplier, ILI9341_BLACK); + tft.setCursor(0, 80); + tft.setTextSize(fermentationRateSize); + tft.println(rate); +} + void displayTime(float elapsedTime) { String timeString = calcTimeToDisplay(elapsedTime);