arloG:
This sounds like you want button 1 to start a timer and button 2 to stop the timer. This is different than you described in your original post. In any case, your problem isn't an LCD issue, it's a logic issue. The code below uses switch 1 to start a timer and switch 2 to stop it. The display is the serial monitor. If you want it on an LCD you can substitute for the Serial.prints. Your messages will have to be shortened for the LCD display.
#include <LiquidCrystal.h>
long startTime; // count seconds
long period;
int seconds;
// assign switch inputs
const int switchPin1 = 2;
const int switchPin2 = 3;
// variables to hold the value of the switchPins
int switchState1 = 0;
int switchState2 = 0;
// variables to hold previous value of the switch
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
void setup()
{ Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
reset();
Serial.println("Ready for button 1");
}
void loop()
{ switchState1 = digitalRead(switchPin1); // check the status of the switch
if (switchState1 != prevSwitchState1) // compare the switchState to its previous state
{ if (switchState1 == LOW)
{ startCount();
}
}
prevSwitchState1 = switchState1; // save the current switch state as the last state
}
void startCount()
{ reset();
Serial.println("Start counting ");
while (switchState2 = prevSwitchState2)
{ switchState2 = digitalRead(switchPin2); // check the status of the switch
prevSwitchState2 = switchState2; // save the current switch state as the last state
period=(int)((millis()-startTime)/1000);
if((int)period!= seconds)
{ Serial.print (" ");
Serial.print(seconds+1);
seconds = (int)period;
}
}
boom();
}
void boom()
{ Serial.println(" Boom");
Serial.println("Interruption received, press button 2 to continue" );
Serial.println();
Serial.print( "Waited for ");
Serial.print(seconds);
Serial.println(" seconds");
Serial.println("Push button 1 to count again");
reset();
}
void reset()
{ startTime = millis();
seconds=0;
switchState2 = digitalRead(switchPin2); // check the status of the switch
prevSwitchState2 = switchState2; // save the current switch state as the last state
}
Wow! Thank you for the code. However, I modified the code you wrote for me to work with LCD display. However, I faced a couple difficult issues. First - how do I get my start() to display message with second number being counted? The 2nd issue goes the same for LCD message after pressing 2nd button for "Reset: X". Here's the modified code -
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
unsigned long time;
long startTime; // count seconds
long startTime1;
long startTime2;
long period;
long period1;
long period2;
int seconds;
int seconds1;
int seconds2;
// assign switch inputs
const int switchPin1 = 2;
const int switchPin2 = 3;
// variables to hold the value of the switchPins
int switchState1 = 0;
int switchState2 = 0;
// variables to hold previous value of the switch
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
//start();
}
void loop()
{
/**
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: ");
time = ((millis() + 500) / 1000);
//prints time since program started
lcd.print(time);
// wait a second so as not to send massive amounts of data
delay(1000);
*/
switchState1 = digitalRead(switchPin1); // check the status of the switch
if (switchState1 != prevSwitchState1) // compare the switchState to its previous state
{
if (switchState1 == LOW)
{
boom();
}
}
prevSwitchState1 = switchState1; // save the current switch state as the last state
switchState2 = digitalRead(switchPin2);
if (switchState2 != prevSwitchState2) // compare the switchState to its previous state
{
if (switchState2 == LOW)
{
newCount();
}
}
prevSwitchState2 = switchState2;
}
void start()
{
forStart();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting... ");
//period = ((millis() - startTime) / 1000);
//lcd.print(period);
lcd.setCursor(0, 1);
lcd.print("seconds.");
}
void boom()
{
forBoom();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BOOM! Press 2");
lcd.setCursor(0, 1);
lcd.print("to continue.");
while (switchState2 = prevSwitchState2)
{
switchState2 = digitalRead(switchPin2); // check the status of the switch
prevSwitchState2 = switchState2; // save the current switch state as the last state
period1 = (int)((millis() - startTime1) / 1000);
if ((int)period1 != seconds1)
{
seconds1 = (int)period1;
}
}
}
void newCount()
{
newReset();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waited: ");
lcd.print(seconds1);
lcd.print(" secs.");
lcd.setCursor(0, 1);
lcd.println("Reset: ");
while (switchState1 = prevSwitchState1)
{
switchState1 = digitalRead(switchPin1); // check the status of the switch
prevSwitchState1 = switchState1; // save the current switch state as the last state
period2 = ((millis() - startTime2) / 1000);
if (period2 != seconds2)
{
seconds2 = period2;
lcd.print(period2);
}
}
}
void forStart()
{
startTime = millis();
seconds = 0;
}
void forBoom()
{
startTime1 = millis();
seconds1 = 0;
switchState2 = digitalRead(switchPin2); // check the status of the switch
prevSwitchState2 = switchState2; // save the current switch state as the last state
}
void newReset()
{
startTime2 = millis();
seconds2 = 0;
switchState1 = digitalRead(switchPin1); // check the status of the switch
prevSwitchState1 = switchState1; // save the current switch state as the last state
}