This is an extension from previous topic where i wanted to store the maximum value from sensor within a specific period and then print that to an lcd.
I managed to make a code that will store the max value and it worked nicely but then i wanted to to add a blowing count down and a warm up time countdown and there came a new problem, both countdown codes worked like i wanted them but the code to get the max value is not working or idk it's just not being excuted. but when i separate it into another file and just run it individually it works fine
here is my code
#include <SoftwareSerial.h>
#include <String.h>
#include <LiquidCrystal.h>
const int Alcohol = 9; //the sensor connected to pin 9
int Alcoholvalue;
int Max;
int Mosfet = 10;
unsigned long previousMillis = 10005; //i set this to 10 sec because of the 10 seconds of the warming time
unsigned const long interval = 5000; //so here is the blowing time
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); //connecting the lcd
void setup() {
// Setup size of LCD 20 characters and 4 lines
lcd.begin(20, 4);
lcd.setCursor(1, 2);
lcd.setCursor(4, 3);
lcd.setCursor(0, 0);
Serial.begin(9600);
pinMode(Alcohol, INPUT);
pinMode (Mosfet, OUTPUT);
lcd.print("warming up, wait");
}
void Warming() //this function will show a countdown timer for 10 seconds (i need the 10 sec as a warm up time for the sensor)
{
while (millis() <= 10000) {
lcd.setCursor(0, 1);
int count = 10 - millis() / 1000;
lcd.print (count);
//to remove the 0 from the 10 before the 9
if (count < 10) {
lcd.print(' ');
}
}
}
void Blowing() //thie is a countdown while exhaling or blowing towards the sensor for 5sec
{
while (millis() >= 10000) { //after 10sec of warming
//now i want this to run for 5 sec only so i timed it until 15005 millis
//i gave the extra 5 millisec here to make sure i get to see all the countdown
while (millis() <= 15005) {
lcd.setCursor(0, 1);
lcd.print(15 - millis() / 1000);
}
}
}
void DetectingTheMax() //function to get the maximum value during the whole time
//and then print it
{
Max = 0;
int Alcoholvalue = digitalRead(Alcohol);
if (Alcoholvalue > Max) {
Max = Alcoholvalue;
}
Serial.print("detected value: ");
Serial.print(Max);
lcd.clear();
lcd.print("detected value: ");
lcd.print(Max);
return Max;
}
void loop()
{
Warming();
lcd.clear(); //clearing the lcd so the blow word can appear
lcd.setCursor(0, 0);
lcd.print("blow");
Blowing();
lcd.clear();
lcd.setCursor(0, 0);
unsigned long currentMillis = millis();
//i want this function to be excuted after the countdown,
//basically at time = 15005 millisec, but it is not working,
//i tried using while instead of if but it is still not working
if (currentMillis - previousMillis == interval) {
DetectingTheMax();
}
}
and here is the separate code for getting the max and it works just fine !!
only getting max value
#include <LiquidCrystal.h>
const int Alcohol = 9;
int Alcoholvalue;
int Max;
unsigned long previousMillis = 10000;
unsigned const long interval = 5000;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); //connecting the lcd
void setup() {
// Setup size of LCD 20 characters and 4 lines
lcd.begin(20, 4);
lcd.setCursor(1, 2);
lcd.setCursor(4, 3);
lcd.setCursor(0, 0);
Serial.begin(9600);
pinMode(Alcohol, INPUT);
}
void DetectingTheMax()
{
Max = 0;
int Alcoholvalue = digitalRead(Alcohol);
if (Alcoholvalue > Max){
Max = Alcoholvalue;
}
Serial.print("detected value: ");
Serial.print(Max);
lcd.clear();
lcd.print("detected value: ");
lcd.print(Max);
return Max;
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis == interval) {
DetectingTheMax();
}
}