Go Down

Topic: Stopwatch timer 3 digits after seconds (Read 904 times) previous topic - next topic

martinius96

Hello there, I want timer stopwatch for Arduino Uno and LCD I2C display.
Code now working that: MINUTEMINUTE:SECONDSSECONDS:DIGIT i want MINUTEMINUTE:SECONDSSECONDS:DIGITDIGITDIGIT Is it possible with millis or must i use micros? Can someone make a example code for that?
Code for try it at you: I want 00:00:000 instead of 00:00:0.
Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x3F  // Define I2C Address where the PCF8574A is
                          // Address can be changed by soldering A0, A1, or A2
                          // Default is 0x27

// map the pin configuration of LCD backpack for the LiquidCristal class
#define BACKLIGHT_PIN 3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C lcd(I2C_ADDR,
                      En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,
                      BACKLIGHT_PIN, POSITIVE);

const int spustacresettlacidlo = 12;     //The pin for spustacresettlacidlo

long lastPressedspustacresettlacidlo = 0;    //Last millisecond we pressed spustacresettlacidlo

boolean run = false;         //Should the system run?

int spustacresettlacidloState = LOW;      //The value the spustacresettlacidlo reads
int spustacresettlacidloCurrState = LOW;  //The last value of spustacresettlacidlo



long timer = 0; //The timer

int second = 0;
int minute = 0;
int tenth = 0;


void setup() {
  lcd.begin(20, 4);                // intialise the LCD.
     lcd.setBacklight(HIGH); // Turn on backlight, LOW for off
  pinMode(spustacresettlacidlo, INPUT);
  lcd.setCursor(0,0);
  lcd.print("00:00:0 L");
   lcd.setCursor(0,1);
   lcd.print("00:00:0 P");

}

void tickClock() {
  Serial.println(millis() /10);
  if((timer - millis()/100) >= 100 || timer == 0) {
    tick();
    timer = millis()/100;
  }
}

void loop() {
  tickClock(); //Start ticking the clock
  spustacresettlacidloState = digitalRead(spustacresettlacidlo);
  checkStart();
}

void checkStart() {
  if(spustacresettlacidloState != spustacresettlacidloCurrState) {    //Check if the button state has changed
    if(spustacresettlacidloState == HIGH && (millis()-lastPressedspustacresettlacidlo) > 100 || lastPressedspustacresettlacidlo == 0) {
      lastPressedspustacresettlacidlo = millis();
      run = !run; //Switch the running state
    }   
  }
  spustacresettlacidloCurrState = spustacresettlacidloState;    //Set the current state equals the button state
}


void tick() {
  if(run) {
    updateLCD();
   
   
   
    if(tenth == 9) {
      tenth = 0;
     
      if(second == 59) {
        second = 0;
        minute++;
       
      } else {
        second++;
      }
    } else {
    tenth++;
    }
  }
}

void updateLCD() {
  //
   lcd.setCursor(0,0);
  if(minute < 10) {      // If hour does not have 2 digits
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");

  if(second < 10) {    // If minute does not have 2 digits
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.print(":");

  lcd.print(tenth, DEC);
  //
  lcd.setCursor(0,1);
  if(minute < 10) {      // If hour does not have 2 digits
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");

  if(second < 10) {    // If minute does not have 2 digits
    lcd.print("0");
  }
  lcd.print(second, DEC);
  lcd.print(":");

  lcd.print(tenth, DEC);
}

Sorry for bad english. I want it for stopwatch for firesport. Thanks a lot! I dont have experiences with time.
Photo of actual project:
Arduino and website programmer

MarkT

#1
Sep 08, 2017, 03:55 pm Last Edit: Sep 08, 2017, 03:56 pm by MarkT
Code: [Select]

void tickClock() {
  Serial.println(millis() /10);
  if((timer - millis()/100) >= 100 || timer == 0) {
    tick();
    timer = millis()/100;
  }
}

should be
Code: [Select]

void tickClock() {
  if (millis() - timer > 0)
  {
    timer += 1 ;
    tick();
  }
}

And then you need to call "tenth" "thousandth", and print all 3 of its digits:
Code: [Select]

  lcd.print (thousandth/100) ;
  lcd.print (thousandth/10%10) ;
  lcd.print (thousandth%10) ;
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

odometer

Code: [Select]

void tickClock() {
  Serial.println(millis() /10);
  if((timer - millis()/100) >= 100 || timer == 0) {
    tick();
    timer = millis()/100;
  }
}

should be
Code: [Select]

void tickClock() {
  if (millis() - timer > 0)
  {
    timer += 1 ;
    tick();
  }
}

And then you need to call "tenth" "thousandth", and print all 3 of its digits:
Code: [Select]

  lcd.print (thousandth/100) ;
  lcd.print (thousandth/10%10) ;
  lcd.print (thousandth%10) ;

I think that this approach might well be doomed to fail.
You are attempting to write to the display 1000 times per second, using probably a rather inefficient library, no less. My guess is that this would cause the clock would fall far behind.
Probably better to stick to displaying tenths of a second when the clock is running, and only show thousandths when paused or stopped.

martinius96

No, working bad. :(  Can you try to fill whole code with that? There is function, where there are increment of each variable..
Arduino and website programmer

martinius96

Arduino and website programmer

martinius96

Arduino and website programmer

Go Up