Lap Counter (Using sensor entry) with display of time elapsed

I have an Uno which counts laps and has an elapsed time that displays on a 20x4 LCD via i2c.

I would like to have a button which resets the "count" which DOES work, and another button that resets the timer. This does NOT work... and I have tried many iterations on how.

Also, I would like to display a quantity below "Lap Count" which displays laps per hour.... I have yet to make this work and not sure how.

Fairly novice here so any help is much appreciated.

#include <Wire.h>  // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

int ledPin = 13; // choose the pin for the LED
int switchPin =6; // choose the input pin (for a pushbutton)
int resetCount =9;
int resetTime =5;
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;
int button1State = 0;
int button2State = 0;

unsigned long startTime;
unsigned long elapsedTime;
unsigned long elapsedSec;
unsigned long elapsedSecDisp;
unsigned long elapsedMin;
unsigned long elapsedMinDisp;
long elapsedHr;
long lapsPh;

void setup() {

 lcd.begin(20,4);   // initialize the lcd for 16 chars 2 lines, turn on backlight
 pinMode(switchPin,INPUT);

 lcd.backlight();
 lcd.setCursor(5,0);
 lcd.print("Lap Counter");
 //lcd.setCursor(4,1);
 //lcd.print("Lap Counter");
 lcd.setCursor(0,2);
 lcd.print("Laps:");

 elapsedTime = millis()/1000;
 
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPin, INPUT); // declare pushbutton as input
pinMode(resetCount, INPUT);
pinMode(resetTime, INPUT);

Serial.begin(9600);
}


void loop(){

elapsedTime = millis()/1000; //determine duration current millis - start millis
 
 elapsedSec = elapsedTime;
 elapsedSecDisp = elapsedTime - (elapsedMin*60);
 elapsedMin = elapsedSec/60;
 elapsedMinDisp = elapsedMin - (elapsedHr*60);
 elapsedHr = elapsedMin/60;
 lapsPh = (counter/(elapsedHr + (elapsedMin/60) + (elapsedSec*3600)));

lcd.setCursor(8,2);
if (counter < 10)
lcd.print(' ');
if (counter < 100)
lcd.print(' ');
if (counter < 1000)
lcd.print(' ');
if (counter < 10000)
lcd.print(' ');
if (counter < 100000)
lcd.print(' ');
lcd.print(counter);
lcd.setCursor(12,3);
if (elapsedHr < 10)
lcd.print('0');
lcd.print(elapsedHr);
lcd.print(":");
if (elapsedMin < 10)
lcd.print('0');
lcd.print(elapsedMinDisp);
lcd.print(":"); 
if (elapsedSecDisp < 10)
lcd.print('0');
lcd.print(elapsedSecDisp);
lcd.setCursor(0,3);
lcd.print("LPH: ");
//lcd print for laps per hour don't work
//lcd.print(lapsPh);

//lap count reset
//this one works
button1State = digitalRead(resetCount);
if (button1State == HIGH) {
   if (counter >= 0) {
     counter = 0;
   } 
}

//time elapased reset
//this one is broken
button2State = digitalRead(resetTime);
if (button2State == HIGH) {
   if (elapsedSec >= 0) {
     elapsedSecDisp = 0;
     elapsedMinDisp = 0;
     elapsedHr = 0;
   } 
}


}

I'm rapidly running out of these [code][/code], but you need them more than me.

Not sure what you mean.... could you be more specific?

Scratch that- sorry didn't know there was a markup for that.

pinMode(resetCount, INPUT);
pinMode(resetTime, INPUT);

what about pullups???

pinMode(resetCount, INPUT_PULLUP);

backwoodsjack:
pinMode(resetCount, INPUT);
pinMode(resetTime, INPUT);

what about pullups???

pinMode(resetCount, INPUT_PULLUP);

Tried that, didn't help at all.

What I would like to do, is have the button connected to DIO5, on press, reset the variables elapsedSecDisp, elapsedMinDisp, and elapsedHr. I have separate display values as In the code, I am using the non Disp values to calculate other values, ie elapsedSec is used to calculate elapsedMin, and elapsedMin is used to calculate elapsedHr, but then I haven't figured out how to say, minutes hits 60, +1 on hours and reset minutes.

Maybe there is a better way to do that too- but that is for an efficiency and size cleanup later. For now I would like to see the reset buttons work as expected which shouldn't be too hard.

if (button2State == HIGH) {
if (elapsedSec >= 0) {
elapsedSecDisp = 0;
elapsedMinDisp = 0;
elapsedHr = 0;

why do u care if elapsedSec>=0 ??
just reset everything...

have u tried that..

Have you confirmed with a simple sketch that the button is wired correctly and that a press of the button is read as you intend? You could have a hardware problem.

backwoodsjack:
if (button2State == HIGH) {
if (elapsedSec >= 0) {
elapsedSecDisp = 0;
elapsedMinDisp = 0;
elapsedHr = 0;

why do u care if elapsedSec>=0 ??
just reset everything...

have u tried that..

I don't care really- would I just remove the if statement?

yes of course…

why dont u digitalWrite to a LED for that function also…so u know for sure the hardware is right, like cattledog said…

cattledog:
Have you confirmed with a simple sketch that the button is wired correctly and that a press of the button is read as you intend? You could have a hardware problem.

It is configured identical to the other button, resetCount which does work. I have tried different buttons, and resistors etc, there is no reason it shouldn't work. Depending on some changes I tried on the code, it kind of worked (ie, would set to zero) but then it would continue counting where it was at before... such as 33, 34, 35, 00, 36, 37... etc so it wasn't fully resetting.

If I do a digital write to LED it does work. So for whatever reason it isn't resetting the actual variable.

u dont check for toggle, u are just checking for
if (button2State == HIGH)

which is the default state, apparently..

u need to test for toggle

@ backwoods has a good idea

which is the default state, apparently..

Please provide a sketch of how your buttons are wired. External pullups/pulldowns?

Have you connected diagonally across the switch? Have you verified with a multimeter that you are connected to the switched legs.

It is configured identical to the other button, resetCount which does work

You think it is identical but most likely it is not. Your code should be functional.

Will this work? I don’t have a diagram built yet…

IN5 Is the button that isn’t working as intended
IN6 takes the signal out from the infrared line tracking module.
IN9 Is the counter reset button that does work.

EDIT:


Sorry, the file was attached but was too large, and I have a limit to how often I can post apparently. Limited to once every 5 minutes. Any way to remove that?

Any way to remove that?

Nope.
Not yet anyway.

AWOL:
Nope.
Not yet anyway.

Thanks for being patient with the new guy to the forum.

I hadn't even looked at C# or an Arduino board before last Sunday, so still mastering a few things but have worked with low voltage, basic web development and computers in a professional capacity the past 12 years or so.

backwoodsjack:
if (button2State == HIGH) {
if (elapsedSec >= 0) {
elapsedSecDisp = 0;
elapsedMinDisp = 0;
elapsedHr = 0;

why do u care if elapsedSec>=0 ??
just reset everything...

have u tried that..

Removed the IF statements, and I still do not get anything out of the resetTime button, I changed the INT values to switch the roles of both buttons and the second button worked for the resetCount variable as well, the other did not work for the resetTime function.

Is it not wired correctly? (see uploaded picture a couple posts back....)

Good move exchanging the buttons to prove that you have a hardware problem.

Rotate the button which does not work by 90 degrees. These buttons have two legs which are always connected and two that are switched. You are wired across a connected pair. If you can't test the configuration of the button with a multimeter, it always works to wire diagonally across the button.

It also more common to use INPUT_PULLUP mode instead of external pull down resisitors. It simplifies the wiring. One wire from a button leg to the input pin which is set to INPUT_PULLUP and the other wire from diagonally across the switch to ground. The logic is reversed with not pressed == HIGH and pressed == LOW.

cattledog:
Good move exchanging the buttons to prove that you have a hardware problem.

Rotate the button which does not work by 90 degrees. These buttons have two legs which are always connected and two that are switched. You are wired across a connected pair. If you can’t test the configuration of the button with a multimeter, it always works to wire diagonally across the button.

It also more common to use INPUT_PULLUP mode instead of external pull down resisitors. It simplifies the wiring. One wire from a button leg to the input pin which is set to INPUT_PULLUP and the other wire from diagonally across the switch to ground. The logic is reversed with not pressed == HIGH and pressed == LOW.

No- I swapped the buttons on the breadboard, and nothing changed (BOTH buttons worked on the input set to resetCount, exactly as placed on the board). I modified the code to switch the input pins around and the issue followed the variable, not the button.

Not sure I follow you on the input_pullup, can you reference a page or share a diagram as to what you mean?

Not sure I follow you on the input_pullup, can you reference a page

You mean the reference page?