serLCD count down issue. Need advice, Thanks.

Hello,

Im trouble shooting for a counter on a serLCD, however, once the button that starts the count down is actuated, there is jumps from one variable to another. EX: ( If I set 60 min for the count down, once I pressed the button that starts the count down, it will jump to 35min and start counting down from there).

HERE IS THE CODE :

#include <SoftwareSerial.h>

#define TextPin 2

SoftwareSerial LCD = SoftwareSerial(0, TextPin);
// since the LCD does not send data back to the Arduino, we should only define the txPin
const int LCDdelay=10; // conservative, 2 actually works

//Positioning of the LCD text

void lcdPosition(int row, int col) {
LCD.write(0xFE); //command flag
LCD.write((col + row*64 + 128)); //position
delay(LCDdelay);
}

// Initial Setup For the LCD
void clearLCD(){
LCD.write(0xFE); //command flag
LCD.write(0x01); //clear command.
delay(LCDdelay);
}
void backlightOn() { //turns on the backlight
LCD.write(0x7C); //command flag for backlight stuff
LCD.write(157); //light level.
delay(LCDdelay);
}
void backlightOff(){ //turns off the backlight
LCD.write(0x7C); //command flag for backlight stuff
LCD.write(128); //light level for off.
delay(LCDdelay);
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
LCD.write(0xFE);
}

// Set up of Button 1 Which makes Increments in minutes every time is pressed

int button1 = 8;// increasing button
int button1Counter = 0; // counts everytime button1 is pressed
int button1State = 0 ;// current state of button1
int lastButton1State = 0;// previuos state of button1

// set up of button 2 Which makes decrements in minutes everytime is pressed
int button2 = 9; //decrease button
int button2State = 0; // current state button2
int lastButton2State = 0; // prevoious state of button2

// set up of button 3 which will be consider as start button
int button3 = 10; // start or okay button
int button3State = 0;
int lastButton3State = 0;

//FIRST SET UP FOR COMMUNICATION BETWEEN LCD AND ARDUINO

void setup(){
Serial.begin(9600); // start serial comunication
pinMode (button1, INPUT); // button1 is declared as an input
pinMode (button2, INPUT); // button2 is declared as an input
pinMode (button3, INPUT); // button3 is declared as an input

//configuration for the LCD screen below
pinMode(txPin, OUTPUT);
LCD.begin(9600);
clearLCD();
lcdPosition(0,0);
LCD.print(“PRESS BUTTON TO START”);
}

// THE LOOP THAT FOCUS ON THE ACTIONS THAT EACH BOTTON WILL TAKE

void loop(){

//FIRST BUTTON SETUP

button1State = digitalRead (button1); //reads when the button is pressed and sets it up as its current state which is HIGH
if (button1State != lastButton1State){ //veryfies and comfirms if button1 has been pressed
if (button1State == HIGH){
button1Counter++; // adds one to the counter everytime button1 is pressed
clearLCD();
lcdPosition(0,0);
LCD.print(“TIME IN MINUTES:”); // everytime button2 is pressed prints the result from above.
LCD.print(button1Counter);
delay(300);
}

else {
Serial.println ( “off”); // if button1 isnt pressed keep it off
}
}

//SECOND BUTTON SET UP

button2State = digitalRead ( button2); //reads when button2 is pressed

if (button2State != lastButton2State){ //verifies wheather button2 is HIGH
if (button2State == HIGH){
button1Counter -= 1; //if button2 is being pressed then timer starts going down by 1 unit
clearLCD();
lcdPosition(0,0);
LCD.print(“TIME IN MINUTES:”); // everytime button1 is pressed print this.
LCD.print(button1Counter);
delay(300);
}
else {
Serial.println ( “off”); // if button2 isnt pressed keep it off
}
}

// THIRD BUTTON SET UP ******* THE PROBLEM IS IN THIS SECTION I BELIEVE**************

lastButton2State = button2State;

unsigned int time;

button3State = digitalRead (button3);
if (button3State != lastButton3State) {
while (button3State == HIGH) {
if((millis()/1000) < button1Counter){ // if the reading of buttom1Counter is higher than 1 sec, it starts counting down
clearLCD();
lcdPosition(0,0);
LCD.print("TIME REMINDING: "); // prints this statement once the timer in counting down
LCD.print(button1Counter -(millis()/1000)); // function that represent the count down
delay(1000);
}
else {

// reached zero, flash the display
time=millis();
while(millis() < time) {
button1Counter == 0; // display 0 for 0.2 second
}

}
}

}

}

Your basic problem is millis() gives the time since your sketch started running, not the time since you pressed the start button.

When you first press button3, you need to get the millis() then, and use that as your start point. That is, subtract it from any future millis() while your countdown is running.