Oh! hahaha alright I will work on it, thanks!
To be more specific, you will usually want something like:
Serial.print("TimeRemaining is: ");
Serial.println(TimeRemaining);
This makes it easy to interpret what you are seeing in the serial monitor, rather than just a row of numbers flashing by.
When you are happy, just comment out those lines. You don't need to delete them, unless you are fussy about making your source code look beautiful. ![]()
Ahh, okay! Thanks for further explaining, really appreciate the help ![]()
Thanks Mr johnwasser, this worked out! The time was able to display the intended 1 min and not 57seconds anymore ![]()
As for this, do you have any idea on how to solve it? I tried analysing it myself and changing the codes here and there but nothing seems to make it stop counting at 00:00. Sorry for the countless dependence on you Mr Johnwasser ![]()
I don't think you will ever see 1:00 on the display. One microsecond after you start the timer, the remaining time is 59.999 seconds which will display as 0:59.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
const int TriggerPin = 8; //set pins for the ultrasonic sensor, button, BuzzerPin and LCD
const int EchoPin = 9;
const int ButtonPin = 2;
const int BuzzerPin = 13;
const unsigned long SECOND = 1000ul;
const unsigned long MINUTE = SECOND * 60;
bool TimerRunning = false; // Start paused
unsigned long TimeRemaining = MINUTE;
unsigned long TimeStarted = 0;
void setup()
{
Serial.begin(115200);
delay(200); // Allow time for Serial Monitor to connect
//set pin modes
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(ButtonPin, INPUT);
pinMode(BuzzerPin, OUTPUT);
lcd.begin (16, 2);
lcd.init();
lcd.backlight();
}
void pause()
{
if (TimerRunning)
{
TimeRemaining = TimeRemaining - (millis() - TimeStarted);
TimerRunning = false;
}
}
void resume()
{
if (!TimerRunning)
{
TimeStarted = millis();
TimerRunning = true;
}
}
void timer()
{
unsigned long currentTime = millis();
long displayTime;
if (TimerRunning)
{
unsigned long elapsedTime = currentTime - TimeStarted;
if (elapsedTime < TimeRemaining)
{
displayTime = TimeRemaining - elapsedTime;
}
else
{
displayTime = 0;
}
}
else
{
// Timer is paused. Show the remaining time
displayTime = TimeRemaining;
}
byte countdown_minute = ((displayTime / 1000) / 60) % 60;
byte countdown_sec = (displayTime / 1000) % 60;
lcd.setCursor(6, 1);
if (countdown_minute < 10)
{
lcd.print(' ');
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10)
{
lcd.print('0');
}
lcd.print(countdown_sec);
// Time has expired
if (displayTime == 0)
{
pause();
tone(BuzzerPin, 1000, 200);
delay(100);
tone(BuzzerPin, 1000, 200);
delay(100);
// Reset the timer
TimeRemaining = MINUTE;
}
}
// Speed of sound: 343 meters per second == 0.002915452 seconds per meter
const float MicrosecondsPerCM = 29.15452;
const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2.0; // ~58.3
void loop()
{
digitalWrite(TriggerPin, LOW); // send out an ultra sonic sound for 5 microseconds and measure the time it took for the sound to go from the trigpin to the echo pin
delayMicroseconds(5);
digitalWrite(TriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
unsigned long signalDuration = pulseIn(EchoPin, HIGH, 6000); // Timeout good for 100 cm.
int plankDist = signalDuration / MicrosecondsPerRoundTripCM; //convert the time the signal took to travel to distance in cm
//proper posture within the distance range of 10cm to 40cm, timer continues counting
if (plankDist >= 10 && plankDist <= 40)
{
resume();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Maintain Posture");
lcd.setCursor(0, 1);
lcd.print("Time:");
lcd.setCursor(10, 1);
timer(); //void timer() displays the time here
}
else // if outside of the above stated range of 10cm to 40cm, timer pauses as posture is improper
{
pause();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Adjust Position");
lcd.setCursor(0, 1);
lcd.print("Timer Paused");
}
}
Hi Mr Johnwasser, this code works amazingly. However, is there any way to change this so that the timer stops counting and won't reset once it reaches 00:00?
Of course there is. How about you have a go at it yourself? Post your code if you still have problems.
Sure. Just replace that line with your choice of infinite loop:
for(;;);
while(1);
while(true){}
The Arduino will stop and you would re-start the timer by pressing the reset button.
Hi Mr Johnwasser, is there anyway to edit this set of codes to become a count up timer?
Yes
if (TimerRunning)
{
unsigned long elapsedTime = currentTime - TimeStarted;
displayTime = TimeElapsed + elapsedTime;
}
else
{
// Timer is paused. Show the elapsed time
displayTime = TimeElapsed;
}
[/quote]
Thanks Mr Johnwasser, I will try them out ![]()
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.