Why does the speed float give ovf?

i have this code:

#include <LiquidCrystal.h>
const int rs=12, e=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);
unsigned long timerstart, time, timesensor, delayms;
float speed;
bool sensor = false;
bool first = true;


void setup() {
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(7,INPUT);
  pinMode(6,OUTPUT);
  pinMode(8, INPUT);
  pinMode(9,OUTPUT);
}

void loop() {
  speed = 0;
  long sure;
  if(first=true){
    digitalWrite(6,HIGH);
    delayMicroseconds(10);
    digitalWrite(6,LOW);
    delayMicroseconds(2);
      sure = pulseIn(7,HIGH);
      timesensor=millis();
  }
  if(millis() - timesensor >= delayms){
    digitalWrite(6,HIGH);
    delayMicroseconds(3);
    digitalWrite(6,LOW);
    delayMicroseconds(2);
      sure = pulseIn(7,HIGH);
      timesensor=micros();
  }
    
  
    digitalWrite(9,HIGH);
  delayMicroseconds(3);
  digitalWrite(9,LOW);
  delayMicroseconds(2);
    long sure2 = pulseIn(8,HIGH);
    
    
  // mesafe formülü : mesafe = (sure/2)/29.1
 if(sure < 1900){
    sensor=true;
    timerstart = micros();
  }
  
    if(sure2 < 1900&&sensor==true)
    {
      time = micros()-timerstart;
      sensor=false;
      speed = round(1/(time/1000000));
    }
   lcd.clear();
   lcd.setCursor(0,0);
   if(speed>0)
   {
     lcd.print(speed);
    Serial.print(speed);
    Serial.print(" ");
    Serial.print(time);
    Serial.print(" ");
     first=false;
     delayms = (1/speed)*1000000;
   }
   
}

the main problem(at least i think) is here:

 if(sure2 < 1900&&sensor==true)
    {
      time = micros()-timerstart;
      sensor=false;
      speed = round(1/(time/1000000));
    }

the speed float (very usually) gives ovf if i put 1 and it (very usually) gives inf when i put 0.07. These values are cm and are supposed to record speed.(maybe theres a better way but i didnt think of it)The time int is usually between 20000 and 50000 in testing environment. if i do the math with 0.07 and 20000 it should give 3,5 but it gives inf. if i do the math with 1 and 20000 it should give 50 but gives ovf.I put round there in hopes of maybe getting actual numbers but didnt work. tried double (for speed) didnt work. tried int (for speed) didnt work. why does it not work? i would maybe assume because of a lot of decimals but at this point im out of ideas. plz send help

Try 1000000/time

Integer division by zero, if time < 1000000.

I think you mean:
if (first == true) {
which, since 'first' is already a bool, is the same as:
if (first) {

that is a problem thanks for figuring out but it doesnt hurt my code or at least doesnt make it not work

im using time/1000000 to get seconds from microseconds(time is microseconds if you look at the code) so that would make the equation wrong

that could be the problem but how can i fix it if it its an int in the operation. also it gives out like 0.01 if the time goes very high in comparison to other tests for example 500000 so i dont think that is the problem

Have you tried as suggested in Post #17? A little grade school mathematics says it works.

If you want rounding instead of truncation, go with:

speed = round(1000000.0/time);
  if (millis() - timesensor >= delayms)
  {
    timesensor = micros();  ///// ???
  }

You are mixing millis() and micros() here.

if i hypothetically use the value 1 in the speed equation i can just divide 1000000 to time so that fixes my problem. still thanks to anyone replying

yes i am very so smart and figured it out myself later but thanks

I doubt that. I was taught that 1/(x/y) == y/x

This may solve the problem , but it is not always true...

x/y!=1/(y/x) if x==0 or y==0

Just to keep everyone sharp ..

That's what caused the original divide by zero problem, so it wasn't overlooked...

Sorry, this statement is nonsense, because division by zero is not defined.

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