void loop(){ //This is to set the running status and light the LED
if (Button2Press == 0){
val = digitalRead(inputPin2);
delay(100);
bounceCheck = digitalRead(inputPin2);
if (val == bounceCheck){
if (val == LOW){
digitalWrite(ledPin,HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("READY TO START");
lcd.setCursor(0,1);
lcd.print("BEST ");
lcd.print(fastesttotal/1000);
Running = true;
Button2Press = 1;
wheelRevs = 0;
delay(1000);
}
}
}
if (Button2Press == 1){ //This is to reset the running status and turn off the LED
val = digitalRead(inputPin2);
delay(100);
bounceCheck = digitalRead(inputPin2);
if (val == bounceCheck){
if (val == LOW){
digitalWrite(ledPin,LOW);
digitalWrite(fastPin,LOW);
lcd.clear();
lcd.print("LAST ");
lcd.print(s1time/1000);
lcd.setCursor(11,0);
lcd.print(s2time/1000);
lcd.setCursor(5,1);
lcd.print(s3time/1000);
lcd.setCursor(11,1);
lcd.print(s4time/1000);
Running = false;
Button2Press = 2;
delay(1000);
}
}
}
if (Button2Press == 3){
val = digitalRead(inputPin2);
delay(100);
bounceCheck = digitalRead(inputPin2);
if (val == bounceCheck){
if (val == LOW){
lcd.clear();
lcd.print("LAST ");
lcd.print((s1time+s2time+s3time+s4time)/1000);
lcd.setCursor(0,1);
lcd.print("TARGET ");
lcd.print((s1fastest+s2fastest+s3fastest+s4fastest)/1000);
Button2Press = 0;
delay(1000);
}
}
}
if (Button2Press == 2){ //This is to reset the running status and turn off the LED
val = digitalRead(inputPin2);
delay(100);
bounceCheck = digitalRead(inputPin2);
if (val == bounceCheck){
if (val == LOW){
lcd.clear();
lcd.print("BEST ");
lcd.print(s1fastest/1000);
lcd.setCursor(11,0);
lcd.print(s2fastest/1000);
lcd.setCursor(5,1);
lcd.print(s3fastest/1000);
lcd.setCursor(11,1);
lcd.print(s4fastest/1000);
Button2Press = 3;
delay(1000);
}
}
}
if (Running){
if (wheelRevs == 1) {
starttime = millis(); //Sets the clock running the first time the sensor is tripped
// time = 0;
digitalWrite(ledPin,LOW);
lcd.clear();
lcd.print("RUNNING");
}
if (wheelRevs == sector1){
s1time = millis() - starttime;
if (s1fastestrun > 1){
lcd.clear();
delta = (s1time - s1fastestrun)/1000;
if (delta > 0){
lcd.print("+");
}
lcd.print(delta);
}
if (s1fastest == 0){
s1fastest = s1time;
}
if (s1time < s1fastest){
s1fastest = s1time;
digitalWrite(fastPin,HIGH);
}
if (s1time > s1fastest){
digitalWrite(ledPin,HIGH);
}
}
if (wheelRevs == sector2){
s2time = millis() - starttime - s1time;
if (s2fastestrun > 1){
lcd.clear();
delta = (s2time + s1time - s1fastestrun - s2fastestrun)/1000;
if (delta > 0){
lcd.print("+");
}
lcd.print(delta);
}
if (s2fastest == 0){
s2fastest = s2time;
}
if (s2time < s2fastest){
s2fastest = s2time;
digitalWrite(fastPin,HIGH);
digitalWrite(ledPin,LOW);
}
if (s2time > s2fastest){
digitalWrite(ledPin,HIGH);
digitalWrite(fastPin,LOW);
}
}
if (wheelRevs == sector3){
s3time = millis() - starttime - s2time - s1time;
if (s3fastestrun > 1){
lcd.clear();
delta = (s1time + s2time + s3time - s1fastestrun - s2fastestrun - s3fastestrun)/1000;
if (delta > 0){
lcd.print("+");
}
lcd.print(delta);
}
if (s3fastest == 0){
s3fastest = s3time;
}
if (s3time < s3fastest){
s3fastest = s3time;
digitalWrite(fastPin,HIGH);
digitalWrite(ledPin,LOW);
}
if (s3time > s3fastest){
digitalWrite(ledPin,HIGH);
digitalWrite(fastPin,LOW);
}
}
if (wheelRevs == courserevs){
s4time = millis() -starttime - s3time - s2time - s1time;
if (s4fastestrun > 1){
lcd.clear();
delta = (s4time + s3time + s2time + s1time - s1fastestrun - s2fastestrun - s3fastestrun - s4fastestrun)/1000;
if (delta > 0){
lcd.print("+");
}
lcd.print(delta);
}
if (s4fastest == 0){
s4fastest = s4time;
}
digitalWrite(fastPin,LOW);
if (s4time < s4fastest){
s4fastest = s4time;
digitalWrite(fastPin,HIGH);
}
Running = false; //Automatically turn off at the finish
delay(2000);
digitalWrite(ledPin,HIGH);
delay(5000);
digitalWrite(fastPin,LOW);
digitalWrite(ledPin,LOW);
lcd.clear();
lcd.print("Total=");
lcd.print((s1time+s2time+s3time+s4time)/1000);
lcd.setCursor(0,1);
lcd.print("Delta=");
lcd.print(delta);
if (delta <= 0){
s1fastestrun = s1time;
s2fastestrun = s2time;
s3fastestrun = s3time;
s4fastestrun = s4time;
fastesttotal = s1time+s2time+s3time+s4time;
}
s1fastestw = word(s1fastest);
s2fastestw = word(s2fastest);
s3fastestw = word(s3fastest);
s4fastestw = word(s4fastest);
s1fastestrunw = word(s1fastestrun);
s2fastestrunw = word(s2fastestrun);
s3fastestrunw = word(s3fastestrun);
s4fastestrunw = word(s4fastestrun);
EEPROM.write(2,highByte(s1fastestrunw));
EEPROM.write(3,lowByte(s1fastestrunw));
EEPROM.write(4,highByte(s2fastestrunw));
EEPROM.write(5,lowByte(s2fastestrunw));
EEPROM.write(6,highByte(s3fastestrunw));
EEPROM.write(7,lowByte(s3fastestrunw));
EEPROM.write(8,highByte(s4fastestrunw));
EEPROM.write(9,lowByte(s4fastestrunw));
EEPROM.write(10,highByte(s1fastestw));
EEPROM.write(11,lowByte(s1fastestw));
EEPROM.write(12,highByte(s2fastestw));
EEPROM.write(13,lowByte(s2fastestw));
EEPROM.write(14,highByte(s3fastestw));
EEPROM.write(15,lowByte(s3fastestw));
EEPROM.write(16,highByte(s4fastestw));
EEPROM.write(17,lowByte(s4fastestw));
}
}
}
/code]