Pages: [1]   Go Down
Author Topic: millis(); not updating with the same loop  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im trying to determine how much time has elapsed for a few lines of code:
Code:
//Read Flow Meter
  countA=0;
  cTime=millis();
  for(int xCount=0; xCount<10001; xCount++){
    int signalA = digitalRead(rpm_rd3); //detects tachometer signal, flow pin goes high every time the magnet spins past the sensor. NOTE: must be int.
    if(signalA==1){
      countA++;
    }
  }
  pTime=millis();
  FLOWCALC = countA/((pTime-cTime)/1000)*60;

but cTime and pTime are returning the same time.

this is a portion of code. i know the values are the same as i have them reading out to a 20x4 LCD.

i know i could use interrupts for this but i am using a Arduino UNO IDE 1.0.3 and pins 2 and 3 are being used for capturing RPMs from a pump and a fan using interrupts 0 and 1.

original code idea from http://davebmiller.wordpress.com/2011/01/18/arduino-flowmeter/
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's quite possible that your 10000 cycle loop takes less than a millisecond to execute
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16459
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of millis() try using micros() as a 16mhz AVR chip may run code a lot faster then you appreciate.  smiley-wink

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

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks i'll give that a try.

also wondering if serial needs to be running as the examples i have seen have it running at 9600 or 19200.
not sure if that makes a difference.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean by "serial is running" ? It's not like Serial.begin() starts some kind of background process smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  FLOWCALC = countA/((pTime-cTime)/1000)*60;
Notice that this is ALL integer arithmetic. Unless the loop takes more than a second to execute (highly unlikely), (pTime - cTime)/1000 will always be 0. (pTime - cTime)/1000.0 would not.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 167
Posts: 12417
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

digitalread takes in the order of 1-4 uSec  the loop should be above 10 millisecs at least. using micros() is OK but won't solve the error I expect..

Note you are doing 10001 iterations, and divide by 1000
for(int xCount=0; xCount<10001; xCount++){

FLOWCALC = countA/((pTime-cTime)/1000)*60;

I don't know the datatypes of the vars used but if ptime and ctime are int/long and ãpprox 40 apart, dividing by 1000 will return zero * 60 is still zero.
Integer math is not forgiving.

Try this formula instead (make ptime and ctime and flowcalc unsigned long )

FLOWCALC = (countA * 60000L)/(pTime-cTime);

Style remark:
It is quite common in C/C++ to write constants/#defines only in upper case and variables not. So FLOWCALC is misleading because of its style.



Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: