float giving back inf value

good afternoon,

i am speed testing a project that tracks an objects speed between to gates, i was doing some testing a low speeds getting 61.73 feet per second. So i decided to up the speed of the object and my display is giving me INF.
I am using to IR sensors to pull the D7 and D8 pins low to start and stop a timer, while using 37.5mm as the distance between them. please send help because i’m running out of ideas what it could be

//#include <splash.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <QTRSensors.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int LED = 13;
float csped = 000;
float msped = 000;
const int IRA = 7;
const int IRB = 8;
int gata = HIGH;
int gatb = HIGH;
int t1 = 0;
int t2 = 0;
float ti = 0;
float dist = .0375;
int isped = 0;
int L = 0;
int k = 0;
void setup() {
// put your setup code here, to run once:
pinMode( IRA, INPUT_PULLUP);
pinMode( IRB, INPUT_PULLUP);
pinMode( LED, OUTPUT);
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) // Address 0x3C for 128x64
{
Serial.println(F(“SSD1306 allocation failed”));
for(;;); // Don’t proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds

// Clear the buffer
display.clearDisplay();
}

void loop() {
// put your main code here, to run repeatedly:
for(; L == 0; L++)
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(45,25);
display.print(csped);
display.setCursor(45,40);
display.print(isped);
display.display();
k=0;
}
gata = digitalRead(IRA);

if( gata == LOW)
{
t1 = millis();

while( k == 0 )
{
digitalWrite(LED, HIGH);
gatb = digitalRead(IRB);
if( gatb == LOW)
{
t2 = millis();
ti = t2-t1;
delay (1);
ti = ti/1000;
msped = dist/ti;//velocity in m/s
csped = msped*3.2808398950131; //velocity in feet per second
//isped = csped + .5;
k= 1;
digitalWrite(LED, LOW);
L = 0;
}
}

}
}

my theory is that the float can’t handle the math on the metres divided by the time which is very quick

Please edit your post to add code tags ("</>" button in the editor).

can’t handle the math on the metres divided by the time

Correct, when the time is zero. Which happens right away in your code.

You make no allowance for the case where the two events are sensed less than 1 mSec apart. When that happens, you are dividing by 0.

Also, your t1 and t2 are ints, while millis() returns unsigned long or uint32_t. That WILL break at some point, giving you a completely bogus result.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.