Go Down

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

Chaso

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.

dave-in-nj

http://arduino.cc/en/Reference/millis

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

Chaso

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.

robtillaart

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)

Chaso

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.

dlloyd

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.

Chaso

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:
ww.arduino.cc/en/Tutorial/Button
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.

Grumpy_Mike

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.

Chaso

Thanks Mike, your suggestion works perfectly. Chaso

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.


Grumpy_Mike

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.

Chaso

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()
{
  pinMode(backLight,OUTPUT);
  digitalWrite(backLight,HIGH);
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.
lcd.print("start.");
while (digitalRead(Button)==LOW) // The test does not start until the button is pushed (and held down).
{

delay(1400);

}
{



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.
{delay(1);}
lcd. clear();
lcd.display();
RTME=millis()-TME; // Time since Led is Red, hence reaction time.
lcd.print("Reaction Time:");
lcd.setCursor(0,1);
lcd.print(RTME);
digitalWrite(LEDR, LOW);
delay(2000);
digitalWrite (LEDR,HIGH);
   delay(2000);
    digitalWrite (LEDR,LOW);
   delay(2000);
  digitalWrite (LEDR,HIGH);
  delay(2000);
digitalWrite (LEDR,LOW);






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


int Time=0;
delay(1000);
}
}

Grumpy_Mike

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).
   {
   delay(1400);
  }

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