Hi,
I'm having fun and frustrations learning Arduino. My goal is to build a timing system for slalom skateboarding, at the same time learn.
I understand a lot of the principles and have been reading (alot of this forum), googling and youtubeing, but when it come to actual programming, I seem to hit a block. A bit like maths.....
Anyway the timer will comprise of 2 tapeswitches, hardwired to the arduino and lcd. Will concentrate on a single lane before doing dual lane.
So far I have found some code and put togther a LCD, start/stop button, reset button and a LED. The timer works and the led flashes when timing is going on and I get a readout in sec and ms (thousands) just as I want.
I'll start off with with just a couple of questions and as the project comes along I'll probably ask more.
-
As for the start/stop button, how do I stop it registering a second press close to the first press The first set of wheels trigger the start of the timer and the button needs to be inactive as the rear wheels press the tape switch. Say a blanking time of 3 seconds?
Or would it be better to split the start/stop button into 1 start and 1 stop button(tapeswitch) ? -
My timer runs, but nothing is displayed on the LCD until the the timer is stopped. Not a problem, but would be nice to see the timer running on the LCD "live" Need a bit of guidance.
Thank you in advance and hope I can learn in the process. Lots to implement like dual lane, random beep start, false start routine, printout to excel etc etc, but one step at a time.....
#include <LiquidCrystal.h>
#define ledPin 10
#define startstopPin 6
#define resetPin 7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int value = LOW; // previous value of the LED
int buttonState; // variable to store button state
int stateReset;
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);
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(startstopPin, INPUT); // not really necessary, pins default to INPUT anyway
pinMode(resetPin, INPUT);
digitalWrite(startstopPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
digitalWrite(resetPin, HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("--Stop Watch--");
lcd.setCursor(0, 1);
lcd.print("System ready!");
}
void loop()
{
// check for button press
buttonState = digitalRead(startstopPin); // read the button state and store
stateReset = digitalRead(resetPin);
if (buttonState == LOW && lastButtonState == HIGH && blinking == false){ // check for a high to low transition
// if true then found a new button press while clock is not running - start the clock
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(5); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lcd.clear();
lcd.print("--Stop Watch--");
lcd.setCursor(0, 1);
lcd.print("Started timing!");
}
else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){ // check for a high to low transition
// if true then found a new button press while clock is running - stop the clock and report
elapsedTime = millis() - startTime; // store elapsed time
blinking = false; // turn off blinking, all done timing
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
// routine to report elapsed time
Serial.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print("."); // print decimal point
// use modulo operator to get fractional part of time
fractional = (int)(elapsedTime % 1000L);
// pad in leading zeros - wouldn't it be nice if
// Arduino language had a flag for this? :)
if (fractional == 0)
Serial.print("000"); // add three zero's
else if (fractional < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
Serial.print("00"); // add two zeros
else if (fractional < 100)
Serial.print("0"); // add one zero
Serial.println(fractional); // print fractional part of time
// set the cursor to column 0, line 2
// (note: line 1 is the second row, since counting begins with 0):
lcd.clear();
lcd.print("--Stop Watch--");
lcd.setCursor(0, 0);
lcd.print("Run over! time:");
lcd.setCursor(0, 1);
lcd.print((int)(elapsedTime / 1000));
lcd.print(":");
//lcd.setCursor(10, 1);
lcd.print(fractional);
lcd.print(" ms");
}
else{
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
if (stateReset == LOW) {
blinking = false;
lcd.clear();
lcd.print("--Stop Watch--");
lcd.setCursor(0, 1);
lcd.print("System ready!");
}
// blink routine - blink the LED while timing
// check to see if it's time to blink the LED; that is, the difference
// between the current time and last time we blinked the LED is larger than
// the interval at which we want to blink the LED.
if ( (millis() - previousMillis > interval) ) {
if (blinking == true){
previousMillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin, value);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}
}
}