Using a while loop in void loop

hi my name is kenneth this is my 2nd post here. sorry if anything i say here is unclear

i am using a qrd1114 sensor getting Digital reading and i can display my digital reading perfectly fine.

now i am trying to make use of a while loop my code are

void loop
{
while(frontsensor==1)
{

MotorRun();

}
MotorStop();
}

it is able to enter the loop but why is it unable to exit the while loop?

MotorRun(); is a function just to move both wheel forward

i know that void loop is a unlimited loop but i need to use this while command as i am programming once the robot does line tracking and reaches a point when the trigger is condition activated it goes into a while loop and does the next thing

I am using a while loop in

main loop
{
}

it does not exit the loop when the loop condition is trigger.

So i research thru the internet but i still don't have a rough idea how millis(); work
i know it just count up the value.

i am trying to use it as a while loop

So i research thru the internet but i still don't have a rough idea how millis(); work

Do you have a wristwatch or a clock? Then, you DO know how millis() works.

i am trying to use it as a while loop

Not anywhere in that won't-even-compile snippet you posted.

You need to post your real code (ALL of it) and explain what it does and how that differs from what you want.

Hi Kenneth

Please post your complete code.

while(frontsensor==1)
{

MotorRun();

}

The while loop will only end when frontsensor changes to something that is not 1. Does it get changed in MotorRun()?

Regards

Ray

okay sorry guys i will copy and paste the whole code here by tomorrow .

okay sorry guys i will copy and paste the whole code here by tomorrow .

One is a "delay()" function.

The other is a "while" loop.

You refer to a "void loop" which is more correctly the main "loop()" function.

This is actually the "while(1)" loop provided for you.

Inside it you probably want an "if" statement that selects either MotorRun() or MotorStop(). But somewhere on each pass of that loop you will need to be determining whether to change the value of the variable "frontsensor".

kenneth619:
okay sorry guys i will copy and paste the whole code here by tomorrow .

Always the smartest idea. :smiley:

The demo code in the first post of this Thread illustrates the use of millis().

...R

The millis() function works line a stop-watch, in that it counts time. When your Arduino is started, or the RESET button is pressed, a timer count begins. One way to access this time is with millis(), which tells you how many milliseconds have elapsed since the last reset. The variable used to hold the number of milliseconds eventually reaches its maximum, and wraps around to zero and starts over again.

The millis() function is probably not what you want for a loop (although maybe it is). You should look at the DelayMilliseconds() function as well. We'll know for certain once you post your code (at least enough of it to reproduce the problem).

You should look at the DelayMilliseconds() function as well.

Why? (And it's delayMicroseconds()).

Hi guys here is my code my problem is it never exit the while loop

//----FRONT RIGHT SENSOR---- //
int FrontRight_Left = 40;
int FrontRight_Mid = 41;
int FrontRight_Right = 42;

int FrontRight_Left_sensorvalue;
int FrontRight_Mid_sensorvalue;
int FrontRight_Right_sensorvalue;

//----FRONT Left SENSOR---- //
int FrontLeft_Left = 44;
int FrontLeft_Mid = 45;
int FrontLeft_Right = 46;

int FrontLeft_Left_sensorvalue;
int FrontLeft_Mid_sensorvalue;
int FrontLeft_Right_sensorvalue;

//--BACK Right Sensor--//
int BackRight_Left = 48;
int BackRight_Right = 49;

int BackRight_Left_sensorvalue;
int BackRight_Right_sensorvalue;

//--BACK Left Sensor--//

int BackLeft_Left = 50;
int BackLeft_Right = 51;

int BackLeft_Left_sensorvalue;
int BackLeft_Right_sensorvalue;



///--------MOTOR--------///

//---Motor A //
int ENA = 2;
int IN1 = 22;
int IN2 = 23;

//--- MotorB //
int ENB = 3;
int IN3 = 24;
int IN4 = 25;



void setup() {
  
Serial.begin(9600);

pinMode(FrontRight_Left, INPUT);
pinMode(FrontRight_Mid, INPUT);
pinMode(FrontRight_Right, INPUT);

pinMode(FrontLeft_Left, INPUT);
pinMode(FrontLeft_Mid, INPUT);
pinMode(FrontLeft_Right, INPUT);

 
pinMode(BackRight_Left, INPUT);
pinMode(BackRight_Right, INPUT);

pinMode(BackLeft_Left, INPUT);
pinMode(BackLeft_Right, INPUT);

pinMode (ENA, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT); 

pinMode (ENB, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT); 

  
analogWrite(ENA,0);
analogWrite(ENB,0);
count =1;


}
  
  
void loop() {

LineSensorReading();

//DisplayReading();
//FrontRightFollowingLine();
//FrontLeftFollowingLine();

while(FrontRight_Mid_sensorvalue==1)
{
  Forward();
}
MotorStop();
 
}
void LineSensorReading()
{
  
FrontRight_Left_sensorvalue=digitalRead(FrontRight_Left);
FrontRight_Mid_sensorvalue=digitalRead(FrontRight_Mid);
FrontRight_Right_sensorvalue=digitalRead(FrontRight_Right);

FrontLeft_Left_sensorvalue=digitalRead(FrontLeft_Left);
FrontLeft_Mid_sensorvalue=digitalRead(FrontLeft_Mid);
FrontLeft_Right_sensorvalue=digitalRead(FrontLeft_Right);

BackRight_Left_sensorvalue=digitalRead(BackRight_Left);
BackRight_Right_sensorvalue=digitalRead(BackRight_Right);

BackLeft_Left_sensorvalue=digitalRead(BackLeft_Left);
BackLeft_Right_sensorvalue=digitalRead(BackLeft_Right);
  
} 

void DisplayReading()
{
Serial.print("FrontRight_Left_sensorvalue  ");
Serial.print(FrontRight_Left_sensorvalue);
Serial.print("   ");
Serial.print("FrontRight_Mid_sensorvalue  ");
Serial.print(FrontRight_Mid_sensorvalue);
Serial.print("   ");
Serial.print("FrontRight_Right_sensorvalue  ");
Serial.print(FrontRight_Right_sensorvalue);
Serial.println("");


Serial.print("FrontLeft_Left_sensorvalue  ");
Serial.print(FrontLeft_Left_sensorvalue);
Serial.print("   ");
Serial.print("FrontLeft_Mid_sensorvalue  ");
Serial.print(FrontLeft_Mid_sensorvalue);
Serial.print("   ");
Serial.print("FrontLeft_Right_sensorvalue  ");
Serial.print(FrontLeft_Right_sensorvalue);
Serial.println("");

Serial.print("BackRight_Left_sensorvalue  ");
Serial.print(BackRight_Left_sensorvalue);
Serial.print("   ");
Serial.print("BackRight_Right_sensorvalue  ");
Serial.print(BackRight_Right_sensorvalue);
Serial.println("");
 
Serial.print("BackLeft_Left_sensorvalue  ");
Serial.print(BackLeft_Left_sensorvalue);
Serial.print("   ");
Serial.print("BackLeft_Right_sensorvalue  ");
Serial.print(BackLeft_Right_sensorvalue);
Serial.println(""); 
Serial.println("");
Serial.println("");
Serial.println(""); 
Serial.println("");
Serial.println("");
}

void Reverse()
{
  
analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);  
  
}

void Forward()
{
  
analogWrite(ENA,150);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,150);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);

}

void TurnLeftSlight()
{
  
analogWrite(ENA,100);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);  
  
}

void TurnRightSlight()

{
  
analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,100);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);  

}


void TurnLeft()

{
  
analogWrite(ENA,140);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);  

}

void TurnRight()

{
  
analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,140);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);  

}



void MotorStop()
{
analogWrite(ENA,0);
 
analogWrite(ENB,0);
  
}

void FrontRightFollowingLine()
{
 
 if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==1))
{
  Forward();
  
}

else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==1))
{
 
 TurnRightSlight();
}

else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==0))
{
  TurnLeftSlight();
}

else if((FrontRight_Mid_sensorvalue==1)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==0))
{
  TurnLeft();
}
else if((FrontRight_Mid_sensorvalue==1)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==1))
{
  TurnRight();
}
else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==0))
{
  Forward(); 

 
}

 
}

void FrontLeftFollowingLine()
{
if((FrontLeft_Mid_sensorvalue==0)&&(FrontLeft_Left_sensorvalue==1)&&(FrontLeft_Right_sensorvalue==1))
{
  Forward();
}

else if(((FrontLeft_Mid_sensorvalue>=0&&FrontLeft_Mid_sensorvalue<=400))&&((FrontLeft_Left_sensorvalue>=0&&FrontLeft_Left_sensorvalue<=400))&&((FrontLeft_Right_sensorvalue>=400&&FrontLeft_Right_sensorvalue<=800)))
{
 TurnLeftSlight();
}

else if(((FrontLeft_Mid_sensorvalue>=0&&FrontLeft_Mid_sensorvalue<=400))&&((FrontLeft_Left_sensorvalue>=400&&FrontLeft_Left_sensorvalue<=800))&&((FrontRight_Right_sensorvalue>=0&&FrontLeft_Right_sensorvalue<=400)))
{
  TurnRightSlight();
}

else if(((FrontLeft_Mid_sensorvalue>=400&&FrontLeft_Mid_sensorvalue<=800))&&((FrontLeft_Left_sensorvalue>=400&&FrontLeft_Left_sensorvalue<=800))&&((FrontLeft_Right_sensorvalue>=0&&FrontLeft_Right_sensorvalue<=400)))
{
  TurnRight();
}
else if(((FrontLeft_Mid_sensorvalue>=400&&FrontLeft_Mid_sensorvalue<=800))&&((FrontLeft_Left_sensorvalue>=0&&FrontLeft_Left_sensorvalue<=400))&&((FrontLeft_Right_sensorvalue>=400&&FrontLeft_Right_sensorvalue<=800)))
{
  TurnLeft();
}
else 
{
  MotorStop();
  
}
 

  
}


void TurnLeft90deg()

{
  analogWrite(ENA,110);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);  
}

my condition will trigger the while loop but why does it not exit when the condition is false

//----FRONT RIGHT SENSOR---- //
int FrontRight_Left = 40;
int FrontRight_Mid = 41;
int FrontRight_Right = 42;

int FrontRight_Left_sensorvalue;
int FrontRight_Mid_sensorvalue;
int FrontRight_Right_sensorvalue;

//----FRONT Left SENSOR---- //
int FrontLeft_Left = 44;
int FrontLeft_Mid = 45;
int FrontLeft_Right = 46;

int FrontLeft_Left_sensorvalue;
int FrontLeft_Mid_sensorvalue;
int FrontLeft_Right_sensorvalue;

//--BACK Right Sensor--//
int BackRight_Left = 48;
int BackRight_Right = 49;

int BackRight_Left_sensorvalue;
int BackRight_Right_sensorvalue;

//--BACK Left Sensor--//

int BackLeft_Left = 50;
int BackLeft_Right = 51;

int BackLeft_Left_sensorvalue;
int BackLeft_Right_sensorvalue;

///--------MOTOR--------///

//---Motor A //
int ENA = 2;
int IN1 = 22;
int IN2 = 23;

//--- MotorB //
int ENB = 3;
int IN3 = 24;
int IN4 = 25;

void setup() {

Serial.begin(9600);

pinMode(FrontRight_Left, INPUT);
pinMode(FrontRight_Mid, INPUT);
pinMode(FrontRight_Right, INPUT);

pinMode(FrontLeft_Left, INPUT);
pinMode(FrontLeft_Mid, INPUT);
pinMode(FrontLeft_Right, INPUT);

pinMode(BackRight_Left, INPUT);
pinMode(BackRight_Right, INPUT);

pinMode(BackLeft_Left, INPUT);
pinMode(BackLeft_Right, INPUT);

pinMode (ENA, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);

pinMode (ENB, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);

analogWrite(ENA,0);
analogWrite(ENB,0);

}

void loop() {

LineSensorReading();

//DisplayReading();
//FrontRightFollowingLine();
//FrontLeftFollowingLine();

while(FrontRight_Mid_sensorvalue==1)
{
Forward();
}
MotorStop();

}
void LineSensorReading()
{

FrontRight_Left_sensorvalue=digitalRead(FrontRight_Left);
FrontRight_Mid_sensorvalue=digitalRead(FrontRight_Mid);
FrontRight_Right_sensorvalue=digitalRead(FrontRight_Right);

FrontLeft_Left_sensorvalue=digitalRead(FrontLeft_Left);
FrontLeft_Mid_sensorvalue=digitalRead(FrontLeft_Mid);
FrontLeft_Right_sensorvalue=digitalRead(FrontLeft_Right);

BackRight_Left_sensorvalue=digitalRead(BackRight_Left);
BackRight_Right_sensorvalue=digitalRead(BackRight_Right);

BackLeft_Left_sensorvalue=digitalRead(BackLeft_Left);
BackLeft_Right_sensorvalue=digitalRead(BackLeft_Right);

}

void DisplayReading()
{
Serial.print("FrontRight_Left_sensorvalue ");
Serial.print(FrontRight_Left_sensorvalue);
Serial.print(" ");
Serial.print("FrontRight_Mid_sensorvalue ");
Serial.print(FrontRight_Mid_sensorvalue);
Serial.print(" ");
Serial.print("FrontRight_Right_sensorvalue ");
Serial.print(FrontRight_Right_sensorvalue);
Serial.println("");

Serial.print("FrontLeft_Left_sensorvalue ");
Serial.print(FrontLeft_Left_sensorvalue);
Serial.print(" ");
Serial.print("FrontLeft_Mid_sensorvalue ");
Serial.print(FrontLeft_Mid_sensorvalue);
Serial.print(" ");
Serial.print("FrontLeft_Right_sensorvalue ");
Serial.print(FrontLeft_Right_sensorvalue);
Serial.println("");

Serial.print("BackRight_Left_sensorvalue ");
Serial.print(BackRight_Left_sensorvalue);
Serial.print(" ");
Serial.print("BackRight_Right_sensorvalue ");
Serial.print(BackRight_Right_sensorvalue);
Serial.println("");

Serial.print("BackLeft_Left_sensorvalue ");
Serial.print(BackLeft_Left_sensorvalue);
Serial.print(" ");
Serial.print("BackLeft_Right_sensorvalue ");
Serial.print(BackLeft_Right_sensorvalue);
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
}

void Reverse()
{

analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);

}

void Forward()
{

analogWrite(ENA,150);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,150);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);

}

void TurnLeftSlight()
{

analogWrite(ENA,100);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);

}

void TurnRightSlight()

{

analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,100);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);

}

void TurnLeft()

{

analogWrite(ENA,140);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);

}

void TurnRight()

{

analogWrite(ENA,0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENB,140);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);

}

void MotorStop()
{
analogWrite(ENA,0);

analogWrite(ENB,0);

}

void FrontRightFollowingLine()
{

if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==1))
{
Forward();

}

else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==1))
{

TurnRightSlight();
}

else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==0))
{
TurnLeftSlight();
}

else if((FrontRight_Mid_sensorvalue==1)&&(FrontRight_Left_sensorvalue==1)&&(FrontRight_Right_sensorvalue==0))
{
TurnLeft();
}
else if((FrontRight_Mid_sensorvalue==1)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==1))
{
TurnRight();
}
else if((FrontRight_Mid_sensorvalue==0)&&(FrontRight_Left_sensorvalue==0)&&(FrontRight_Right_sensorvalue==0))
{
Forward();

}

}

void FrontLeftFollowingLine()
{
if((FrontLeft_Mid_sensorvalue==0)&&(FrontLeft_Left_sensorvalue==1)&&(FrontLeft_Right_sensorvalue==1))
{
Forward();
}

else if(((FrontLeft_Mid_sensorvalue>=0&&FrontLeft_Mid_sensorvalue<=400))&&((FrontLeft_Left_sensorvalue>=0&&FrontLeft_Left_sensorvalue<=400))&&((FrontLeft_Right_sensorvalue>=400&&FrontLeft_Right_sensorvalue<=800)))
{
TurnLeftSlight();
}

else if(((FrontLeft_Mid_sensorvalue>=0&&FrontLeft_Mid_sensorvalue<=400))&&((FrontLeft_Left_sensorvalue>=400&&FrontLeft_Left_sensorvalue<=800))&&((FrontRight_Right_sensorvalue>=0&&FrontLeft_Right_sensorvalue<=400)))
{
TurnRightSlight();
}

else if(((FrontLeft_Mid_sensorvalue>=400&&FrontLeft_Mid_sensorvalue<=800))&&((FrontLeft_Left_sensorvalue>=400&&FrontLeft_Left_sensorvalue<=800))&&((FrontLeft_Right_sensorvalue>=0&&FrontLeft_Right_sensorvalue<=400)))
{
TurnRight();
}
else if(((FrontLeft_Mid_sensorvalue>=400&&FrontLeft_Mid_sensorvalue<=800))&&((FrontLeft_Left_sensorvalue>=0&&FrontLeft_Left_sensorvalue<=400))&&((FrontLeft_Right_sensorvalue>=400&&FrontLeft_Right_sensorvalue<=800)))
{
TurnLeft();
}
else
{
MotorStop();

}

}

void TurnLeft90deg()

{
analogWrite(ENA,110);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENB,0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}

The problem is that the value FrontRight_Mid_sensorvalue never change. To "see it change" you need to call the function LineSensorReading(); inside the while loop. It makes sense to you?

You should look at the DelayMilliseconds() function as well.
Why? (And it's delayMicroseconds()).

Sorry, I meant delay(), which takes an argument in milliseconds. Why did I raise this question? Because delays are common in loops, and the actual code hadn't been posted yet. I thought it was possible that the poster was mistakenly using millis() instead of delay(). As we now know, that is not the case.