no problem at all... i'm a bit further at the moment but have the idea that I can crop the code a lot so if anyone knows better way of handling... feel free to comment (especially writing to the display were the same code returns a lot of times but I did not succeed in putting it in a custom function)
a few comments are missing since I'm still working on the code. When you have questions feel free to ask.
but note that the IR beam itself has almost no programming since (in my project) breaking the beam just switches a relais so when you hook it up to 3/5V and to an input pin you just read a High or Low when the beam is broken and the relais is switched...
part 1
/* Nomothumu 0.13
* Vincent Riemersma 2008
*
* Program for timekeeping using a IR beam and a relais.
* Elapsed time is displayed on 7 segment displays.
*
*/
int ledPin = 13; // LED for blinking connected to digital pin 13
int buttonPin = 12; // Relais connected to digital pin 12 to start/stop the timing
int delayPin = 11; // LED for delay connected to digital pin 11
int modePin = 10; // Flipswitch connected to digital pin 10 to switch between single and continious mode
int resetPin = 9; // Button connected to digital pin 9 to reset timer
int delaybuttonPin = 8; // Button connected to digital pin 8 to close beam
int lastlapPin = 7; // Button to show last lap for 2 seconds
// pins to drive the 7 segment displays
int latchPin = 3; // Pin connected to ST_CP of 74HC595
int clockPin = 5; //Pin connected to SH_CP of 74HC595
int dataPin = 2; //Pin connected to DS of 74HC595
// bytes to store the numbers to be displyed on the 7 segment display without decimal point
byte cijfer[11] = {B11100111, B00100001, B11001011, B01101011, B00101101, B01101110, B11101110, B00100011, B11101111, B01101111, B00000000};
// bytes to store the numbers to be displyed on the 7 segment display including decimal point
byte cijfertwee[10] = {B11110111, B00110001, B11011011, B01111011, B00111101, B01111110, B11111110, B00110011, B11111111, B01111111};
int lastlap; // integer to store time of previous lap
int lastlapState; // State of the last-lap button
int lapcount = 0; // integer to store number of laps
int resetState; // state of the reset button
int delaybuttonState; // state of the delay button
int modeState; // flipwitch position for running mode
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(57600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(delayPin, OUTPUT);
pinMode(resetPin, INPUT);
pinMode(delaybuttonPin, INPUT);
pinMode(modePin, INPUT);
digitalWrite(resetPin, LOW);
digitalWrite(buttonPin, HIGH);
digitalWrite(delaybuttonPin, LOW);
digitalWrite(delayPin, LOW);
// at start up set time to 00.
digitalWrite(latchPin, LOW); // send to 7 segmetn displays
shiftOut(dataPin, clockPin, LSBFIRST, B11100111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B11110111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B0); // turn last 3 digits off
shiftOut(dataPin, clockPin, LSBFIRST, B0);
shiftOut(dataPin, clockPin, LSBFIRST, B0);
digitalWrite(latchPin, HIGH); // stop sending to 7 segment display
}
void loop()
{
int h,i,j,k,l,m,n, o; // 7 extra integers for time display
buttonState = digitalRead(buttonPin); // read the button state and store
resetState = digitalRead(resetPin); // read the button state and store
delaybuttonState = digitalRead(delaybuttonPin); // read the button state and store
modeState = digitalRead(modePin); // read the button state and store
lastlapState = digitalRead(lastlapPin); // read the button state and store
if (blinking == false && resetState == HIGH) // if time is not running and reset button is pressed reset lapcount to zero
{
lapcount = 0;
digitalWrite(latchPin, LOW); // send to 7 segmetn displays
shiftOut(dataPin, clockPin, LSBFIRST, B11100111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B11110111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B0); // turn last 3 digits off
shiftOut(dataPin, clockPin, LSBFIRST, B0);
shiftOut(dataPin, clockPin, LSBFIRST, B0);
digitalWrite(latchPin, HIGH); // stop sending to 7 segment display
digitalWrite(ledPin, LOW); // Turn blinking LED off (so it won't stay light when button preseed when LED is on)
digitalWrite(delayPin, HIGH); // Light delay LED for 2 seconds
delay(20); // Close beam for 2 seconds
digitalWrite(delayPin, LOW); // Turn off delay LED
}
else if (blinking == true && resetState == HIGH) // if time is running and reset button is pressed stop time, keep lapcount
{
blinking = false; // turn off timing while timing
digitalWrite(latchPin, LOW); // send to 7 segmetn displays
shiftOut(dataPin, clockPin, LSBFIRST, B11100111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B11110111); // set seconds to 0.
shiftOut(dataPin, clockPin, LSBFIRST, B0); // turn last 3 digits off
shiftOut(dataPin, clockPin, LSBFIRST, B0);
shiftOut(dataPin, clockPin, LSBFIRST, B0);
digitalWrite(latchPin, HIGH); // stop sending to 7 segment display
digitalWrite(ledPin, LOW); // Turn blinking LED off (so it won't stay light when button preseed when LED is on)
digitalWrite(delayPin, HIGH); // Light delay LED for 2 seconds
delay(2000); // Close beam for 2 seconds
digitalWrite(delayPin, LOW); // Turn off delay LED
}
if (lastlapState == true) // if lastlap button is pressed show last lap
{
if ((int)(lastlap /60000) < 1) // if time < 1 minute show as 12.345
{
h = (int)((lastlap / 10000L) % 10L); // get number of deca-seconds (10)
i = (int)((lastlap / 1000L) % 10L); // get number of seconds (1)
j = (int)((lastlap / 100L) % 10L); // get number of deci seconds (0,1)
k = (int)((lastlap / 10L) % 10L); // get number of centi seconds (0,01)
l = (int)(lastlap % 10L); // get number of milli seconds (0,001)
digitalWrite(latchPin, LOW); // send to 7 segmetn displays
shiftOut(dataPin, clockPin, LSBFIRST, cijfer[h]); // write deca seconds to first diplay
shiftOut(dataPin, clockPin, LSBFIRST, cijfertwee[i]); // write seconds + decimal point to seconds display
shiftOut(dataPin, clockPin, LSBFIRST, cijfer[j]); // write deci seconds to third display
shiftOut(dataPin, clockPin, LSBFIRST, cijfer[k]); // write centi seconds to fourth display
shiftOut(dataPin, clockPin, LSBFIRST, cijfer[l]); // write millie seconds to fifth display
digitalWrite(latchPin, HIGH);
}