Hey,
My project:
Make a simple timer that starts at the click of a button. My sketch works, but it starts immediately after reset.
I want the sketch to stop and wait for a button press.
If anyone could help, i would be very grateful:)
Here is my code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define ledPin 13 // LED connected to digital pin 13
#define buttonPin 8 // button on pin 4
int value = LOW; // previous value of the LED
int buttonState; // variable to store button state
int lastButtonState; // variable to store last button state
//int blinking; // condition for blinking - timer is timing
long interval = 100; // blink interval - change to suit
long previousMillis = 0; // variable to store last time LED was updated
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int fractional; // variable used to store fractional part of time
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Test");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
activate();
}
else {
digitalWrite(13, HIGH);
}
}
void activate()
{
buttonState = digitalRead(buttonPin);
if (buttonPin == HIGH){
// if true then found a new button press while clock is not running - start the clock
startTime = millis(); // store the start time
delay(5); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
else if (lastButtonState == HIGH){
// if true then found a new button press while clock is running - stop the clock and report
elapsedTime = millis(); //- startTime; // store elapsed time
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.setCursor(0,1);
lcd.print(" ");
// routine to report elapsed time
lcd.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
lcd.print("."); // print decimal point
fractional = (int)(elapsedTime % 1000L);
if (fractional == 0)
lcd.print("000");
else if (fractional < 10)
lcd.print("00");
else if (fractional < 100)
lcd.print("0");
lcd.println(fractional); // print fractional part of time
}
else{
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time