Help with void loop

hi guys i have code in which i have created functions and called it in main void loop via if else statement but the problem is whenever the statement gets true it executes the code only for 1 loop but i want that it executes nonstop unlimited loop like void loop does.What is problem with my code please help me out

here is my code

ardu#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(13,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("serial communication stablished");
  
  servo_motor.attach(9); //our servo pin

  servo_motor.write(90);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
if (Serial.available())
  {
    char inChar = Serial.read();
    Serial.println(inChar);
    if (inChar == '1')
    led();
    {
     
     Serial.println("Command completed LED turned ON");
    }
    if (inChar == '2')
    {
     digitalWrite(13, LOW);
     Serial.println("Command completed LED turned OFF");
    }
    if (inChar == '3')
    {
     automode();
     Serial.println("automode turned ON");
    }
    if (inChar == '4')
    {
     moveStop(); 
     Serial.println("automode turned OFF");
     }
  } 
}
int led (){
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

int automode(){
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(1);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
}

int lookLeft(){
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

Thanks in advance

Make inChar a global variable. Or a static local variable.

I assume you are referring to the long delays in your functions? You will have to re-design your entire program using millis() instead of delay(), non-blocking cooperative multitasking using a state machine.

sterretje:
Make inChar a global variable. Or a static local variable.

And put a closing brace after this:

if (Serial.available())
  {
    char inChar = Serial.read();
    Serial.println(inChar);

You'll need to remove the one that currently ends the block controlled by the if.

sterretje:
Make inChar a global variable. Or a static local variable.

i am not able to make it global in this program it is getting errors

Post your code.
Post the errors. Include the full text of the error.

//here is the new code

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name

char inChar = Serial.read();
void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(13,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("serial communication stablished");
  
  servo_motor.attach(9); //our servo pin

  servo_motor.write(90);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
if (Serial.available())
  {
    
    Serial.println(inChar);
    if (inChar == '1')
    
    {
     led();
     Serial.println("Command completed LED turned ON");
    }
    if (inChar == '2')
    {
     digitalWrite(13, LOW);
     Serial.println("Command completed LED turned OFF");
    }
    if (inChar == '3')
    {
     automode();
     Serial.println("automode turned ON");
    }
    if (inChar == '4')
    {
     moveStop(); 
     Serial.println("automode turned OFF");
     }
  } 
}
int led (){
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

int automode(){
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(1);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
}

int lookLeft(){
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

i is continously printing question mark an didnt turn on the led for atleast 1 loop

char inChar = Serial.read();

Think carefully about that line.

if (Serial.available())
  {
   
    Serial.println(inChar);
    if (inChar == '1')

Did we forget an important something here?

TheMemberFormerlyKnownAsAWOL:

char inChar = Serial.read();

Think carefully about that line.

sorry my mind is freezed i am not able to think i am frustrated

I think on your other thread on a very similar topic, I suggested a flag for auto-mode.

Why did you decide that was a bad idea?

TheMemberFormerlyKnownAsAWOL:
I think on your other thread on a very similar topic, I suggested a flag for auto-mode.

Why did you decide that was a bad idea?

i dont know what i flag mode and i noticed that my code is working for one loop but not repeating so i stayed with my code

char inChar = Serial.read();

I said to make it a global variable; not to read the serial port there.

It's not guaranteed that the Serial object is already initialised at that point.
2)
The port is not initialised with 9600 at that point.
3)
The code in loop() never reads the serial port.

...
...
char inChar;
...
...

void setup()
{
  ...
  ...
}

void loop()
{
  if (Serial.available())
  {
    inChar = Serial.read();
  }

  if (inChar == '1')
  {
    ...
    ...
  }

  etc  
}

A flag is just a Boolean.
You test it with an "if", and if it true, you do one thing and keep looping and testing until it turns false, doing your auto-mode thing

If you didn't know what it was, why didn't you ask?

TheMemberFormerlyKnownAsAWOL:
A flag is just a Boolean.
You test it with an "if", and if it true, you do one thing and keep looping and testing until it turns false, doing your auto-mode thing

If you didn't know what it was, why didn't you ask?

actually i am new to programming and i dont want to make you bore and frustrated

sterretje:

char inChar = Serial.read();

I said to make it a global variable; not to read the serial port there.

It's not guaranteed that the Serial object is already initialised at that point.
2)
The port is not initialised with 9600 at that point.
3)
The code in loop() never reads the serial port.

...

...
char inChar;
...
...

void setup()
{
  ...
  ...
}

void loop()
{
  if (Serial.available())
  {
    inChar = Serial.read();
  }

if (inChar == '1')
  {
    ...
    ...
  }

etc 
}

sorry sir making it global didnt solved my problem again it is looping for only once

// here is the code suggested by you

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
char inChar;

void setup(){

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(13,OUTPUT);
  
  Serial.begin(9600);
  Serial.println("serial communication stablished");
  
  servo_motor.attach(9); //our servo pin

  servo_motor.write(90);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){
if (Serial.available())
  {
    char inChar = Serial.read();
    Serial.println(inChar);
    if (inChar == '1')
    {
     led();
     Serial.println("Command completed LED turned ON");
    }
    if (inChar == '2')
    {
     digitalWrite(13, LOW);
     Serial.println("Command completed LED turned OFF");
    }
    if (inChar == '3')
    {
     automode();
     Serial.println("automode turned ON");
    }
    if (inChar == '4')
    {
     moveStop(); 
     Serial.println("automode turned OFF");
     }
  } 
}
int led (){
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

int automode(){
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(1);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
}

int lookLeft(){
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

That's not what I suggested; you're creating a new variable inChar in your loop(); that is not the same variable as the global one.

Think about what you have now.
loop executes, you have a character available to read.
You read it.
It's '1'.
So you call "led()".
The function returns and loop() gets called again.
You no longer have a character available, so you don't read and you don't examine the character you haven't got, so you don't call led() again.

TheMemberFormerlyKnownAsAWOL:
It's '1'.
So you call "led()".

...and all the other program features are non-responsive until the LED stops blinking.

TheMemberFormerlyKnownAsAWOL:
Think about what you have now.
loop executes, you have a character available to read.
You read it.
It's '1'.
So you call "led()".
The function returns and loop() gets called again.
You no longer have a character available, so you don't read and you don't examine the character you haven't got, so you don't call led() again.

yaa i am able to understand thats the problem. how to make it continuous?

aarg:
...and all the other program features are non-responsive until the LED stops blinking.

led stops blinking after 2 seconds as stated by delay and didnt repeats :o :o :o