Could anyone direct me to a Arduino-based stopwatch/timer project which counts in seconds, with a stop-start button?
ie. if is press the button at five seconds, it will pause. Then if I press it again, it will start counting again from five seconds. It should reference against the hardware clock for accuracy (use millis()).
A basic program structure would also be okay. I would like to use it with an LCD display, and I know how to display variables.
I would prefer to know how to have two separate timers running, controlled by two tactile switches, with both times being displayed on the LCD. Thanks.
Here's some different pieces of info that will at least point you in the right direction:
if you want to translate millis into seconds then make a seconds variable.
setup:
unsigned long now;
main:
now = millis()
if (now - then > 1000){
seconds++;
then=millis()
}
You can set a variable equal to millis() with a button
unsigned long time1;
if (button is pressed){
time1=millis()
}
toggle a timer on using a trigger:
if (button is pressed){
if (trigger ==0){
starttimer=1; // make trigger = 1 after the stopwatch has been running for a second or something
}else{
trigger=0
stoptimer=1
}
}
if (starttimer ==1){ //you could use a while loop here
starttime= seconds;
timenow= starttime - seconds;
}
Here's a modified version of the buttonstate example
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
created 27 Sep 2005
modified 17 Jun 2009
by Tom Igoe
http://arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
unsigned long start;
unsigned long now;
unsigned long stoptime = 0;
unsigned long time1;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int trigger=0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
//00000000000000000000000000000000000000000000000000
// STOP WATCH
now= millis();
if (now - stoptime > 100){ // if it's been a little time since it last stopped
if (trigger == 0){
if (buttonState == HIGH) {
start=millis();
trigger==1;
}
}
}
if (trigger=1){
time1 = now - start; // the time that has passed since you first hit the button
Serial.println(time1);
if (buttonState == HIGH && time1 > 500){ //at least half a second has passed since you hit the button
trigger=0; //now you can turn it off
stoptime= millis();
}
}
}