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");
}
}