The void function keeps repeating and it shows the temperature and the timer every millisecond so I can't see anything, I also need help making it so after it ends it makes a buzzer sound and if the temperature is 28° it starts the countdown again. The countdown also shouldn't be affected by changes in temperature in the 6 hour time span. Also I need to make a button to start the countdown, I need help urgently.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
float tempValue;
float voltage;
float tempC;
float tempF;
int buzzer = 8;
int hours = 6;
int minutes = 0;
int seconds = 0;
void setup(){
lcd.init();
lcd.backlight();
pinMode(buzzer, OUTPUT);
lcd.setCursor(3,0);
lcd.print("TECH FAIR");
lcd.setCursor(5,1);
lcd.print("NOMBRE");
delay(2000);
lcd.clear();
}
void loop(){
tempValue = analogRead(A0);
voltage = (tempValue * 5) / 1024;
tempC = (voltage-0.5)* 100;
tempF = (tempC * 1.8) + 32;
timer();
}
void timer(){
if(tempC >= 28){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown:");
// formato HH:MM:SS
lcd.setCursor(0, 1);
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0"); // zero para los digitos de 1
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
// quitale al countdown
if (seconds > 0) {
seconds--;
} else {
if (minutes > 0) {
minutes--;
seconds = 59;
} else {
if (hours > 0) {
hours--;
minutes = 59;
seconds = 59;
}
}
}
}else{
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Temp (C):");
lcd.setCursor(8,0);
lcd.print(tempC);
}
}
Hi @juliojeda ,
Welcome to the forum..
curious, is the temp producing correct values??
give this a try..
#include <LiquidCrystal_I2C.h>
#define BTN_PIN 7
LiquidCrystal_I2C lcd(0x20, 16, 2);
float tempValue;
float voltage;
float tempC;
float tempF;
int buzzer = 8;
int hours = 6;
int minutes = 0;
int seconds = 0;
bool countStarted = false;
bool countFin = false;
unsigned long lastCount = 0;
unsigned long intervalCount = 1000;
unsigned long lastBuzz = 0;
int intervalBuzz = 1000;
byte buzzState = 0;
bool buzzFin = false;
unsigned long lastDebounce = 0;
int intervalDebounce = 50;
byte lastBtn = 1;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(buzzer, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
lcd.setCursor(3, 0);
lcd.print("TECH FAIR");
lcd.setCursor(5, 1);
lcd.print("NOMBRE");
delay(2000);
lcd.clear();
}
void loop() {
unsigned long now = millis();
if (millis() - lastDebounce >= intervalDebounce) {
byte state = digitalRead(BTN_PIN);
if (state != lastBtn) {
lastBtn = state;
lastDebounce = millis();
if (state == HIGH) {
//button pressed and released
if (!countStarted) {
StartCount();
}
}
}
}
tempValue = analogRead(A0);
voltage = (tempValue * 5) / 1024;
tempC = (voltage - 0.5) * 100;
tempF = (tempC * 1.8) + 32;
if (tempC >= 28 && !countStarted) {
Serial.print("TempC:");
Serial.print(tempC);
StartCount();
}
if (countStarted && !countFin) {
if (now - lastCount >= intervalCount) {
lastCount = now;
countFin = timer();
}
} else if (countStarted && countFin) {
if (!buzzFin) {
buzz();
} else {
//finsihed the buzzing..
//reset everythings..
buzzFin = false;
countFin = false;
countStarted = false;
lcd.clear();
}
}
}
//starts the count down
void StartCount() {
countStarted = true;
lastCount = millis() - 1001;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown:");
hours = 6;
minutes = 0;
seconds = 0;
intervalBuzz = 1000;
buzzState = 0;
}
bool timer() {
bool result = false;
// formato HH:MM:SS
lcd.setCursor(0, 1);
lcd.print(hours);
lcd.print(":");
if (minutes < 10) {
lcd.print("0"); // zero para los digitos de 1
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) {
lcd.print("0");
}
lcd.print(seconds);
int left = hours + minutes + seconds;
if (left == 0) {
//all done
result = true;
}
// quitale al countdown
if (seconds > 0) {
seconds--;
} else {
if (minutes > 0) {
minutes--;
seconds = 59;
} else {
if (hours > 0) {
hours--;
minutes = 59;
seconds = 59;
}
}
}
return result;
}
void buzz() {
switch (buzzState) {
case 0: tone(buzzer, 392); lastBuzz = millis(); buzzState++; break;
case 1: if (millis() - lastBuzz >= intervalBuzz) {
buzzState++;
noTone(buzzer);
intervalBuzz = 500;
lastBuzz = millis();
}
break;
case 2: if (millis() - lastBuzz >= intervalBuzz) {
buzzState++;
intervalBuzz = 1000;
tone(buzzer, 175);
lastBuzz = millis();
}
break;
case 3: if (millis() - lastBuzz >= intervalBuzz) {
buzzState = 0;
noTone(buzzer);
buzzFin = true;
}
break;
}
}
i did not test the full 6 hour count down obviously, had mine set to 10 seconds, seems to work properly..
have fun.. ~q
1 Like
Holy crap dude thanks! I think the temp is producing correct values. I tried the code and it's awesome but the timer is kinda slowed- any clue why?
If you can see in the image attached, the runtime vs the actual countdown is different- maybe its my laptop but idk
thanks so much
PD: It would be great if you wanted to explain some of the stuff you did, I'd like to understand better but ofc you don't have to (my dc is tacostaco)
You're welcome..
The count is controlled by intervalCount which is currently set too 1000 milli seconds or 1 second, should be pretty dead on but you can adjust the interval if needed..
added a button and a millis timer to debounce..
button is input pull up, so just need to hit ground..
I changed the timer function to return whether it has finished, returns true when done..
The buzz routine demonstrates the use of a state machine, when the buzzing is finished then it sets a flag..
a few flags were added for count started, count finished and buzz finished..
You can add more state to the buzz routine if you want to play a tune or something..
this design does rely on a fast loop, so don't add any delay() functions or you can compromise the countdown interval..
I was adjusting seconds to 10, hours and minutes at 0 in the start count function for testing, looks like it was counting proper..
got it in a sim here..
I did have to change the temp calc as it was reporting like 200c, that's why I asked, could just be something in the sim, only had one temp sensor to use in there..
WoKWi pretty nice..
have fun.. ~q
1 Like
Actually looking at your pic, looks proper..
That's the total simulator time..
in setup there is a 2 second delay and then might be a second or so before it starts..
So yeah, looks about right..
~q
Yeah mine is definitely slower but that might just be on my end, I'll try it on another laptop tomorrow- thanks again!