This is my complete code
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance,a,b;
int ircount = 0;
int level = 0;
void ultrasonic();
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT_PULLUP); // Sets the echoPin as an Input
Serial.begin(9600); //Start serial communication boud rate at 9600
}
void loop() {
while(1) {
level++;
if(level==25){
ultrasonic();
}
if(digitalRead(2)==HIGH){
ircount=0;}
delay(50);
if(digitalRead(2)==LOW) {
// If no signal print collision detected
ircount++;
if(ircount==1) {
}
}
}
}
void ultrasonic()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Sets the trigPin on HIGH state for 50 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(50);
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
a=distance;
if( (a >= 5) && (a<= 255) ) {
Serial.println(a);
}
level = 0;
}
hope you will give me an edit