Hello as i mentioned before Wireless analog read. - Project Guidance - Arduino Forum
im creating a project regarding a runner(about training,for personal evaluation).The idea is based on the stopwatch.Im using the CountUpDownTimer library Arduino Playground - HomePage currently trying it with push buttons. The issue is with the timer although it seems to be working correctly it needs some improvement.This is the code im currently using.The problem is with T.StartTimer(); that starts time.If it in setup area,the time starts before pressing the button even though you cannot see it, so supposedly 5 seconds passes and you press the button the time is printed from 6s and etc.If i leave T.StartTimer(); the first if(where i have it now) it creates a debounce so as long as i have the button pressed the time wont start,when i leave the button the time starts.That is the main issue that needs improving so the question is where T.StartTimer(); should be put to have the code be working correctly?
#include<CountUpDownTimer.h>
#define DOWN 0
#define UP 1
boolean startT =0;
const int buttonPin = 2;
const int buttonPin1 = 3;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
CountUpDownTimer T(UP);
unsigned long start, finished, elapsed;
float h, m, s, ms;
unsigned long over;
void setup()
{
//lcd.begin(16, 2);
Serial.begin(115200);
//lcd.clear();
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// print out the value you read:
if (buttonState == HIGH) {
startT=1 ;
T.StartTimer(); //Start the timer sequence
}
if (startT==1){
T.Timer(); //This is the actual timer in the library
elapsed = T.ShowMilliSeconds();
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
Serial.print(m, 0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.println();
}
if (buttonState1 == HIGH) {
startT=0 ;
}
}
^^Thats the last resort, its not the problem of the library though as using another timer with millis() instead of this has the same result.So the issue remains and i cant figure it out!
ShowMilliSeconds counts up regardless of direction. (I did that on purpose hint hint)
Also these should have UL on the ends of them.
elapsed = T.ShowMilliSeconds();
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
i understood the ul part but not the first suggestion.As English is my second speaking language i apologize for asking to make it more clear for me, thank you in advance for your reply and understanding.
the millis() function starts as soon as the arduino is turned on, and ShowMilliseconds() just takes the difference from the current millis() and when you started the timer. It always counts up regardless of direction. If you want it to count down then you need to subtract your timer value from ShowMilliseconds().
The library's?That wouldnt cross my mind.Well i said i didnt want to disturb you until i couldnt find the solution somewhere else.So is my problem solvable?Thank you very much.
+Ive used your code without changing it on another try but had issues with transforming to centiseconds(showmilliseconds - showseconds*1000) although it worked , after 10 seconds it adds a digit .
the code runs fine, the problem is with the placement startrimer(). if i leave where it is ,as long as i am pushing the button the time wont start, only when i push and leave instantly the button does the time starts.
To be more precise, my project involves 2 sets of infrared beams instead of buttons, 1 at start and one at finish. When i pass through the start the times begins to count. If i leave starttimer() like this and the issue exists ,as long as my body passes through the sensor the time wont start before i completelly pass,the same happens on finish.Through running i loose only some centiseconds but it is essential to be precise and not loose anything!
You dont need my timer library for that. I mean yes you could use it, but it is not necessary (its kinda overkill for something so simple).
Like this.
const byte ButtonOne = 2;
const byte ButtonTwo = 3;
unsigned long PrevTime;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(ButtonOne, INPUT);
pinMode(ButtonTwo, INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if (digitalRead(ButtonOne) == HIGH)
PrevTime = millis();
if (digitalRead(ButtonTwo) == HIGH)
Serial.println(millis() - PrevTime);
}
if i set the code like this the pressing button problem is gone, but as you can see if you run it adds an extra digit after the 10th second so it makes the lcd reading difficult
#include<CountUpDownTimer.h>
#include <LiquidCrystal.h>
//LiquidCrystal lcd(9, 8, 7, 6, 5,4);
#define DOWN 0
#define UP 1
boolean startT =0;
const int buttonPin = 2;
const int buttonPin1 = 3;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
CountUpDownTimer T(UP);
unsigned long over;
void setup()
{
//lcd.begin(16, 2);
Serial.begin(115200);
//lcd.clear();
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
T.StartTimer();
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// print out the value you read:
if (buttonState == HIGH) {
startT=1 ;
}
if (startT==1){
T.Timer();
Serial.print(T.ShowSeconds());
Serial.print(":");
Serial.println(T.ShowMilliSeconds()-T.ShowSeconds()*1000);
}
if (buttonState1 == HIGH) {
startT=0 ;
}
}
i see your point, i know its much more simple, ive tried "play" with millis before but still had the same result.Even in what you just sent me occurs the same,if you keep button2(where you set it to print) pushed time counts as long as you have it pushed .that's the issue!
even this stopwatch Code - The Trey Clock , while it is a good project occurs the same.while you are pressing the buttons the time wont run(because of debounce?).That's is why i need it to be exact for accuracy reasons.
Hi,
Please post you entire code.
Where in the sketch do you print to the lcd, I see serial printing but no lcd.print.
If you are getting an extra digit on the lcd, make sure you are clearing the digit space on the lcd before you write to it.
lcd.print over writes but if you print 2 digits where there was 3, the 3rd old digit will still be displayed.
was just trying it on lcd.On both(pc and lcd) screens i get the extra digit.On lcd after the 10th seccond and on computer's screen when you dont press the button right away.for example you let your code run for 5 secs and then you press the button, it will you the actuall time but it will add on the millis section a digit showing how much time the code was running,that is what i want to remove
#include<CountUpDownTimer.h>
#include <LiquidCrystal.h>
//LiquidCrystal lcd(9, 8, 7, 6, 5,4);
#define DOWN 0
#define UP 1
boolean startT =0;
const int buttonPin = 2;
const int buttonPin1 = 3;
const int buttonPin2 = 4;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
CountUpDownTimer T(UP);
unsigned long over;
void setup()
{
//lcd.begin(16, 2);
Serial.begin(115200);
//lcd.clear();
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
T.StartTimer();
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// print out the value you read:
if (buttonState == HIGH) {
startT=1 ;
}
if (startT==1){
T.Timer();
Serial.print(T.ShowSeconds());
Serial.print(":");
Serial.println(T.ShowMilliSeconds()-T.ShowSeconds()*1000);
}
if (buttonState1 == HIGH) {
startT=0 ;
}
}
i presented 2 codes, the main issue on this 2 codes is the T.starttimer(),that should be put somewhere else or have some regulations in order my code to perform perfect