Hello,
I need a simple timer or stopwatch sketch.
I have the uno.
I am going to use 2 photoelectric sensors and all I need is a basic simple sketch to start a timer when the first one is tripped and then turn the timer off when the second one is reached. I would like it to be displayed in seconds and minutes. I would also like to have a reset button to start it all over. I will read it on a digital readout, but I already know how to do that.
Any help would be appreciated.
Thanks,
Tony
the very same question was asked in the forum a few days ago.
is that for a school assignment?
check this link - may be you'll find one of your classmate...
Build some code doing this:
in plain English it's
Stage = waiting // will only start if detector1 goes from above to below threshold
Loop
Read both detectors and the button.
if button is pressed then set stage to waiting
else
If detector 1 is above threshold and stage is waiting then set stage to starting
If detector1 is below threshold and stage is starting then
Record start time and set stage to measuring
If detector2 is below threshold and stage is measuring then
We are done. Display Duration which is (current time - start time). Stage goes to waiting.
If stage is measuring display current time - startTime in the LCD // live view will slow measure precision
heliguy02:
Hello,
I need a simple timer or stopwatch sketch.
I have the uno.
I am going to use 2 photoelectric sensors and all I need is a basic simple sketch to start a timer when the first one is tripped and then turn the timer off when the second one is reached. I would like it to be displayed in seconds and minutes. I would also like to have a reset button to start it all over. I will read it on a digital readout, but I already know how to do that.
Any help would be appreciated.
Which Arduino board are you going to use?
Timing accuracy of all boards which create their contrroller clocking from a ceramic resonator and not from a crystal oscillator are very limited in timing accuracy. If you need high accuracy and chose inadequate board, you possibly have to use a RTC module and write a sketch for to achieve higher accuracy than the internal board clocking can provide.
So which board do you want to use?
And: Do you have any thoughts about the timing accuracy you need?
If you say that the board is an "UNO R3" and that the accuracy has to be "better than 100 milliseconds within an hour of timing, then I'd suggest to use an additional RTC module.
The OP wrote Uno but did not tell which one.
// RCArduinoPersonalLapTimer Part 1 by DuaneB is
// licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
// Based on a work at rcarduino.blogspot.com.
#include <avr/pgmspace.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include "laptimes.h"
//********************
// USER INTERFACE DEFINITIONS
//****************************
// initialise the liquidCrystal Library
// Initialise the LCD using 12,11,10,9,8,7
LiquidCrystal lcd(12,11,10,9,8,7);
// PINs for user interface buttons - use any
#define KEY_OK_PIN 6
#define KEY_CANCEL_PIN 5
#define KEY_UP_PIN 4
#define KEY_DOWN_PIN 3
// bit flags used in key functions getKeys, waitForKeyPress, waitForKeyRelease
#define KEY_NONE 0
#define KEY_OK 1
#define KEY_CANCEL 2
#define KEY_UP 4
#define KEY_DOWN 8
#define KEYPRESS_ANY B11111111
// display width + 1, used by getRamString to copy a PROG_MEM string into ram
#define DISPLAY_ROW_BUFFER_LENGTH 17
//****************
// Lap Capture definitions
//*********
#define LAP_CAPTURE_LED 13
#define BUZZER_PIN A0
// minimum and maximum duration of qualifying IR Pulse
#define MIN_PULSE_DURATION 200
#define MAX_PULSE_DURATION 500
// start and end of pulse
uint32_t ulStartPulse;
uint32_t ulEndPulse;
volatile uint32_t ulPulseDuration;
// flags to manage access and pulse edges
volatile uint8_t bIRPulseFlags;
//
volatile uint32_t ulNewLapStartTime;
#define IR_PULSE_START_SET 1
#define IR_PULSE_END_SET 2
//***
// Global Instance of CLapTimes class
//***
CLapTimes gLapTimes(new CEEPROMLapStore());
///////
//
// doShowSessionSummaries
//
// implements the show session summary menu
// allows the user to scroll up and down through summaries of the recorded sessions
//
//////
void setup()
{
Serial.begin(9600);
Serial.println("In Setup");
lcd.begin(16, 2);
lcd.print("Lap Timer");
lcd.setCursor(0,1);
lcd.print("Version 0.9 Beta");
delay(3000);
pinMode(KEY_OK_PIN,INPUT);
pinMode(KEY_CANCEL_PIN,INPUT);
pinMode(KEY_UP_PIN,INPUT);
pinMode(KEY_DOWN_PIN,INPUT);
pinMode(LAP_CAPTURE_LED,OUTPUT);
pinMode(BUZZER_PIN,OUTPUT);
digitalWrite(LAP_CAPTURE_LED,LOW);
digitalWrite(BUZZER_PIN,LOW);
showTotals();
Serial.println("Out Setup");
}
/////
//
// base loop, implements root of menu system
//
// allows the user to scroll up and down through summaries of the recorded sessions
//
//////
void loop()
{
// lets keep control of the loop
while(true)
{
// wait for a key command to tell us what to do
Serial.println("Beginning Loop");
switch(waitForKeyPress(KEYPRESS_ANY))
{
// start recording
case KEY_OK:
doRecord();
break;
// delete all sessions
case KEY_CANCEL:
doConfirmDeleteSessions();
break;
// scroll through recorded session summaries
case KEY_UP:
case KEY_DOWN:
doShowSessionSummaries();
break;
}
showTotals();
waitForKeyRelease();
}
}
/////
//
// doRecord
//
// start recording new sessions, update screen every second
// check for new laps
// record new laps
// show lap time for a few seconds at the end of a lap
// update and show new best lap if its a new session best
//
/////
void doRecord()