Alarm system using ultrasonic sensor (sensor not giving right measurement)

I am working on an alarm system using ultrasonic sensor.
At the beginning I had a simple circuit where if the sensor detects any obstacles closer than 5 cm the LED and the buzzer will go on if not they will remain off as a second step I added a keypad where if 1 is pressed the sensor will start detecting any obstacles and if any is closer than 5 cm the LED and the buzzer will go on if not they will remain off but with this addition the sensor wasn't as accurate any-more and when I tried to add a tone and a delay before the sensor starts detecting anything and after the button 1 is pressed the sensor is completely not accurate and not giving the right measurement thus the whole project isn't functioning as supposed to and while I am running the program the following error is appearing:

Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:258)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

I attached my code
Any help is very much appreciated :slight_smile: :slight_smile: :slight_smile: :slight_smile:

alarm_system_code.txt (1.67 KB)

There is no reason to use an attachment when the code is so short.

#include <Keypad.h>
#define ECHOPIN 11
#define TRIGPIN 12
int LED = 13;
int buzzer = 10;
int val = 0;
int ledPin_state;


const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] = {
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3};    //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
 Serial.begin(9600);
     pinMode(ECHOPIN, INPUT);
     pinMode(TRIGPIN, OUTPUT);
     pinMode(LED, OUTPUT);           // Sets the digital pin as output.
     pinMode(buzzer, OUTPUT);

}
 
void loop(){
 char key = keypad.getKey();
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
 
if (key){
   Serial.println(key);

if (key == '1') 

//tone (buzzer, 300);
//delay(1000);
//tone (buzzer, 800);
//delay(1000);
//tone (buzzer, 300);
//delay(1000);
//tone (buzzer, 800);
//delay(1000);
//tone (buzzer, 300);
//delay(1000);
  
 {
  float distance = pulseIn(ECHOPIN, HIGH);
  tone (buzzer, 300);
  delay(1000);
  tone (buzzer, 800);
  delay(1000);
  tone (buzzer, 300);
  delay(1000);
  tone (buzzer, 800);  
  delay(1000);
  tone (buzzer, 300);
  delay(1000);
  distance= (distance/2)/29.1;
  Serial.print(distance);
  Serial.println("  cm");
  delay(200);
    
  if (distance >= 5)
  {  
    digitalWrite(LED, LOW);
    tone(buzzer, 0);
  }
  else {
    digitalWrite(LED, HIGH);
    tone(buzzer, 700);
}}}}

There is no excuse for posting crap that looks like this, either.

EVERY } goes on its own line!

Commented out code is NOT part of the problem. GET RID OF IT!

if (key == '1') 
 {
  float distance = pulseIn(ECHOPIN, HIGH);

You seem to have forgotten to send a pulse. No sense measuring how long it takes to get a response to something you haven't sent.