While or if?

I’ve got a quick question.

I’ve built an object following robot.
Now I want make a self charging object following robot.

I’ve a Ampere hour meter connected to the battery. Which measures the Ah left in the battery.

Now I want the following code:

Aslong as the Ah of the battery > 240 mAh de robot will follow objects.
Once the Ah of the battery goes below 240 I want to start the charging phase.

Should I do it like this:

int a = analogRead(A0)

while (a > 240) {
	//follow objects

while (a <240) {
       //charge phase

Or should I use the IF statement.

Another quick question is: can I use else with while?


Or should I use the IF statement.

Which one makes the most sense to you? Will the actions in the body be blocking or not?

Ask 10 programmers how to do something, and you'll get 12 ways to do it.

I would like to use the While function. Am i able to use two while functions in the void loop?

Am i able to use two while functions in the void loop?

I am. Whether you are, or aren't, is not something I can answer.

I should rephrase my question.

Is it possible to use two while loops in the void loop?
Or do I need to do something like this?

void loop {
	int a = analogRead(A0)

	while (a > 240 || a<240) {
		if (a>240){
		if (a<240){


How many times will your while loop, as you’ve written it, loop?

It wil loop continuously until the Ampere hour of the battery is lower then 240 mAh

Have another go.

I don't quite understand it anymore.

I thought a while loop was looping continuously until an expression becomes false. I my case the expression is that a needs to be bigger then 240. Until a is smaller then 240 the while loop will loop.

Or am I completely not understanding the while loop?

I thought a while loop was looping continuously until an expression becomes false.

You are absolutely correct in that belief. Look at your code again and ask yourself "after that while loop has started, what makes it stop? What makes that condition false?"

I think the problem is that I used OR in the while loop
while ( a>240 || a<240)
So it stops right at the beginning and doesn’t loop?
if this is the case, how would I fix this?

OK, imagine your test is just while (a > 240) {

How many times does the while loop loop, if a is 241?

It loops infinitely because the expression is true right?? So sorry for me being stupid

You're not stupid - you got there, didn't you?

There is actually two problems with that while() loop.

  1. The OR expression is always true, unless a is EXACTLY 240

  2. You never update the value of a inside the loop, so whenever it gets into the loop, it will never leave.

The second one is actually the more serious and hard-to-find error. You would have found the first one pretty quickly.

Oooh I see, I should've definitely seen the first problem!

But I still don't quite know how to write the code... Do I need to write something like I've done the first time?

So I now have written this code

int a = analogRead(A0)

while (a > 240) {
         a = analogRead(A0)
    //follow objects
} else {

Am i going into the right direction?

No, because while does not have an else.

So how would I do the else part instead?

By making it an "if", and not a "while" (because loop(), you know, loops)