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