Standalone Arduino StopWatch - SainSmart LCD Keypad Shield

12K109

Intro: Standalone Arduino StopWatch - SainSmart LCD Keypad Shield

So, for my first Instructable, I'll also share with you my first project: A Standalone Arduino StopWatch.



I must admit that this wasn't FULLY created by me. I took the idea of another stopwatch where you read the info on the serial port and then changed it to use the Sainsmart LCD Keypad Shield.

The original project, which needed electronic assembly can be found here.



So, being my first project, I learned A LOT about programming in C (I have a few basic knowledge of programming, but thats it). Knowing there are a lot of guys with the same issues as me, I commented the HECK out of this code (and even included some redundancies on purpose, to explain the concepts further). I hope all the pros here will forgive me, but it's also a way for me to know if I got things right when you all review it :P


So let's get down to business!


Materials needed:

- Arduino Uno or equivalent (that fits the Shield);
- Sainsmart LCD Keypad Shield;
- Aditional power source (I use this) - optional.

___________________________________

I'm assuming you already know how to upload sketches to your Arduino.


Connect the shield and upload the code.
Use your external battery (or USB connection) to power the Arduino and have fun!

___________________________________

Final Notes:

If you reviewed the code, you will note commented redundancies, as well as LOTS of commenting.
I did this to explain some stuff that I had to find out for myself.

I hope the n00bs (like me :D) will find it helpfull.


Buttons on the Shield:
I found online 2 ways to use the shield's buttons:
- http://www.hobbytronics.co.uk/arduino-lcd-keypad-shield
- http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)

The first one uses conditions to find out which button is being pressed.
The second one actually defines the buttons.

For clarity, I used the first one, as I think is easier to follow up how the numbers are calculaded (check the code on the first link).
On my sketch I used the line: "if (x < 600 && x > 400)", unlike the original one on the link, so that ONLY the intended button works, not any other.

You can re-write the program to the second option if you like, it will help you understand further programming in C.

To be honest, I haven't completely understand how the Arduino takes a number between 400 and 600 and categorizes it as the LEFT button, anyone care to explain? :D


The millis() function:
As I understand, you can't make the clock start counting ONLY when you press the button. It starts right away when the Arduino is turned on and the program starts running (press SELECT before starting the counting and it will return the elapsed time since it's on). The millis() function is called and stores that time into a variable when you start counting (so it knows when it started) and when you want to see the elapsed time (so it knows when it "stopped") and does the math from there.
Nothing special, just thought I'd share this cool finding.



So here's my first project, hope you enjoy, and please, for the sake of my learning curve, make reviews, critics, suggestions! :D


Have fun!

8 Comments

Your sketch has a flaw in it as follows;
Here is the sequence of time printed on the Serial Monitor.

The lcd monitor is missing the leading zero(s) also.
0 h 0 m 3 s 944 ms
0 h 0 m 3 s 965 ms
0 h 0 m 3 s 985 ms
0 h 0 m 4 s 7 ms (The 7 should have 2 zeros in front of it: 4 s 007ms)
0 h 0 m 4 s 25 ms (The 25 should have 1 zero in front of it 4 s 025ms)
0 h 0 m 4 s 45ms (4 s 045 ms)
0 h 0 m 4 s 64 ms (4 s 64 ms)
0 h 0 m 4 s 84 ms (4 s 084 ms)
0 h 0 m 4 s 104 ms
0 h 0 m 4 s 124 ms

The following is the solution:
lcd.print(m, 0);
lcd.print(" m ");
lcd.print(s, 0);
lcd.print(".");
if (ms < 100) lcd.print ("0"); // THIS LINE NEEDS TO BE ADDED
if (ms < 10) lcd.print ("0"); // THIS LINE NEEDS TO BE ADDED
if (h < 10){
lcd.print(ms, 0);
}
lcd.print(" sec. "); // The code is modified to display x.xxx sec. which suits my //purpose better.

Thanks for your comment. It's not really a bug, it just wasn't done with that in mind because of simplicity.

Long are the years I did this so I don't remember if there are any "shortcuts" in the C language for it, but you could convert each number to string, and based on it prepend the leading zeros.

Something like (metacode from memory, not sure if it works in C directly):

[.......]
String milliseconds = String(ms);
int millisecondsLength = milliseconds.length();

if (millisecondsLength < 10) {
milliseconds = "00" + milliseconds;
} else if (millisecondsLength < 100) {
milliseconds = "0" + milliseconds;
}

lcd.print(milliseconds, 0);
lcd.print("ms");
[.......]

This should display correctly.


thanks for the reply. I didn't see it until after I updated my post. Your solution should work also.

Thank you for this. Sometimes those of us new to the Arduino are easily overwhelmed by some of the code, assembly and instructions for some intro projects. This allowed me to just deal with the keypad/./lcd shield while not having to worry about other components confusing things. It was really helpful to this n00b ;) - exactly what I needed, thanks!

"To be honest, I haven't completely understand how the Arduino takes a number between 400 and 600 and categorizes it as the LEFT button, anyone care to explain? :D"

Answer:

Since all of the buttons are using a single analogue pin, Each button can complete the circuit. The voltage supplied to the buttons is 5V. The Analogue pin has an analogue to digital converter (ADC) which converts a certain voltage detected to a digital number with a maximum value of 1024.

So:

- 0 volts reads as a digital 0

- 1 volt reads as a digital 205 (approximately)

- 2 volts a digital 410 (approximately)

- 5 volts a digital 1024 (and any voltage higher such as 5.3 also reads as 1024)

So on the left button they place a resistor which in parallel with another resistor feeds between 1 volt(digital 200) and 2 volts(digital 400)

and so on for the rest of the buttons.

The reset button is (should be) directly connected to the reset pin.

Really good

can you edit is so it can be stopped and started when wanted without having to hold down select?

Hey,

nice work. where can I find the sketch please?

BR
Andreas