Please Help

So I am working on a project that requires 3 things.

  1. Ultrasonic sensor to detect range
  2. Infrared Sensor to detect RPM
  3. LED lights up when a formula using RPM is more than the range detected.

The problem.
Without the code that i have included for the ultrasonic sensor, the RPM works fine. But when the code for the ultrasonic sensor is included, the RPM sensor will gives false reading

An example would be

Without Ultrasonic Sensor Code (Works Fine)

17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00
17:15:01.724 → RPM: 1230.00

With Ultrasonic Sensor Code ( Gives false reading when RPM is supposed to be around 1230)

17:12:13.884 → RPM: 1186.00
17:12:13.884 → RPM: 1186.00
17:12:13.884 → RPM: 411.00
17:12:13.884 → RPM: 411.00
17:12:13.930 → RPM: 411.00
17:12:13.930 → RPM: 411.00
17:12:13.930 → RPM: 411.00
17:12:13.930 → RPM: 411.00
17:12:13.930 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:13.977 → RPM: 1216.00
17:12:14.024 → RPM: 1216.00
17:12:14.024 → RPM: 1216.00
17:12:14.024 → RPM: 1216.00
17:12:14.024 → RPM: 1216.00
17:12:14.024 → RPM: 638.00
17:12:14.071 → RPM: 638.00
17:12:14.071 → RPM: 638.00

This is the code that i used

const int dataIN = 8; //IR sensor INPUT
const int trigPin = 9; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 10; // Echo Pin of Ultrasonic Sensor

unsigned long prevmillis; // To store time
unsigned long durations; // To store time difference
unsigned long refresh; // To store time for refresh of reading

long duration;
int distance;

float rpm; // RPM value
float velocity;
float D;
float v;
float BD;

boolean currentstate; // Current state of IR input scan
boolean prevstate; // State of IR sensor in previous scan

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

void setup()
{
Serial.begin(115200); // Starting Serial Terminal
pinMode(dataIN,INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
prevmillis = 0;
prevstate = LOW;
}

void loop()
{
{
// Ultrasonic Sensor to detect distance
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
// Serial.print("Distance: ");
// Serial.println(distance);

}

{
// RPM Measurement
currentstate = digitalRead(dataIN); // Read IR sensor state
velocity = 10rpm23.15/60; // converting rpm to velocity with radius of 10cm
v = velocity/100; //converting cm/s to m/s
D = ((v
v)/(23.29))+(v0.515); // Distance required for braking
BD = D100; //converting back to cm
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
durations = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpm = (60000000/durations); // rpm = (1/ time millis)10001000
60;
prevmillis = micros(); // store time for nect revolution calculation
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
}

// LCD Display
if( ( millis()-refresh ) >= 100 )
{
Serial.print ("RPM: ");
Serial.println (rpm);
/Serial.print ("Braking Distance: ");
Serial.print (BD);
Serial.println (“cm”);
Serial.print ("Velocity: ");
Serial.print (velocity);
Serial.println (“cm/s”);
/

}
/* if(D>=cm)
{
digitalWrite(LED_BUILTIN, HIGH);
}
if(D<cm)
{
digitalWrite(LED_BUILTIN,LOW);
}*/

}

The pulseIn() function is a blocking function so the program is stuck there while waiting for a the echo pulse to finish.

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar, then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code.

You could use interrupts to capture the pulses from the IR sensor while pulseIn() is blocking:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/