Go Down

Topic: TME=millis problem (Read 5573 times) previous topic - next topic


Hi, I am trying to incorporate a "Timed record" between two simple functions, ie, LED HIGH and PUSHBUTTON. I believe " TME = millis is what I should be incorporating in the code. I have searched Arduino Tutorials and Playground to see how " TME= millis " works, but have had no luck. Could someone point me in the right direction to learn about this function. Needles to say "Timed record" has to transfer to LCD screen. Thanking you in anticipation, CHASO.



are you trying to display the elapsed time from the LED HI to the push of the button?

long ledmillis = 0;
long buttonmillis = 0;
long elapsedmillis = 0;

load the current millis into your variable ledmillis when the LED goes HI
ledmillis = millis

load the current millis into yoru variable buttonmillis when the button is pressed
buttonmills = millis

subtrack ledmills from buttonmillis to get the elapsed time.
elapsedmills = buttonmills - ledmillis


Thanks Dave for your response. Sorry I have been so long in replying, I have been busy trying to sort out my code for this project.
Once sorted I hope to be able to include your details into my code. Once again, Thanks CHASO.


better use
unsigned long ledmillis = 0;
unsigned long buttonmillis = 0;
unsigned long elapsedmillis = 0;

as the millis() returns  unsigned long.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Hi Guys, I have overcome my "timing" problem thanks to you Guys. However I now notice I have a fundamental problem, not enough  digital pins on my IDE. The LCD display uses 7 of the 13 pins leaving 6 spare. My project includes 5 LEDs and 5 Pushbuttons, meaning I am 4 shot.  Can I use the Analogue pins to rectify the shortfall ? Any help would be welcome. CHASO.


You could detect 5 push buttons with one analog input with something like this, if you add another pushbutton (S0) at the bottom of the diagram that switches the signal to ground.


Thanks  dlloyd  for your reply, but it does require  a lot extra hardware and complexity. I have come across what appears to be a simpler version from member "Jeff McD",  ie, 

Re: Analog input as digital input for button

« Reply #4 on: January 19, 2011, 04:41:53 am »
Bigger Bigger Smaller Smaller Reset Reset 

Quick question:
Is the below correct if i want to read a push button on analog pin (0)?
its a rough version i know, but i just want to check the important parts...

pinmode(14,input);                       // set the pin to input
int BUTTON = digitalread(14);        // read analog pin 0 as digital and set it to a variable

if (BUTTON == HIGH)                   // it shows pressed
else if (BUTTON == LOW)             // it shows not pressed

hardware layout:
5v___button____10k ohm resistor___ground
                    |___analog pin 0

layout done as shown in:
Thanks smiley

I have tried this simplified version and it works, But, only on  3 of the 6 pins, (0,2,and 3) I suspect I may have a hardware problem. any other reason ?  Thanks Chaso.


Yes you can use the analogue pins as digital pins just like this.
Although use A0 in place of 14 for the pin number as that gets translated to the right pin for all models of arduino, the use of 14 only works with some models.


Thanks Mike, your suggestion works perfectly. Chaso


Hi Guys, I have to admit "defeat",  I have spent hours and hours trying to sort out " Unsigned long ledmillis=0; "    "Unsigned long buttonmillis=0;    " Unsigned long elapsedmillis=0;   At an early stage I had it worked out fine, then I made the fatal mistake of fine tuning what I had, without making a backup copy. As you can guess I lost what success I had and have not been able to resurrect it. Could someone explain how to incorporate the above "millis" into the code.
One minor problem, while logged into "playground", in the search box there seems to be no way to enact the search, just an x. I would like to do more elementary home work in playground ,but  search box is not helping. Thanks Chaso.


Read the how to use the forum sticky post about using code tags on code.
Then post what you have and we can see if we can spot your mistake.


Thanks Mike, Enclosed is my code to date.
As you will see the "elapsed time " is recorded only for while the button is depressed. I am wanting to release the button , continue with the programme, then activate the same button or another button to record the total "elapsed time".
Have sorted the playground Search Box access, I just needed to press "enter" key . 
Code: [Select]

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);

int backLight = 13;

int LEDR = A1; // Red Led pin 6
int LEDB = A0; // Blue Led pin 7
// Green Led pin 6
int Button=1;

int PSE; // variable pause
int TME; // Time
int RTME=0; // Reaction Time

void setup()
lcd.begin(16, 2);
pinMode(LEDR, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(Button, INPUT);


void loop()
lcd.clear(); // Clear screen.
lcd.print("Hold Button to");
lcd.setCursor(0,1); // move to second line.
while (digitalRead(Button)==LOW) // The test does not start until the button is pushed (and held down).



TME=millis(); // Time since this program has started (so Blue or Green loop included).
digitalWrite(LEDR, HIGH);

while (digitalRead(Button)==HIGH)// This loop runs untill you have released the button, hence the reaction time.
lcd. clear();
RTME=millis()-TME; // Time since Led is Red, hence reaction time.
lcd.print("Reaction Time:");
digitalWrite(LEDR, LOW);
digitalWrite (LEDR,HIGH);
    digitalWrite (LEDR,LOW);
  digitalWrite (LEDR,HIGH);
digitalWrite (LEDR,LOW);

while (digitalRead(Button)==LOW) // The test does not re-start until the button is pushed once.

int Time=0;


Thanks for posting. There are several things wrong with the code, correct these and try again.

The millis() function returns an unsigned long type variable, so all variables involved in calculations must be of the same type. You have TME declared as in int as well as RTME and PSE. By the way, convention says that you reserve names with all capitals for #defined constants. It will still work as you have it but it does not look nice to some one who knows C.

Next this code:-
Code: [Select]
while (digitalRead(Button)==LOW) // The test does not start until the button is pushed (and held down).

Will not do what the comment says. Suppose you press the button as the delay is just about to expire, then you come out of the while loop immediately. The comment here should say // The test does not start until the button is pushed and held down for between zero and 1.4 seconds. Is that what you want?

Go Up