Adding several loops

Hello. So I am currently making a line follower robot with 3 sensors and the maze we have to finish has several areas where the IR sensors used for detecting the line will all be detecting the line at the same time. I want to use these as sort of checkpoints and want to break the loop present and move onto another new and different loop where certain things will be different such as movement speeds according to the area of the maze. Ive tried taking help from chatgpt and other forums but cant find my exact answer as everyone seems to simply use the same void loop but using a while function in the middle . Please can someone help me as i need a fully new loop in which i can once again paste the line follower code and change it according to my preferance. Once again i want the criterea for a new loop to start to be that all IR sensors (3 sensors) detect the black line

Here is my current code for the line follower using 3 ir sensors 2 motors and a L298N motor driver:

#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 200
#define IR_SENSOR_MIDDLE 13
//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

void setup()
{
  //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  //For that we need to increase the frequency of analogWrite.
  //Below line is important to change the frequency of PWM signal on pin D5 and D6
  //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  //This sets frequecny as 7812.5 hz.
  TCCR0B = TCCR0B & B11111000 | B00000010 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT);
  pinMode(IR_SENSOR_LEFT, INPUT);
  pinMode(IR_SENSOR_MIDDLE, INPUT);
  rotateMotor(0,0);   
}


void loop()
{

  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
  int midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
    rotateMotor(255, 255);
  }
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == LOW)
  {
      while (midIRsensorvalue == LOW) {
      rotateMotor(160, 230);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  }
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == LOW)
  {
    while (midIRsensorvalue == LOW) {
      rotateMotor(230, 160);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  } 

   else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
      while (rightIRSensorValue == HIGH) {
      rotateMotor(160, 215);
      rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
      }
  }

   else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
      while (leftIRSensorValue == HIGH) {
      rotateMotor(215, 160);
      leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
      }
  }
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }
}


void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);      
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);      
  }

  if (leftMotorSpeed < 0)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);    
  }
  else if (leftMotorSpeed > 0)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);      
  }
  else 
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);      
  }
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}

Hello hamzahatpp

The basic Arduino sketch provides one loop() function only.

You can design and code your own functions() to be called from this loop().

Have a nice day and enjoy coding in C++.

I am sorry for annoying you but can you please give a small example of how these functions will switch between each other perhaps one function that blinks an led twice and then a second one that does this forever

The comment above should be // if mid sensor detects the black line, then go straight

The line above should have the comment // if mid and right sensor detect the black line, go slight right

The line above should have the comment // if mid and left sensors detect the black line, go slight left

A missing action is if all sensors are LOW, then STOP

if (rightIRSensorValue == LOW && leftIRSensorValue == LOW && midIRsensorvalue == LOW) {
    rotateMotor(0,0);
} 

I think the line you want for detecting the "end of maze" black blocks is something like this:

// if all sensors are HIGH, end of maze or call a new function
if (rightIRSensorValue == HIGH && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH) {
    // new function call
}

yeah bro i copied the code from somewhere else and changed it a bunch so the comments make no sense sorry i forgot to mention just please ignore it

Could you please give me an example of how the multiple functions in a loop work please im stuck rn

I gave you examples in my previous comment... which were related to the motors in the sketch.

If you want to blink an LED, use your IDE >> FILE >> EXAMPLES >> BASIC EXAMPLES

Sorry to bother you so much but this is what i figured out and wrote so far please ignore the comments the problem here is that even if the condition for stopping function number1 isnt met the function number2 still seems to be beginning as it says in the serial monitor (i have the bot so i can see that it start the second function even if only the middle sensors detects black.
Heres the code:

#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 200
#define IR_SENSOR_MIDDLE 13
//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

boolean thing1=true;
void setup()
{
  //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  //For that we need to increase the frequency of analogWrite.
  //Below line is important to change the frequency of PWM signal on pin D5 and D6
  //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  //This sets frequecny as 7812.5 hz.
  TCCR0B = TCCR0B & B11111000 | B00000010 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT); 
  pinMode(IR_SENSOR_LEFT, INPUT);
  pinMode(IR_SENSOR_MIDDLE, INPUT);
  rotateMotor(0,0);   
  
  Serial.begin (9600);
}
 void loop(){
number1();
number2();
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);      
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);      
  }

  if (leftMotorSpeed < 0)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);    
  }
  else if (leftMotorSpeed > 0)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);      
  }
  else 
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);      
  }
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}
void number1()
{
  if (!thing1) return;
  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
  int midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
    rotateMotor(255, 255);
  }
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == LOW)
  {
      while (midIRsensorvalue == LOW) {
      rotateMotor(160, 230);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  }
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == LOW)
  {
    while (midIRsensorvalue == LOW) {
      rotateMotor(230, 160);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  } 

   else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
      while (rightIRSensorValue == HIGH) {
      rotateMotor(160, 215);
      rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
      }
  }

   else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
      while (leftIRSensorValue == HIGH) {
      rotateMotor(215, 160);
      leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
      }
  }

  else if (rightIRSensorValue == HIGH && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
   boolean thing1 = false;
   
  }
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }

}
void number2 (){
  Serial.println("loop 2 begins");
  rotateMotor(0, 0);
}

You are calling number1() and number2() maybe set a "stopflag" in number1() that gets checked in number2()

void number2 (){
  if (!stopflag) {
    Serial.println("loop 2 begins");
  }
}

It kinda worked i just added the line if (thing1){
at the start of the second function but now theres a new problem where i think because in the first function it was set to a while loop where it would keep turning until the middle sensor would turn black it now doesnt stop at the intersection of the line (where all sensors would turn black) but instead keeps moving. i held the robot in my hand and if the loop isnt active (only the middle sensor is black not the side ones) then the robot does stop but it is in the loop (i would make the right sensor detect the line and then place the robot at the intersection) it would keep moving the motor. Any suggestion on how to fix this?

Sounds like your sensors are not aligned or mis-wired. Block two sensor receivers with tape and see how each individual sensor reacts to a black line, no black line and a large black area. Then test the other two sensors the same way.

the sensors work perfectly fine ive tested it seperately plus the first loop works perfectly (simple line follower) soooo the sensors have to be fine

if a while loop is ongoing will the arduino still detect the other changes (basically asking that SHOULD the robot still stop after detecting black on all its sensors even if a while loop hasnt been fulfilled yet)

Hey ive looked at it some more and its seem like the second function/loop only seems to work (start) if all the ir sensors are black at the start so if one detects a white line and then all of them detect black line it will no longer go to the second loop would you have any idea why?

Hello. So i am currently making a line follower robot using 3 ir sensors 2 motors and a L298N motor driver. The maze (basically just a line) has many intersections where all 3 sensors would be detecting black and i want to use these as checkpoints. The current code i have written is supposed to be a normal line follower but as soon as all three sensors detect the line at the same time it is supposed to stop and stay still. However using the serial monitor i can see that it only goes to the next function when all three sensors detect black as soon as i turn it on. If upon turning on even one detects white then later if all three do detect black it does not go over to the next function (which just says stop) can anyone please help me here the code is:

#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 200
#define IR_SENSOR_MIDDLE 13
//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

boolean thing1=true;
void setup()
{
  //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  //For that we need to increase the frequency of analogWrite.
  //Below line is important to change the frequency of PWM signal on pin D5 and D6
  //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  //This sets frequecny as 7812.5 hz.
  TCCR0B = TCCR0B & B11111000 | B00000010 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT); 
  pinMode(IR_SENSOR_LEFT, INPUT);
  pinMode(IR_SENSOR_MIDDLE, INPUT);
  rotateMotor(0,0);   
  
  Serial.begin (9600);
}
 void loop(){
number1();
number2();
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);      
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);      
  }

  if (leftMotorSpeed < 0)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);    
  }
  else if (leftMotorSpeed > 0)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);      
  }
  else 
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);      
  }
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}
void number1()
{
  if (!thing1) return;
  
  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
  int midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
    rotateMotor(255, 255);
  }
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == LOW)
  {
      while (midIRsensorvalue == LOW) {
      rotateMotor(160, 230);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  }
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == LOW)
  {
    while (midIRsensorvalue == LOW) {
      rotateMotor(230, 160);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  } 

   else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
      while (rightIRSensorValue == HIGH) {
      rotateMotor(160, 215);
      rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
      }
  }

   else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
      while (leftIRSensorValue == HIGH) {
      rotateMotor(215, 160);
      leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
      }
  }

  else if (rightIRSensorValue == HIGH && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
  boolean thing1=false;
  }
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }

}
void number2 (){
  
  Serial.println("loop 2 begins");
  rotateMotor(0, 0);
 
}


It might be clearer to combine your sensor results into a bitmap:

000 = no sensor active
xx1 = right active
x1x = mid active
1xx = left active 

Then you could use a switch statement:

switch( bitmap )
{
   case 0b000: // no sensors active
               break;
   case 0b001: // right only
               break;
   case 0b010: // mid only
               break;
   case 0b011: // right & mid
               break;
   case 0b100: // left only
               break;
   :
   :
   etc...
}

Your number1() function does not have a routine for stopping.

Your loop() says to run number2() every loop without checking any sensor.

https://forum.arduino.cc/t/switching-between-different-functions/1137879

thanks a lot i fixed the code and it has TECHNICALLY worked but the problem now is that the robot is too fast to detect that the intersection has come and so just drives over it. I am thinking of adding two more ir sensors a little more forward then the rest and make it so that when one of them detects any black line it slows down the robot somehow. Can you help with this or if you have a better idea can you tell it here is my current code:

#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 200
#define IR_SENSOR_MIDDLE 13
//Right motor
int enableRightMotor=6;
int rightMotorPin1=7;
int rightMotorPin2=8;

//Left motor
int enableLeftMotor=5;
int leftMotorPin1=9;
int leftMotorPin2=10;

bool thing1=false;
void setup()
{
  //The problem with TT gear motors is that, at very low pwm value it does not even rotate.
  //If we increase the PWM value then it rotates faster and our robot is not controlled in that speed and goes out of line.
  //For that we need to increase the frequency of analogWrite.
  //Below line is important to change the frequency of PWM signal on pin D5 and D6
  //Because of this, motor runs in controlled manner (lower speed) at high PWM value.
  //This sets frequecny as 7812.5 hz.
  TCCR0B = TCCR0B & B11111000 | B00000010 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT); 
  pinMode(IR_SENSOR_LEFT, INPUT);
  pinMode(IR_SENSOR_MIDDLE, INPUT);
  rotateMotor(0,0);   
  
  Serial.begin (9600);
}
 void loop(){
if (!thing1) {
    number();
  } else {
    number2();
  }
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);    
  }
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);      
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);      
  }

  if (leftMotorSpeed < 0)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);    
  }
  else if (leftMotorSpeed > 0)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);      
  }
  else 
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);      
  }
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}
void number()
{
  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
  int midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
    rotateMotor(160, 160);
  }
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == LOW)
  {
      while (midIRsensorvalue == LOW) {
      rotateMotor(100, 150);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  }
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == LOW)
  {
    while (midIRsensorvalue == LOW) {
      rotateMotor(150, 100);
      midIRsensorvalue = digitalRead(IR_SENSOR_MIDDLE);
      }
  } 

   else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW && midIRsensorvalue == HIGH)
  {
      while (rightIRSensorValue == HIGH) {
      rotateMotor(100, 150);
      rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
      }
  }

   else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
      while (leftIRSensorValue == HIGH) {
      rotateMotor(150, 100);
      leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);
      }
  }

  else if (rightIRSensorValue == HIGH && leftIRSensorValue == HIGH && midIRsensorvalue == HIGH)
  {
   thing1 = true;
   Serial.println("getting outta first");
   
  }
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }

}
void number2 (){
  Serial.println("loop 2 begins");
  rotateMotor(-255, -255);
}

You could speed your processing as pointed to by @awneil from your other topic.
You could slow the motor speed.
You could replace "poling" (if..then..else) with interrupts that sense when pins rise, fall, change or are low.