Can an Else If statement return to run an If statement?

Hello everybody,

I'm new to Arduino, and I have a relatively simple question to ask. It is possible for an else if statement to return and run the original if statement code upon completion?

Here is my code:

#include <IRremote.h>
#include <MotorLibrary.h>

MotorLibrary motorLibrary;
int RECV_PIN = 2; //define input pin on Arduino
IRrecv irrecv(RECV_PIN);
decode_results results;

#include <Ultrasonic.h>
int PIEZOPin = 9; // Pin used for the PIEZO Speaker
int maximumRange = 45; // Maximum range needed
int minimumRange = 0; // Minimum range needed

Ultrasonic ultrasonic(5, 6); // (Trig PIN,Echo PIN)

void setup()
{
pinMode(PIEZOPin, OUTPUT);// Use Piezo (if required)  
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
motorLibrary.debugMotors(true);
}
void loop() {

long distance = ultrasonic.Ranging(CM); // CM or INC
//Serial.print("Object distance (in cm's) = ");

if (results.actualKey == "*")
{
  motorLibrary.forward(255); 
}
else if (distance <= maximumRange)
{
  motorLibrary.stopMotors();  
}
 irrecv.resume(); // Receive the next value
}

Basically, I want my tank to detect and object, avoid it, and then continue on.

I've tried using while statements and the like to no avail, so I'm back to where I have started.

Any help is greatly appreciated :slight_smile:
:slight_smile:

The loop() function loops, very rapidly.

All you need to do is, somewhere in the loop function, execute a single if block:

if (object_detected) {
  do_avoid_object();
  object_detected = false;  //may not be needed
 }

The program will continue on until the end of the loop() function and loop() will be called again, to start over.

jremington:
The loop() function loops, very rapidly.

All you need to do is, somewhere in the loop function, execute a single if block:

if (object_detected) {

do_avoid_object();
  object_detected = false;  //may not be needed
}



The program will continue on until the end of the loop() function and loop() will be called again, to start over.

Thanks for the reply! I'll try it out now