trouble using millis() for timing less than 0.5sec

I am using millis() to calculate an elapsed time. I am assigning millis() to variables and then subtracting them to get the difference. The variables are assigned as floats so I can get decimal numbers. The timer works great when timing something over ~0.50 seconds. When timing something less than that, the timer continues to run to ~0.50 seconds and then stops. For my application, i need it to be able to calculate runs less than 0.5 seconds. Anyone know why this is happening or how to fix it?

Thanks!

Not a lot of detail here. You need to post code.

The variables are assigned as floats so I can get decimal numbers.

Subtracting one int from another will not give a fractional result.

I second AWOL's comments.

Subtracting is good, but the conversion to float is "scary"

Post your code than we can give better advice what went wrong , can't see your code from here ;)

I use this kind of code for timing

unsigned long before = millis();
 // do something
unsigned long elapsed = millis() - before;
Serial.print(elapsed);

// to floatize the data

float f = elapsed / 1000.0;
Serial.println(f, 3);

And you can use my stopwatch class - http://www.arduino.cc/playground/Code/StopWatchClass -

Try defining your millis related variables as unsigned long instead. Always do your math as (later time) - (earlier time) to get a whole number of milliseconds back. Use microseconds if need it more precise - also defined as unsigned long.

Here is my code. I changed the variables to unsigned long and I am still experiencing the same problem. I began outputting the time variables using serial.print to attempt to troubleshoot and noticed that the loop runs 6 iterations before stopping at .40-.50 seconds.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// PIN NUMBERS:
const int buttonPin = 6; // Start Button
const int buttonRPin = 8; // Reset Button
const int ledPin = 9; // LED
const int loadPin = A0; // load cell input

// VARIABLES:
int buttonState = 0; // Start Button
int buttonRState = 0; // Reset Button
int loadState = 0; // load cell reading
float peak = 0; // max force
unsigned long time = 0; // time when load cell = 0
unsigned long timer = 0; // random start time
unsigned long startTime = 0; // time when buzzer sounds
float boardTime = 0; // time swimmer is on board
float force = 0; // current force on loadcell
float preLoad = 0;
float pounds = 0;

void setup() {
pinMode(10, OUTPUT); // set a pin for buzzer output
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize start button
pinMode(buttonRPin, INPUT); // initialize reset button

lcd.begin(16, 2); // set up the LCD’s number of columns and rows
Serial.begin(9600); // serial print

preLoad = analogRead(loadState);

}

// LOOP
void loop() {

force = analogRead(loadState);
buttonState = digitalRead(buttonPin);
buttonRState = digitalRead(buttonRPin);

if (buttonState == LOW && peak == 0) {
lcd.setCursor(0, 0);
lcd.print(" Press Start “);
lcd.setCursor(0, 1);
lcd.print(” when Ready. ");

}

if (buttonState == HIGH) {
lcd.noDisplay();
timer = random(2000, 7000); // random start time between 10 to 20 seconds
digitalWrite(ledPin, HIGH); // turn LED on
delay(timer); // delay start random number of seconds

tone(13, 2800, 400); // buzz on pin # at 2800Hz for 500 milliseconds

startTime = millis();
digitalWrite(ledPin, LOW); // turn LED off
}

if (force > preLoad+1) {
peak = max(peak, (force-preLoad));
time = millis();

pounds = (peak); // Subract preload here
boardTime = (time - startTime);

lcd.display();
lcd.setCursor(0, 0);
lcd.print("Time= “);
lcd.print(boardTime/1000);
lcd.print(” secs ");

lcd.setCursor(0, 1);
lcd.print("Max = “);
lcd.print(pounds);
lcd.print(” cnts “);
}
Serial.print(”\n\ntime = “);
Serial.print(time/1000);
Serial.print(” startTime = “);
Serial.print(startTime/1000);
Serial.print(” Output = “);
Serial.print(boardTime/1000);
Serial.print(” Preload = “);
Serial.print(preLoad);
Serial.print(” Force = ");
Serial.print(force);

}
}

  1. Please use the code tags when posting code (or press the '#' in the editor.)
  2. Integer contants will result in integer math.
  lcd.print(boardTime/1000);

Is not the same as:

  lcd.print(boardTime/1000.0);

Does that code compile? It looks like you have one too many closing braces.

Thanks for the help, James. That worked.

  pounds = (peak);        // Subract preload here

Why was it necessary to put peak in parentheses? What does the comment have to do with the code?