Lap Timer

Hi guys, seems like I have almost finished my lap timer build. (first ever project)

I have it all working well, I just need a quick pointer,

I have the timer displaying in just milliseconds, so if i did 1 lap in 1.350 seconds, it would simply display as 1350. Is there a quick and easy way to change this to read with a decimal point, so it would say 1.350.

Here is the code I am currently using. Any help would be much appreciated.

#include <LiquidCrystal.h>

/*

VERSION 0.3 

-Added Green LED For Best Round

*/


// PIN CONFIGURATION
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int laserPin = 13;
int buttonPin1 = 7;
int buttonPin2 = 8;
int speakerPin = 9;
int ledPin1 = 0; // red 
int ledPin2 = 1; // red
int ledPin3 = 10; // green


/*

 States // The central state the timer is in
 
 0 = pause;
 1 = countdown;
 2 = running;
 
*/


int state = 0;
boolean displayLastRound = true;
boolean running = false;

int lightLevel = 0;
long lastHit = 0; //time of the last hit in the barrier (absolut)
boolean lock; // locks the light barrier while a car is passing. The look is removed as soon the car left the lightbeam and the roundTreshold time has passed

long lastRound = 0; // time of the lastround (absolut)
long bestRound = 0; // time of the bestround (relative)
int roundNr = 0;


int laserTreshold = 10;
int roundTreshold = 300; // minimum time that has to pass before a new round is counted

// Absolut time for pressing buttons
long lastPress1 = 0;
long lastPress2 = 0;


// LED Timer

long timeLEDGreen = 0;

void setup() {
  pinMode(laserPin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);


  lcd.setCursor(0, 0);
  lcd.print("Arduino");
  lcd.setCursor(0, 1);
  lcd.print("Laser Lap Timer  ");
  
  delay(1500);
  
  lcd.setCursor(0, 0);
  lcd.print("Nath's Lap ");
  lcd.setCursor(0, 1);
  lcd.print("Timer    ");


  //Serial.begin(9600);
  delay(1500);

}

void loop() {

  if(!digitalRead(buttonPin1)){

    button1Pressed();

  };

  if(!digitalRead(buttonPin2)){

    button2Pressed();

  };



  switch (state){
  case 0: 
    statePaused(); 
    break;
  case 1: 
    stateCountdown(); 
    break;
  case 2: 
    stateRunning(); 
    break;
  }
}

void didFinishRound(){
  if(roundNr > 0)lastRound = millis()-lastHit;
  

  if (bestRound == 0){
    bestRound = lastRound;
  }
  if (lastRound < bestRound){
    bestRound = lastRound;
    timeLEDGreen = millis()+1000;
  }
  lastHit = millis();
  lock = true;  
  ++roundNr;
  displayLastRound = true;  

}

void statePaused(){
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(laserPin,LOW);
  lcd.setCursor(0, 0);
  lcd.print("Press right   ");
  lcd.setCursor(0, 1);
  lcd.print("button to start");

}

void stateRunning(){

  //switch laser on

  digitalWrite(laserPin,HIGH);


  //mesasure light

  lightLevel = analogRead(0);
  lightLevel = map(lightLevel, 0, 900, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);

  //Serial.println(lightLevel);
  // react to light

  if(lightLevel < laserTreshold){
    //digitalWrite(ledPin,LOW);
    lock = false;
  }
  else{
    // digitalWrite(ledPin,HIGH);
    if (millis()-lastHit > roundTreshold && lock == false){
      didFinishRound();
    }
  }

  // We have'nt finished the first round
  if(roundNr == 0){

    lcd.setCursor(0,0);
    lcd.print("Standby...      ");
    lcd.setCursor(0,1);
    lcd.print("                ");    

  }
  // Display the time and best / last round
  else{
    displaySecondLine();
    displayFirstLine();

  }
  
  // turn off all LED
  
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);

  if (timeLEDGreen > millis()){
      digitalWrite(ledPin3, HIGH);
  }
  
  else{
      digitalWrite(ledPin3, LOW);
  }
  
  



}

void stateCountdown(){

  lcd.clear();

  lcd.setCursor(0, 0);
  lcd.print("Ready             ");
  digitalWrite(ledPin1, HIGH);

  playNote('c',300);

  delay(1000);

  lcd.setCursor(0, 0);
  lcd.print("Set             ");
  digitalWrite(ledPin2, HIGH);

  playNote('c',300);

  delay(1000);

  lcd.setCursor(0, 0);
  lcd.print("Go             ");
  digitalWrite(ledPin3, HIGH);
  digitalWrite(laserPin,HIGH);

  playNote('g',600);

  state = 2;

}



void displayFirstLine(){

  lcd.setCursor(0, 0);


  lcd.print("Rd");
  lcd.print(" ");
  lcd.print(roundNr);
  lcd.print(" ");
  lcd.print(millis()-lastHit);
  lcd.print("ms");

  lcd.print("                       ");



}

void displaySecondLine(){

  lcd.setCursor(0, 1);

  if(displayLastRound){



    lcd.print("Last ");
    lcd.print(lastRound);
    lcd.print("ms");
    lcd.print("                       ");


  }



  else{
    lcd.print("Best ");
    lcd.print(bestRound);
    lcd.print("ms");
    lcd.print("                       ");

  }



}

void button1Pressed(){
  if(millis()-lastPress1 > 800){
    lastPress1 = millis();
    displayLastRound = !displayLastRound;
  }

}

void button2Pressed(){
  if(millis()-lastPress2 > 800){
    lastPress2 = millis();
    if (state == 0){
      state = 1;
    }
    if (state == 2){
      digitalWrite(laserPin,LOW);
      roundNr = 0;
      bestRound = 0;
      lastRound = 0;
      state = 0;
    }    

  }
}

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
void playNote(char note, int duration) {
  char names[] = { 
    'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'   };
  int tones[] = { 
    1915, 1700, 1519, 1432, 1275, 1136, 1014, 956   };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

Do your digits have the decimal points? Turn on the appropriate one.
If (time <=9), turn on none.
If (time>9 && time < 100), turn on first one
if (time >100 && time < 1000), turn on 2nd one

Is that not for a 7 segment display ? I am using a 16x2 LCD display.

Sorry if I am wrong, I am still very new to all this.

Thanks

Anyone ?

Im sure need to change this part of the code, but not sure how

}



void displayFirstLine(){

  lcd.setCursor(0, 0);


  lcd.print("Rd");
  lcd.print(" ");
  lcd.print(roundNr);
  lcd.print(" ");
  lcd.print(millis()-lastHit);
  lcd.print("ms");

  lcd.print("                       ");



}

void displaySecondLine(){

  lcd.setCursor(0, 1);

  if(displayLastRound){



    lcd.print("Last ");
    lcd.print(lastRound);
    lcd.print("ms");
    lcd.print("                       ");


  }



  else{
    lcd.print("Best ");
    lcd.print(bestRound);
    lcd.print("ms");
    lcd.print("                       ");

  }

play with this for a few values of myMilliseconds:

unsigned long myMilliseconds = 9071350;

void setup(void)
{
  Serial.begin(9600);
  int fullHours = myMilliseconds / 3600000; // get value for full hours
  myMilliseconds %= 360000; // lets work with the remainder
  int fullMinutes = myMilliseconds/60000;  // get value for full minutes
  myMilliseconds %= 60000; // lets work with the remainder
  int fullSeconds = myMilliseconds/1000;  // get a value for full seconds
  myMilliseconds %= 1000; //convert myMilliseconds to remainder.
  char timeRemaining[32] = "";  //create a buffer to put display value
  sprintf(timeRemaining, "%2d:%02d:%02d.%03d", fullHours, fullMinutes, fullSeconds, myMilliseconds);
  Serial.println(timeRemaining);
}

void loop(void)
{

}

this is all integer math...

Im gradually getting there when it comes to understanding what it all means, its just still a little baffling as to how to change parts of codes for new parts. if you catch my drift.

Nathanolder: Im gradually getting there when it comes to understanding what it all means, its just still a little baffling as to how to change parts of codes for new parts. if you catch my drift.

When you start to learn a new language, your vocabulary is limited... the more exposure you have (aka "learning") the broader your vocabulary...

Commit to learn just a few words a day/week and then voila, you become functionally literate.