HC-SR04

Hi, can you help me. I have a code

#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5

long duration, distance;

void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
unsigned char m;
if ( Serial.available() ) {
m = Serial.read();
if ( m == '1' ) {
SonarSensor(trigPin1, echoPin1);
if ( distance < 30 ) {
Serial.print("1"); // parking slot filled
}
else {
Serial.print("0"); // parking slot empty
}
}
else if ( m == '2' ) {
SonarSensor(trigPin2, echoPin2);
if ( distance < 30 ) {
Serial.print("1");
}
else {
Serial.print("0");
}
}
}
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
delay(500);
}

but when I upload it in serial monitor blank :frowning: :frowning: :frowning: . Is there something wrong with A code?

Is there something wrong with A code?

You have not told us what YOU are sending from the Serial Monitor app TO the Arduino, so it may be perfectly reasonable that the Arduino sends nothing to the Serial Monitor app.

I wired up 2 HCSR04 sensors to my Uno, compiled and uploaded your code. If I type '1' or '2' in the serial monitor and send, I get '0' or '1' displayed in serial monitor depending on the distance that the sensor returns. Any other input from serial monitor prints nothing because there is nothing in the code to check for invalid input and print an error message. The code is working as far as I can see. I think that the most likely problem is a mismatch of baud rate between the sketch (9600) and serial monitor (?).