Hi guys,
I've been googling help for some time before deciding to come to this forum to seek some help and assistance (Mind me for my English, I'm not proficient in it). As its my first time using Arduino (I have no choice as its been assigned to me for my final year project), I have little knowledge about this program as previously I'm using Microsoft VisualStudio C++ for all my programming.
Right, so basically, the board I have to use is an Arduino UNO SMDEDITION, I've already tried to come out with a program but the results do appear in the serial monitor in my laptop, but it actually doesn't show up on my LCD display. Shown below is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
unsigned long start, finished, elapsed;
int frameRate = 100; // the frame rate (frames per second) at which the stopwatch runs - Change to suit
long interval = (1000/frameRate); // blink interval
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int fractional; // variable used to store fractional part of Frames
int fractionalSecs; // variable used to store fractional part of Seconds
int fractionalMins; // variable used to store fractional part of Minutes
int elapsedFrames; // elapsed frames for stop watch
int elapsedSeconds; // elapsed seconds for stop watch
int elapsedMinutes; // elapsed Minutes for stop watch
char buf[10]; // string buffer for itoa function
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT); // the start button
pinMode(3, INPUT); // the stop button
Serial.println("Press 1 for Start/reset, 2 for elapsed time");
}
void displayResult()
{
float h, m, s, ms;
unsigned long over;
elapsed = finished - start;
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
Serial.print("Elapsed time: ");
Serial.print(h, 0);
Serial.print("h ");
Serial.print(m, 0);
Serial.print("m ");
Serial.print(s, 0);
Serial.print("s ");
Serial.print(ms, 0);
Serial.println("ms");
Serial.println();
}
void loop()
{
if (digitalRead(2) == HIGH)
{
start = millis();
delay(200); // for debounce
Serial.println("Started...");
}
if (digitalRead(3) == HIGH)
{
finished = millis();
delay(200); // for debounce
displayResult();
}
elapsedTime = millis() - startTime; // store elapsed time
elapsedMinutes = (elapsedTime / 60000L);
elapsedSeconds = (elapsedTime / 1000L); // divide by 1000 to convert to seconds - then cast to an int to print
elapsedFrames = (elapsedTime / interval); // divide by 100 to convert to 1/100 of a second - then cast to an int to print
fractional = (int)(elapsedFrames % frameRate); // use modulo operator to get fractional part of 100 Seconds
fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
lcd.clear(); // clear the LDC
if (fractionalMins < 10){ // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalMins, buf, 10)); // convert the int to a string and print a fractional part of 60 Minutes to the LCD
lcd.print(":"); //print a colan.
if (fractionalSecs < 10){ // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractionalSecs, buf, 10)); // convert the int to a string and print a fractional part of 60 Seconds to the LCD
lcd.print(":"); //print a colan.
if (fractional < 10){ // pad in leading zeros
lcd.print("0"); // add a zero
}
lcd.print(itoa(fractional, buf, 10)); // convert the int to a string and print a fractional part of 25 Frames to the LCD
}
I would really appreciate if anyone could really help me correct/add the codings required into this program to make everything work as this is quite urgent. Thanks!
Below attached is the sample picture of the whole project, basically, I got to program the stopwatch (as shown with the 3 buttons) and also add fibre sensors at the tank, so as to start and stop the time with the object passes it. (Sensors that I'm using is shown here: www.keyence.co.jp/switch/fiber/fu/fu_e40).