Low budget pre-defined multiple path line follower

hey folks,

I've been building a line follower robot which picks up supplies(food,medicines)from the nurse, traverses to the patient's bed waits there till the person takes the supplies and returns to the start point.... it uses 2 sensors for following the line and 9 sensors in the form of 3x3 matrix to find the path it is supposed to go....using hc-05 Bluetooth module i can choose the path, it is supposed to go .... the problem i am facing is after the robot returns back to start point i can't make it stop :confused: .....I've called functions inside the loop, at the last else if statement in the below code i want the functions to break from the loop and stop the robot so that when i send different inputs like(4,5,6,7) for (forward,left,right,stop) manual mode(Bluetooth remote control).... here is my code

#include <SoftwareSerial.h>
const int ls1=8; //RIGHT
const int ls2=9; //LEFT
const int p1=10; // p1...p9 is matrix
const int p2=11;
const int p3=12;
const int p4=13;
const int p5=A0;
const int p6=A1;
const int p7=A2;
const int p8=A3;
const int p9=A4;
const int ts1=7;
SoftwareSerial BT(0,1); //TX, RX respetively
String readdata;

void linefollower()
{
if(digitalRead(ls1)==LOW&&digitalRead(ls2)==LOW) //moving forward follow the line
{
digitalWrite (3,LOW);
digitalWrite (4,HIGH);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);

}

else if (digitalRead(ls1)==HIGH&&digitalRead(ls2)==LOW) //TURN left
{
digitalWrite (3,HIGH);
digitalWrite (4,LOW);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);
}

else if(digitalRead(ls1)==LOW&&digitalRead(ls2)==HIGH) // TURN right
{
digitalWrite(3, LOW);
digitalWrite (4, HIGH);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
}

else if (digitalRead(ls1)==HIGH&&digitalRead(ls2)==HIGH) //move forward for 4 seconds
{

digitalWrite (3,LOW);
digitalWrite (4,HIGH);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);
delay(4000);

}
}

void one()
{
if(digitalRead(p2)==HIGH&&digitalRead(p4)==HIGH&&digitalRead(p5)==HIGH&&digitalRead(p7)==HIGH&&digitalRead(p8)==HIGH&&digitalRead(p9)==HIGH&&digitalRead(p1)==LOW&&digitalRead(p3)==LOW&&digitalRead(p6)==LOW) // matrix =number 1

{

digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);
delay(5000);
//turn left toward patient bed

}
}

void three()
{
if(digitalRead(p2)==HIGH&&digitalRead(p5)==HIGH&&digitalRead(p7)==HIGH&&digitalRead(p8)==HIGH&&digitalRead(p9)==HIGH&&digitalRead(p1)==LOW&&digitalRead(p3)==LOW&&digitalRead(p4)==LOW&&digitalRead(p6)==LOW) // matrix =number 3
{

digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);
delay(5000);
//turn left toward patient bed

}

}

void five()
{
if(digitalRead(p1)==HIGH&&digitalRead(p2)==HIGH&&digitalRead(p4)==HIGH&&digitalRead(p5)==HIGH&&digitalRead(p7)==HIGH&&digitalRead(p8)==HIGH&&digitalRead(p3)==LOW&&digitalRead(p6)==LOW&&digitalRead(p9)==LOW) // matrix =number 5=stop
{

digitalWrite (3,LOW);
digitalWrite (4,LOW); // stop
digitalWrite (5,LOW);
digitalWrite (6,LOW);
delay(5000);

}
}

void tray() // this sensor detects if there is something on the tray or not
{
digitalWrite (3,LOW);
digitalWrite (4,HIGH);
digitalWrite (5,HIGH);
digitalWrite (6,LOW);
delay(5000);

}

void setup() {
// put your setup code here, to run once:
BT.begin(9600);
Serial.begin(9600);
pinMode(ls1,INPUT); //Pin 8 is connected to the output of ir sensor similarly the rest is self explanatory
pinMode(ls2,INPUT);
pinMode(p1,INPUT);
pinMode(p2,INPUT);
pinMode(p3,INPUT);
pinMode(p4,INPUT);
pinMode(p5,INPUT);
pinMode(p6,INPUT);
pinMode(p7,INPUT);
pinMode(p8,INPUT);
pinMode(p9,INPUT);
pinMode(ts1,INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
while (BT.available()){
delay(10);
char c = BT.read();
readdata += c; //standard protocol
}
if (readdata.length() > 0) {
Serial.println(readdata);

if(readdata == "1")
{
if (digitalRead(ts1)==LOW)
{ linefollower();
one(); //go to patient one only if there is somthing on the tray
linefollower();
five();
}
else if (digitalRead(ts1)==HIGH) // after the patient takes the food it comes back
{

linefollower();

}
}

else if(readdata == "2")
{
if (digitalRead(ts1)==LOW)
{
linefollower();
three();
linefollower();
five();
}
else if (digitalRead(ts1)==HIGH)
{

linefollower();
// same as readdata=1 here, so far the program runs the way i want to
}
}
else if (readdata == "4") // it should stop when i press 4 but,since everything is stacked on the loop it dosen't
{ // i am poor at programming so please help me instead of trolling

digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
delay (100);
return; // i have no idea how to break and return the funtions i have called inside the loop
// or maybe that's not possible any ideas to reformat the program to make it stop and add remote control
}

// thanks :wink:

}

}

// p1...p9 is matrix

So why isn't it, you know, a matrix?
Your code needs factoring.

What do your debug prints tell you about how the device is behaving?

Please remember to use code tags when posting code.

well, u know in a matrix u can assign multiple values ...like in a 3x3 matrix u can assign 21 numbers i guess or 21 different possibilities....just an idea couldn't think of anything else... factoring how?...can u please elaborate... i don't understand debug print but, it works the way i want it to in automatic mode....this was my first post i'll use code tags from now on thanks for tip bruh and here is my map

..like in a 3x3 matrix u can assign 21 numbers i guess or 21 different possibilities.

Where does 21 come from?
29 = 512

And the delays.
They have to go.

yup, you are absolutely right i should have been more precise with my explanation .... possibilites of labelling numbers in the matrix was my dumb idea ...like in the pic below...i might still not be right with 21...forgive me... what should i do? replace delays with more conditions or micro() or bwd :sweat_smile: