problem with analogRead()

I have a strange problem with analogRead(), it seems to interfere with loops in a sketch:
if the analogRead() function is activated (now not active, see below) the if-loop behaves like a while loop!!!!
Any suggestions are welcome!

/*
Arduino sketch 
problem with if-loop and analogRead(), inside while loop : if-loop behaves like while loop.
*/

#define statusLED 13  // the statusLED on the Arduino board
#define pushBUTTON 7  // pinnumber of the switch.

int solarcellPin = 3; // solarcell on pin A3
int solarvalue;

void setup() 
{
  pinMode(statusLED, OUTPUT);
  pinMode(pushBUTTON, INPUT);
  digitalWrite(pushBUTTON, HIGH);
}

void loop() 
{
 
  if (digitalRead(pushBUTTON) == LOW) { // pushbutton activated
  
    while (digitalRead(pushBUTTON) == LOW) {} // wait until the button has been released
    delay (100); // delay for contact rest
    
    digitalWrite(statusLED, HIGH); // StatusLED goes HIGH because pushbutton was activated
    
    while (digitalRead(pushBUTTON) == HIGH){  // do something until pushbutton is pressed again
//      solarvalue = analogRead(solarcellPin);
      if (1 == 1){
        digitalWrite(statusLED, HIGH); // here some function can be used
        delay (100);
        digitalWrite(statusLED, LOW);
        delay (100);
      }
    }
    digitalWrite(statusLED, LOW);  // exit while cycle
    while (digitalRead(pushBUTTON) == LOW) {} // wait until the button has been released
    delay (100); // delay for contact rest
  }
}

Please modify the post. Select the text and hit the # icon.

if the analogRead() function is activated (now not active, see below) the if-loop behaves like a while loop!

Can you be a bit more specific, what do you see?
Because there is no such thing as an if loop.

Could it be that the time it takes to do the analogue read is actually putting some debounce delay in your program?

What does this line of code do exactly?

if (1 == 1){

Sorry to be unclear and indeed the IF statement does not make sense. The problem is with the WHILE loop:

while (digitalRead(pushBUTTON) == HIGH){  // do something until pushbutton is pressed again - here the statusLED is flashing.
  solarvalue = analogRead(solarcellPin);  // solarcell connected to pin A3
  digitalWrite(statusLED, HIGH);
  delay (100);
  digitalWrite(statusLED, LOW);
  delay (100);
}

When an analogRead() is done within the WHILE loop, the loop always seems to keep running although it should end when the pushBUTTON is pressed.
When the analogREAD() is taken out, the WHILE loop acts as it should and the pushBUTTON has the expected effect (end flashing statusLED)
I am using an UNO.
It does not make sense to me, does it to anyone else?

Still not using the #icon, last warning after that you will be ignored by me.

// do something until pushbutton is pressed again - here the statusLED is flashing.
Is not a correct statement, this will continue as long as the button is being held down not until it is pressed again. Therefore your statement:-

When an analogRead() is done within the WHILE loop, the loop always seems to keep running although it should end when the pushBUTTON is pressed.

Cannot be correct.
What you are describing with that code fragment simply cannot happen.

So in order to find out what you are actually doing wrong can you describe exactly how you have got your push buttons and what input you have analogue 3, also what you measure on the analogue 3 input with your meter.

Grumpy_Mike:
Still not using the #icon, last warning after that you will be ignored by me.

So in order to find out what you are actually doing wrong can you describe exactly how you have got your push buttons and what input you have analogue 3, also what you measure on the analogue 3 input with your meter.

Sorry about not knowing exactly what you meant by # icon, hopefully this is what you mean.

Anyway, I have a pusbutton between ground and pin 7 and a solarcell between ground and A3.
I have reduced the code that I am using to show the origin of my problem: I would expect that the code in the WHILE-loop runs if the condition regarding the pushBUTTON is met.
Indeed all is correct if no analogREAD is included in this code. However, if the analogREAD is carried out, the WHILE loop seems to keep seeing the pushBUTTON in a high state after running the loop once.
Reduced Sketch:

/*
Arduino UNO sketch 
problem with WHILE-loop when analogRead() is inside the loop : No reaction on pushBUTTON
*/

#define statusLED 13  // the statusLED on the Arduino board
#define pushBUTTON 7  // pinnumber of the pushbutton
int solarcellPin = 3;
int solarvalue;

void setup() 
{
  pinMode(statusLED, OUTPUT);
  pinMode(pushBUTTON, INPUT);
  digitalWrite(pushBUTTON, HIGH);
}

void loop() 
{
    while (digitalRead(pushBUTTON) == LOW){  // do something only when pushbutton is pressed
        solarvalue = analogRead(solarcellPin);
        digitalWrite(statusLED, HIGH); // here some function can be used
        delay (100);
        digitalWrite(statusLED, LOW);
        delay (100);
    }
}

Highlight your code like this, using the "#" button, as requested twice above:

while (digitalRead(pushBUTTON) == HIGH){  // do something until pushbutton is pressed again - here the statusLED is flashing.
  solarvalue = analogRead(solarcellPin);  // solarcell connected to pin A3
  digitalWrite(statusLED, HIGH);
  delay (100);
  digitalWrite(statusLED, LOW);
  delay (100);
}

analogRead takes 100 uS to run, plus you have built in 2 x 100 mS delays. So you are only testing the button every 0.2001 seconds. Plus Grumpy_Mike is right. you are not testing for it to be "pressed again".

gert3d:
Anyway, I have a pusbutton between ground and pin 7 and a solarcell between ground and A3.

What voltage do you measure on the solar cell (that is, A3)?

OK, well the symptoms you are describing are not what is expected so there must be something else going wrong. Try disconnecting your solar cell and see if the same thing happens. If could be it is providing too much voltage an upseting your processor.

Grumpy_Mike:
... could be it is providing too much voltage an upseting your processor.

cooking it

Thanks,
The solar cell only puts out 3 volts max. on my desk, anyway when disconnected the phenomenon is still there.
I now see that even the analogREAD() outside the WHILE loop also disables the loop, but the function works fine: the statusLED is ON when the solarcell is in the light, and OFF when covered.
However, I cannot stay in the WHILE loop by holding the pushBUTTON low (No problem if no analogREAD is done!).

/*
Arduino UNO sketch 
problem with WHILE-loop when analogRead() is inside the loop : No reaction on pushBUTTON
*/

#define statusLED 13  // the statusLED on the Arduino board
#define pushBUTTON 7  // pinnumber of the pushbutton
int solarcellPin = 1;
int solarvalue;

void setup() 
{
  pinMode(statusLED, OUTPUT);
  pinMode(pushBUTTON, INPUT);
  digitalWrite(pushBUTTON, HIGH);
}

void loop() 
{
        solarvalue = analogRead(solarcellPin);  
        if (solarvalue > 100){
          digitalWrite(statusLED, HIGH);
        }
        else{
        digitalWrite(statusLED, LOW);
        }
    while (digitalRead(pushBUTTON) == LOW){  // do something only when pushbutton is pressed
//        solarvalue = analogRead(solarcellPin);
        digitalWrite(statusLED, HIGH); // flash the LED
        delay (100);
        digitalWrite(statusLED, LOW);
        delay (100);
    }
}

I now see that even the analogREAD() outside the WHILE loop also disables the loop,

This is pointing to either a faulty processor chip in the arduino or something wrong with the instillation of the arduino software.

Thanks, I'll try and find another board / processor first.

On my Uno, with the second analogRead there, the LED still flashes. However you say that doesn't happen for you?

Sorry to get into this "conversation"...@gert3d , I read your code and I try to figure out the problem, assuming you are using a push-button type ( Press = ON = True = 1 ) and ( No Press = OFF = False = 0 ) And you want to stay in the loop while the button is "No Press" , right ? you press , you get out, right ?

Let me re-write the code for you : I use an exta variable call pushing and check for a 1 or 0 in the loop, to take into account the de-bounce of the switch ( switches are not a clean signal , you know ) and check the push button for a press by putting a 1 in the variable pushing so you can get out of the while() loop. But I did look at the code again, I am afraid the Led will always "flash", even you press the push button, it will get out the while-loop, going to the begining of the void loop(), execute and check the analog read , light the led ( on or off ) and going back to the while loop again, press to get out, .......

Here the modify code. But it will still "look" it is flashing...

/*
Arduino UNO sketch 
problem with WHILE-loop when analogRead()
is inside the loop : No reaction on pushBUTTON
*/

#define statusLED 13  // the statusLED on the Arduino board
#define pushBUTTON 7  // pinnumber of the pushbutton
int solarcellPin = 1;
int solarvalue;
int pushing;

void setup() 
{
  pinMode(statusLED, OUTPUT);
  pinMode(pushBUTTON, INPUT);
  digitalWrite(pushBUTTON, HIGH);
}

void loop() 
{
        solarvalue = analogRead(solarcellPin);  
        if (solarvalue > 100){
          digitalWrite(statusLED, HIGH);
        }
        else{
        digitalWrite(statusLED, LOW);
        }
    pushing=digitalRead(pushButton);
    delay(10); // debounce the pushbutton    
    while (pushing==0)
    {  // do something only when pushbutton is pressed
        solarvalue = analogRead(solarcellPin);
        digitalWrite(statusLED, HIGH); // flash the LED
        delay (100);
        digitalWrite(statusLED, LOW);
        delay (100);
        pushing=digitalRead(pushButton);
        delay(10);
    }
}

Since you are inside loop() you could actualy change the while to an if(). loop() will continually run itself and so is really an infinite while

void loop()
{
   if (digitalRead(pushBUTTON) == HIGH){  // if pushbutton is pressed flash the led
      solarvalue = analogRead(solarcellPin);  // solarcell connected to pin A3
      digitalWrite(statusLED, HIGH);
      delay (500);
      digitalWrite(statusLED, LOW);
      delay (500);
    }
}

I suggest this because sometimes a slightly different view or approach can be illuminating...

@ offransen, thanks for the suggestion to work around the problem.

@ Techone, thanks for the code, that did the trick!
Apparently it is not correct to do the digitalRead() directly in the WHILE condition. at least not in combination with analogueRead().

So problem SOLVED, I'll get on with my TimeLapse project.