Maker Pro
Maker Pro

Arduino code help - while statements

Robert Hill

Mar 5, 2015
112
Joined
Mar 5, 2015
Messages
112
Hi guys,

I'm looking at Arduino code and learning about While statements. I've got the following code:

const int kPinLed = 13;
void setup()
{
pinMode(kPinLed, OUTPUT);
}

int delayTime = 1000;
void loop()
{
while(delayTime > 0){ // while delayTime is greater than 0
digitalWrite(kPinLed, HIGH);
delay(delayTime);
digitalWrite(kPinLed, LOW);
delay(delayTime);
delayTime = delayTime - 100;
}
while(delayTime < 1000){
delayTime = delayTime + 100; // do this first so we don!!
digitalWrite(kPinLed, HIGH);
delay(delayTime);
digitalWrite(kPinLed, LOW);
delay(delayTime); }
}

I understand it's function is to flash the LED faster then go back to flashing slower. However I don't understand why it works. It seems to me that the programme would work in the following way.

1.The delay time is 1000 so the first while statement executes because the time is greater than 0. so the light goes on and off then the delay time is reduced to 900.
2. Now both while statements will execute because the time is both above 0 and below 1000. This means the LED will flash on and off and the time be reduced to 800, then the time will be returned to 900 by the second while statement before the LED flashes at a delay of 900. This process will repeat over and over which as far as I can see means the led will always flash at 900 time delay.

However, i'm sitting here with an LED that flashes slower then faster then Slower so i've obviously got it wrong. anyone able to lay out the steps to show how the timer gets reduced?

Thanks.
 

Robert Hill

Mar 5, 2015
112
Joined
Mar 5, 2015
Messages
112
Ah, think i've just worked it out.

1. The delay is 1000 so the first while statement triggers. This flashes the LED then reduces the delay to 900. Rather than checking the second while statement the program returns to the start of the first while statement to check if it is still true. It repeats and reduces the delay to 800 and so on until it reaches 0. At this point the first while statement is no longer true so stops running. Then the second while statement is checked and found to be true so 100 is added and the LED flashes. This second while statement repeats until it reaches 1000 and turns itself off. This then lets the first while statement take over again.

So in a nutshell only one while statement can run at a time and the program will 'get stuck' on that statement until it becomes no longer true.
Have I cracked it?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Hi guys,

I'm looking at Arduino code and learning about While statements. I've got the following code:

const int kPinLed = 13;
void setup()
{
pinMode(kPinLed, OUTPUT);
}

int delayTime = 1000;
void loop()
{
while(delayTime > 0){ // while delayTime is greater than 0
digitalWrite(kPinLed, HIGH);
delay(delayTime);
digitalWrite(kPinLed, LOW);
delay(delayTime);
delayTime = delayTime - 100;
}
while(delayTime < 1000){
delayTime = delayTime + 100; // do this first so we don!!
digitalWrite(kPinLed, HIGH);
delay(delayTime);
digitalWrite(kPinLed, LOW);
delay(delayTime); }
}

I understand it's function is to flash the LED faster then go back to flashing slower. However I don't understand why it works. It seems to me that the programme would work in the following way.

1.The delay time is 1000 so the first while statement executes because the time is greater than 0. so the light goes on and off then the delay time is reduced to 900.
2. Now both while statements will execute because the time is both above 0 and below 1000. This means the LED will flash on and off and the time be reduced to 800, then the time will be returned to 900 by the second while statement before the LED flashes at a delay of 900. This process will repeat over and over which as far as I can see means the led will always flash at 900 time delay.

However, i'm sitting here with an LED that flashes slower then faster then Slower so i've obviously got it wrong. anyone able to lay out the steps to show how the timer gets reduced?

Thanks.
This program is very linear... It does not jump or branch out... so the first while loop will 'hook' and the program will ONLY do what is in that first while loop until delayTime is no longer greater than 0. Once the first while loop 'no longer' applied, the program will continue and check the second while loop which would be true. I would then get caught in that while loop and run ONLY the stuff in the second While Loop until delayTime is larger than 1000.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Ah, think i've just worked it out.

1. The delay is 1000 so the first while statement triggers. This flashes the LED then reduces the delay to 900. Rather than checking the second while statement the program returns to the start of the first while statement to check if it is still true. It repeats and reduces the delay to 800 and so on until it reaches 0. At this point the first while statement is no longer true so stops running. Then the second while statement is checked and found to be true so 100 is added and the LED flashes. This second while statement repeats until it reaches 1000 and turns itself off. This then lets the first while statement take over again.

So in a nutshell only one while statement can run at a time and the program will 'get stuck' on that statement until it becomes no longer true.
Have I cracked it?
You figured that out before I hit 'post reply' :p
In a sense you are right. Only one of these while loops will run at a time. Considering them to catch the program. As long as the condition for a While is true, the program will never leave that while loop.
*There are special commands that will force you to kick out of this loop.


That said. You can also 'nest' loops inside each other...
Code:
count = 0
While x < 16{
   While y < 16{
      count = count + 1
      y = y + 1
   }
   count = count + 1
   x = x + 1
}

count will equal 256 at the end of this little sample.
The x While will run 16 times, and each time the y While 'inside' will run 16 times.
So they can be 'nested' inside each other.
 

Robert Hill

Mar 5, 2015
112
Joined
Mar 5, 2015
Messages
112
Thanks for your help. It was funny how I posted up then worked it out, but it's always good to confirm you've got it right.
 
Top