how to add two functions in one code

hi i am trying to add the sonar sensor code and knock sensor code together,both works well individually but the knock sensor doesnt work the way it works individually when both are put together.please correct my code.

Sonar sensor code

 {
    digitalWrite(trigpin,LOW);
    delayMicroseconds(2);
    
    digitalWrite(trigpin,HIGH);
    delayMicroseconds(2);
      digitalWrite(trigpin,LOW);
    delayMicroseconds(2);
  
    
    duration=pulseIn (echopin,HIGH);
    Serial.println(distance);
    distance= duration/58.2;
    delay (50);
}

Knock sensor code
{

 sensorReading = analogRead(knockSensor);
 
  if (sensorReading >= threshold && oneTime == 0) 
  {
    oneTime = 1;
    
    knock = 1;
    delay(100);
    Serial.println("Knock!");
  }

  if (sensorReading < threshold)
  {
     oneTime = 0;
     knock = 0;
}
  }

both together this is probably wrong,correct me

void loop ()
{
 {
    digitalWrite(trigpin,LOW);
    delayMicroseconds(2);
    
    digitalWrite(trigpin,HIGH);
    delayMicroseconds(2);
      digitalWrite(trigpin,LOW);
    delayMicroseconds(2);
  
    
    duration=pulseIn (echopin,HIGH);
    Serial.println(distance);
    distance= duration/58.2;
    delay (50);
}
  
  {
    sensorReading = analogRead(knockSensor);
    if (sensorReading >= threshold && oneTime == 0) 
  {
    oneTime = 1;
    knock = 1;
    delay(100);
    Serial.println("Knock!"); 
  }
    if (sensorReading < threshold)
  {
     oneTime = 0;
     knock = 0;
  }
  
}

Please post your whole sketch, with both operations in it. Then it can be copied/pasted to the IDE and verified.

Try this:-

#define echopin  8 // echo pin
#define trigpin 9 // Trigger pin
int maximumRange = 30;
long duration, distance;
const int ledPin = 13 ;
const int knockSensor = A0;
const int threshold = 5;
int sensorReading = 0;
int knock = 0;
byte oneTime = 0;

void setup()
{
    Serial.begin (9600);
    pinMode(ledPin, OUTPUT);
    pinMode (trigpin, OUTPUT);
    pinMode (echopin, INPUT );
    pinMode (6, OUTPUT);
    pinMode (5, OUTPUT);
    pinMode (11, OUTPUT);
    pinMode (10, OUTPUT);
}

void loop ()
{
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(2);
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    duration = pulseIn (echopin, HIGH);
    Serial.println(distance);
    distance = duration / 58.2;
    delay (50);

    sensorReading = analogRead(knockSensor);
    if (sensorReading >= threshold && oneTime == 0)
    {
        oneTime = 1;
        knock = 1;
        delay(100);
        Serial.println("Knock!");
    }
    if (sensorReading < threshold)
    {
        oneTime = 0;
        knock = 0;
    }
}

Those delays probably need to go. You need to look into using millis() -based timing. You can miss events using ‘delay()’. With the 150mS of delays, your code can only detect engine knocks 400 times per minute.

Still, the above compiles fine.

On another note, you should never PM people with your code, and especially not just because there’s a 10 minute minimum between posts and you’re in a hurry.

hi when i put only the Knock sensor code i put if lf knock ==1 for a LED to get on and delay for 1 second it works perfectly for each tap, but when i add the sonar code it stops working nothing happens it blinks only for some taps

This was nowhere in the code you posted:-

lf knock ==1

And it couldn't work anyway as you typed it.

OldSteve: Please post your whole sketch,

+1

COMPLETE CODE MISSED IT SORRY

#define echopin  8 // echo pin
#define trigpin 9 // Trigger pin
int maximumRange = 30;
long duration, distance;
const int ledPin = 13 ;
const int knockSensor = A0;
const int threshold = 10;
int sensorReading = 0;
int knock = 0;
byte oneTime=0;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}



void loop ()
{
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpin, HIGH);
    delayMicroseconds(2);
    digitalWrite(trigpin, LOW);
    delayMicroseconds(2);
    duration = pulseIn (echopin, HIGH);
    Serial.println(distance);
    distance = duration / 58.2;
    delay (50);

    sensorReading = analogRead(knockSensor);
    if (sensorReading >= threshold && oneTime == 0)
    {
        oneTime = 1;
        knock = 1;
        delay(100);
        Serial.println("Knock!");
    }
    if (sensorReading < threshold)
    {
        oneTime = 0;
        knock = 0;
    }

   
 if (knock == 1)
    {
    digitalWrite(13, HIGH);
    delay(100);
    }
    else if (knock == 0)
    {
      digitalWrite(13, LOW);
    }}