Understanding the "while" statement

Hi everyone,

I think I need to use a “while” statement to solve my problem, but I can’t seem to understand how it actually functions. I have read the tutorials, and a few forum posts and still don’t get it… Can someone dumb it down for me lol. (or maybe I don’t need a while statement, but some other syntax?)

For the attached code, I have 2 LEDs connected to Pin2 and pin3 on my Uno, and a potentiometer connected to A0.

In the attached code labeled ProgramA, this works for me with the exception that it is (i think) actually turning the LED on and off extremely quickly, as it is still going through the main loop.

My thought was to use while statements instead of if statements (ProgramB). However this is not working.

Goal is to have both leds low while pot is between 400-500, led high when pot>500 and ledB high when pot <400, but continuous. Is this a possibility?

Thanks everyone in advance.

ProgramA

int led = 2;
int ledB = 3;
int pot = A0;

void setup() {

  pinMode(led, OUTPUT);
  pinMode(ledB, OUTPUT);

}


void loop() {
  
int potVal = analogRead(A0);
digitalWrite(led, LOW);
digitalWrite(ledB, LOW);
 
if (potVal >500)
{
  subProgram1();
}
  
if (potVal <400)
{
   subProgram2();
}
}
  
void subProgram1(){
digitalWrite(led, HIGH);
}

void subProgram2(){
digitalWrite(ledB, HIGH);
}

ProgramB

int led = 2;
int ledB = 3;
int pot = A0;

void setup() {

  pinMode(led, OUTPUT);
  pinMode(ledB, OUTPUT);

}


void loop() {
  
int potVal = analogRead(A0);
digitalWrite(led, LOW);
digitalWrite(ledB, LOW);
 
while (potVal >500)
{
  subProgram1();
}
  
while (potVal <400)
{
   subProgram2();
}
}
  
void subProgram1(){
digitalWrite(led, HIGH);
}

void subProgram2(){
digitalWrite(ledB, HIGH);
}

The while loop will repeat the stuff inside the {} directly after it until the case (potVal > 500) is false. In your case, it would get stuck in the first while loop forever because you dont update the value of potVal within the while statement.

The main loop is all you need for this. Perhaps the [if…else](http://The while loop will repeat the stuff inside the {} directly after it until the case (potVal > 500) is false. In your case, it would get stuck in the first while loop forever because you dont update the value of potVal within the while statement. The main loop is all you need for this. Perhaps the if…else would be more appropriate.) would be more appropriate. Specifically:

if (potVal >500)
{
  subProgram1();
}
else if (potVal <400)
{
   subProgram2();
}
else
{
  digitalWrite(led, LOW);
  digitalWrite(ledB, LOW);
}

But then, what do you do when potVal is between 400 and 500?
Edit: saw that you did handle that case and changed the code with a final “else”. You could whack the LOW writes in the beginning as the “else” statement will handle that.

Does this read correctly?
“Goal is to have both leds low while pot is between 400-500, led high when pot>500 and ledB high when pot <400, but continuous. Is this a possibility?”

You do know you can do stuff like this:

if (potVal >= 400 && potVal <= 500)
  {
    digitalWrite(led, LOW);
    digitalWrite(ledB, LOW);
  }

  else if (ptoVal >500)
  {
    digitalWrite(led, HIGH);
    //etc.
  }

  else if (ptoVal < 400)
  {
    digitalWrite(ledB, HIGH);
    //etc.
  }

Many ways to do it, but I can’t see while would be a good option. Combine larryds approach with your own subProgram structure. To make sure you don’t end up with two lit leds if you turn it really fast or get strange readings, turn off the other led when you lit the other.

int led = 2;
int ledB = 3;
int pot = A0;

void setup() {

  pinMode(led, OUTPUT);
  pinMode(ledB, OUTPUT);

}


void loop() {
  
int potVal = analogRead(A0);
 
if (potVal >500)
{
  subProgram1();
}
  
if (potVal <400)
{
   subProgram2();
}

if (potVal >= 400 && potVal <= 500)
  {
   subProgram3();
  }

}
  
void subProgram1()
{
  digitalWrite(led, HIGH);
  digitalWrite(ledB, LOW);
}

void subProgram2()
{
  digitalWrite(led, LOW);
  digitalWrite(ledB, HIGH);
}

void subProgram3(){
  digitalWrite(led, LOW);
  digitalWrite(ledB, LOW);
}

Thanks everyone for the responses!

Both function great. The If statement is a lot less code to write, but is there a benefit to having sub programs? (if that's what they are called)

mgale31: Thanks everyone for the responses!

Both function great. The If statement is a lot less code to write, but is there a benefit to having sub programs? (if that's what they are called)

They're called functions in C++.

They should have names that self-describe what they do, not "subprogram blah".