Reading from SR04 during interrupt

Hello everyone,
I'm trying to read distance with SR04 sensor during interrupt but the distance isn't right.
I tried to check the SR04 in separate function without the interrupt and in this function the SR04 works.
How can i read from SR04 during the interrupt to get the accurate distance?


#include <Servo.h> 
#include<NewPing.h>
Servo myservo;   
#include <AFMotor.h>
#define MAX_SPEED 100
#define SLOW_SPEED 110
#define MAX_DISTANCE 400
#define echoPinLeft 49 //LEFT ULTRASONIC
#define trigPinLeft 44 //LEFT ULTRASONIC
#define echoPinFRONT 47  //FRONT ULTRASONIC
#define trigPinFRONT 46  //FRONT ULTRASONIC
#define echoPinRIGHT 48 //RIGHT ULTRASONIC
#define trigPinRIGHT 45 //RIGHT ULTRASONIC

#define LED_FRONT_GREEN 52
#define LED_RIGHT_RED 53
#define LED_LEFT_YELLOW 51
AF_DCMotor Motor1(1,MOTOR12_64KHZ);
AF_DCMotor Motor2(2,MOTOR12_64KHZ);
AF_DCMotor Motor3(3,MOTOR12_64KHZ);
AF_DCMotor Motor4(4,MOTOR12_64KHZ); 
NewPing sonarLEFT(trigPinLeft, echoPinLeft, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonarFront(trigPinFRONT, echoPinFRONT, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonarRIGHT(trigPinRIGHT, echoPinRIGHT, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int frontReceiver = 15; //Read from the front receiver
int rightReceiver=16;
int leftReceiver=14;
int flag=0;




int distanceArik();
void front_signal();
void drive_slow_blind();
void drive_forward();
void stop_driving();
void drive_fast();
void drive_right();
void drive_left();
void drive_back();
void rotateRight90();
void lookLeftservo();
void lookRightServo();
void turnOnLeftLed();
int ReadDistanceFront();
int ReadDistanceLeft();
int ReadDistanceRight();
void avoid_obstacle();
void turnORightLed();
void turnOnLedFront();
void IR_signal();
void right_signal();
void left_signal();
void setup() 
{

  Serial.begin(9600);
  pinMode(LED_FRONT_GREEN,OUTPUT); //LED FRONT
  pinMode(LED_RIGHT_RED,OUTPUT); //LED RIGHT
  pinMode(LED_LEFT_YELLOW,OUTPUT); //LED LEFT
  pinMode(leftReceiver,INPUT);
  pinMode(frontReceiver, INPUT);
  pinMode(rightReceiver,INPUT);
  pinMode(trigPinLeft,OUTPUT);
  pinMode(echoPinLeft,INPUT);
  pinMode(trigPinFRONT,OUTPUT);
  pinMode(echoPinFRONT,INPUT);
  pinMode(trigPinRIGHT,OUTPUT);
  pinMode(echoPinRIGHT,INPUT);

attachInterrupt(digitalPinToInterrupt(21), front_signal, LOW); //FRONT
attachInterrupt(digitalPinToInterrupt(19), right_signal, LOW);  //LEFT
attachInterrupt(digitalPinToInterrupt(20), left_signal, LOW);  //RIGHT


}
void loop() 
{
 
}

void right_signal()
{
 digitalWrite(LED_FRONT_GREEN,LOW);
 digitalWrite(LED_RIGHT_RED,HIGH);
 digitalWrite(LED_LEFT_YELLOW,LOW);
  drive_right();
    delay(1000);
}

void left_signal()
{
 digitalWrite(LED_FRONT_GREEN,LOW);
 digitalWrite(LED_RIGHT_RED,LOW);
 digitalWrite(LED_LEFT_YELLOW,HIGH);
 drive_left();
  delay(1000);
}



void front_signal()
{
 digitalWrite(LED_FRONT_GREEN,HIGH);
 digitalWrite(LED_RIGHT_RED,LOW);
 digitalWrite(LED_LEFT_YELLOW,LOW);
  drive_forward();
}
void drive_slow_blind()
{
 
 Motor1.setSpeed(70);
 Motor2.setSpeed(70);
 Motor3.setSpeed(70);
 Motor4.setSpeed(70);
  
  Motor1.run(FORWARD);
  Motor2.run(FORWARD);
  Motor3.run(FORWARD);
   Motor4.run(FORWARD);
}



void turnOnLeftLed()
{
     digitalWrite(LED_LEFT_YELLOW,HIGH);
     delay(10);
     digitalWrite(LED_LEFT_YELLOW,LOW);
     delay(10);
}


void turnORightLed()
{
   digitalWrite(LED_RIGHT_RED,HIGH);
     delay(10);
     digitalWrite(LED_RIGHT_RED,LOW);
     delay(10);
}
void turnOnLedFront()
{   
     digitalWrite(LED_FRONT_GREEN,HIGH);
     delay(10);
     digitalWrite(LED_FRONT_GREEN,LOW);
     delay(10);
}

void avoid_obstacle()
{
  int distanceR=ReadDistanceRight();
  int distanceL=ReadDistanceLeft();
  if (distanceR>distanceL)
  {
    drive_right();
  while (ReadDistanceLeft()<70)
    drive_forward();
   drive_left(); 
   drive_forward();
   delay(500);
  }
  else if(distanceL>distanceR)
  {
   drive_left();
    while (ReadDistanceRight()<70)
     drive_forward();
   drive_right();
   drive_forward();
   delay(500);
  }
}

void drive_forward()
{
  drive_fast();
 Motor1.run(FORWARD);
 Motor2.run(FORWARD);
 Motor3.run(FORWARD);
 Motor4.run(FORWARD);

return;
}
void drive_fast()
{
 Motor1.setSpeed(MAX_SPEED);
 Motor2.setSpeed(MAX_SPEED);
 Motor3.setSpeed(MAX_SPEED);
 Motor4.setSpeed(MAX_SPEED);

}

void drive_slow()
{ 
  Motor1.setSpeed(SLOW_SPEED);
  Motor2.setSpeed(SLOW_SPEED);
  Motor3.setSpeed(SLOW_SPEED);
  Motor4.setSpeed(SLOW_SPEED);
}
void stop_driving()
{
  Motor1.setSpeed(0);
  Motor1.run(RELEASE);
  Motor2.run(RELEASE);
   Motor2.setSpeed(0);
  Motor3.run(RELEASE);
   Motor3.setSpeed(0);
  Motor4.run(RELEASE);
  Motor4.setSpeed(0);

}
void drive_left()
{
    drive_slow();
  Motor1.run(BACKWARD);
  Motor2.run(FORWARD);
  Motor3.run(FORWARD);
  Motor4.run(BACKWARD);

}
void rotateRight90()
{
  drive_slow();
  Motor1.run(BACKWARD);
  Motor2.run(FORWARD);
  Motor3.run(FORWARD);
  Motor4.run(BACKWARD);
  delay(500);
  stop_driving();
}
void rotateLeft90()
{
  drive_slow();
  Motor1.run(FORWARD);
  Motor2.run(BACKWARD);
  Motor3.run(BACKWARD);
  Motor4.run(FORWARD);
  delay(500);
  stop_driving();
}

void drive_right()
{
  drive_slow();
  Motor1.run(FORWARD);
  Motor2.run(BACKWARD);
  Motor3.run(BACKWARD);
  Motor4.run(FORWARD);
}
void drive_back()
{
   drive_fast();
  Motor1.run(BACKWARD);
  Motor2.run(BACKWARD);
  Motor3.run(BACKWARD);
  Motor4.run(BACKWARD);
  
}
int ReadDistanceLeft()
{
  delay(30);
  int sum=0;
  int distance=0;
  for (int i=0;i<10;i++)
  {
   distance=sonarLEFT.ping_cm();
  sum=sum+distance;
  }
  distance=sum/10;
  if (distance==0)
  distance=5000;
  return distance;
}
int ReadDistanceFront()
{
  delay(30);
 int distance=0;
 int sum=0;
 for (int i=0;i<10;i++)
 {
  distance=sonarFront.ping_cm();
  sum=sum+distance;
  delay(20);
 }
 distance=sum/10;
 if (distance==0)
  distance=5000;
  return distance;
}
int ReadDistanceRight()
{
  delay(30);
 int distance=0;
 int sum=0;
 for (int i=0;i<10;i++)
 {
  distance=sonarRIGHT.ping_cm();
  sum=sum+distance;
 }
 distance=sum/10;
  if (distance==0)
  distance=5000;
  return distance;
}
Thank you very much!

Reading the SR04 almost certainly involves using interrupts but interrupts are disables whilst in an ISR

Can you see the problem ?

Why do you need to read the SR04 in an ISR ?

Why?

Interrupts are meant to be fast - something the speed of sound is not.
Much less the one second delay :roll_eyes:

The Echo is a
distance object that is pulse width and the range in proportion .You can
calculate the range through the time interval between sending trigger signal and
receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the
range = high level time * velocity (340M/S) / 2; we suggest to use over 60ms
measurement cycle, in order to prevent trigger signal to the echo signal

Your code must COMPUTE the distance based on time. There is NOTHING to read.

attachInterrupt(digitalPinToInterrupt(21), front_signal, LOW); //FRONT
attachInterrupt(digitalPinToInterrupt(19), right_signal, LOW);  //LEFT
attachInterrupt(digitalPinToInterrupt(20), left_signal, LOW);  //RIGHT

What is connected to the interrupt pins?

What are you trying to achieve with calling these functions from the interrupt? delay() will not increment within an isr.

void right_signal()
{
 digitalWrite(LED_FRONT_GREEN,LOW);
 digitalWrite(LED_RIGHT_RED,HIGH);
 digitalWrite(LED_LEFT_YELLOW,LOW);
  drive_right();
    delay(1000);
}

void left_signal()
{
 digitalWrite(LED_FRONT_GREEN,LOW);
 digitalWrite(LED_RIGHT_RED,LOW);
 digitalWrite(LED_LEFT_YELLOW,HIGH);
 drive_left();
  delay(1000);
}



void front_signal()
{
 digitalWrite(LED_FRONT_GREEN,HIGH);
 digitalWrite(LED_RIGHT_RED,LOW);
 digitalWrite(LED_LEFT_YELLOW,LOW);
  drive_forward();
}

I will clarify my question.
I have a function called "Front signal"
If I receive signal from Front IR I want to jump to the function "front_signal"
At this function I want to make a decision if I have an obstacle in front of me.
if the distance from the obstacle is under 70 and i have signal it means that i reached to the destination.
But at this function i have problems with reading from the SR04 sensor.
How can i make a reading during the "front_signal" function?

I have a goal to reach with the car to the destination using IR sensors.
If I receive from the front IR I want to drive forward so I automatically jump to the front_signal function and want to do it at this function.
At the same way I'm doing with the right and left signals.
If I have signal I want to drive to this side but with condition if i have an obstacle i must to stop.

Then this function must do the initial send trigger, save the time, and wait and test a flag that the interrupt will set, indicating a a response echo has been received. Then not the new time, and compute the time difference and compute the distance. That will give you the distance to the object.

How can I do the initial send trigger?

With a digitalWrite

Follow the documentation!

Yet you never call void IR_signal() or appear to read the IR signal anywhere. Perhaps you could break the code into smaller pieces and develop the IR sensor code and then add the NewPing library SR04 readings once you have the car responding to the IR inputs.

I tried to use New.ping library during the interupt because if i receive a signal i firstly want to check if i have obstacle or not.
I understand the problem but dont know how to fix it. The reading distance function uses delay during the function and delay isn't working during interupt.
Do you know maybe what can i do instead?
Thank you very much

There is no shame in starting a project over from the beginning. You know a whole lot more now than in the beginning. I suggest you begin from the beginning and work out the logic problems.

I did a lot of changes ,Im working on this project a lot of time without progression.
In the current situation it drives and reach the destination but i am not able to mesure distance.
The problem is that reading function uses delay which are not provided at arduino.
How can i use delay in another way? Only to read the distance .. very short time

@Studing_engineering
I'm not convinced your project has anything to do with Covid, so I've moved it to a more suitable place. In the unlikely event that it is about Covid please ask me to move it back.

Thank you.

I don't understand. What has delay to do with what you are doing? Again, YOU DO NOT READ THE DISTANCE, YOU COMPUTE THE DISTANCE!
As I posted before after you trigger the sound pulse, you begin testing a boolean that the echo interrupt will set. When that boolean becomes true, you compute the time difference between the pulse trigger and the current milis() and use that time to compute the distance. No delay involved, anywhere.

Thank you for your help.
I am very appriciate it.

Can you provide some simplified version of the code which compiles and runs and shows what you are trying to do with the IR sensor interrupt and the ultrasonic sensor SR04 data.

It may make no difference if you use the new ping library or write your own code for reading the echo from SR04, but i am really confused about the structure of your code and where the IR data is actually coming from.

You can't. What you can do is put the Echo signal on an interrupt pin set for CHANGE interrupts. When the pin changes to HIGH, not the time in microseconds. Later, when the pin changes to LOW, subtract the recorded time from the current micros() to get the echo time. From that you can calculate the distance.

Pulse each of the Trigger pins at intervals of at least 30 milliseconds. 50 milliseconds might be better. Now you will have a continuously updated distance value that you don't have to wait for in an ISR.