combine two codes with condition

Hi All, I have read this tutorial on how to combine 2 arduino sketches (Demo of merging the code from two sketches - Programming Questions - Arduino Forum), however, my case is a bit different. I wan't to only run either of the two depending which one meets the condition. My pseudo code would be below.


start looking for obstacle on left, right and front side

if there is no obstacle
run codeA
else
run codeB

CodeA //this should run in loop until arduino is turned off

CodeB //this should run in loop until arduino is turned off


CodeA is for line following code like this Follower 4 sensors Lesson 5 - YouTube

and codeB is for wall maze like this Arduino Microbot Solves Maze - YouTube

now, I only want to run my code above once and not in loop so that it wont keep searching for obstacles when it has met the condition. Do I put it in a void loop or I can just name it "void postStartup" so that it will only run once?

Thanks in advance.

Create a global variable. In setup() set it to 1 (look for obstacle)

In loop() if it is 1 do look for obstacle function. In that function set the variable to 2 do CodeA or 3 do CodeB.

Steve

Thanks Steve for a quick reply.

as what I have understand on yyour suggestion, my code would be


void setup()

variable1 //start looking for obstacle on left, right and front side

void loop

if there is no obstacle
run codeA
else
run codeB

void codeA

void codeB


is that right? also note that codeA and B contains a loop within themselves.

I don't think you want that code in setup AND in loop.

CtrlAltElite:
I don't think you want that code in setup AND in loop.

Yes, forgot to remove it, was just copy pasting, edited it now.

start of loop()
  if obstacle detected
     call functionA
  else
     call functionB
  end if
end of loop()

functionA  //must be non blocking and return to loop()
//code
end of function A

functionB  //must be non blocking and return to loop()
//code
end of function B