Help restarting code when sonar has sensed something

Hi,
Ive spent hours trying to figure this out and I give up. Any help would be more than greatly appreciated.

I have this sequence for a relay. It opens and closes for a sequence it needs to run. I have a sonar sensor and I need it to restart the sequence when an object is detected less than 70 cm away. As a begginer, ive spent at least 8 hours trying to figure this out but I give up. Your solution is my last hope!

Ive labeled where exactly im having trouble. Thank you so much for your help.

//Begin
#define echoPin 10
#define trigPin 12
long duration;
long distance;


void(*resetFunc)(void) = 0;

void setup() {

  Serial.begin(9600);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(3, OUTPUT);
  Serial.print("Restart");

  // Tap Sequence
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
}
void loop()

{


  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration/58.2;

  Serial.print(distance);
  delay(50);

 [b]//this is the code I need to inturupt and start again if the sonar senses something
 [/b]

//start

  {
    if (distance < 70) resetFunc();
    delay(1000);

    digitalWrite(3, LOW);
    delay(4400);

    digitalWrite(3, HIGH);
    delay(3500);

    delay(1000);
    digitalWrite(3, LOW);
    delay(2800);
    
    delay(1000);
    digitalWrite(3, HIGH);
    delay(2000);

   
    delay(1000);
    digitalWrite(3, LOW);
    while (1) {};

//end 

  }

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration / 58.2;

  Serial.print(distance);
  delay(50);
}

My latest attempt was putting
if (distance < 70) resetFunc();
in between every change of LOW or HIGH of the relay but the value read from the sensor freezes and doesnt update after the relay code has started.

I have also tried this with no luck. The values spit back freeze after the relay sequence has started

Screenshot_1.png

You are not are not going to be able to simply stop executing this code because of the delay() and while() statements.

[b]//this is the code I need to inturupt and start again if the sonar senses something
 [/b]

//start

  {
    if (distance < 70) resetFunc();
    delay(1000);

    digitalWrite(3, LOW);
    delay(4400);

    digitalWrite(3, HIGH);
    delay(3500);

    delay(1000);
    digitalWrite(3, LOW);
    delay(2800);
   
    delay(1000);
    digitalWrite(3, HIGH);
    delay(2000);

   
    delay(1000);
    digitalWrite(3, LOW);
    while (1) {};

//end

you have to restructure it to use millis() for the timing element and also test a flag such as distanceLessThan70 to abort the sequence as required.

What are you planning to do with resetFunc() ?

Thanks ill try that tmrw. The reset was to reboot the arduino and run the code again. My way of restarting that sequence.

mmimadi:
Thanks ill try that tmrw. The reset was to reboot the arduino and run the code again. My way of restarting that sequence.

loop() does exactly what it says, it continuously repeats.
So if you write something like;

void loop()
{
    sequence();
}

The sequence will run, it will return into loop and then loop will be called again so the sequence will repeat.
You don't reset the processor.

Statements like while(1) { }; are infinite loops and the processor will just sit there forever so don't do that.

If you have a fixed sequence that needs delays between the steps you can use the delay() function. However while in the delay() function the processor does nothing. If you want the processor to perform tasks in parallel then you need to avoid using delay().

thank you. I will try that today and update you.