if vs while problem

Hello everyone. I have a quite simple program and i can not figure out where's the problem. The problem is as follows: I have 2 conditions a if condition and a while condition. What i want to do is to increment the variable i as long as the button is not pressed. If the button is pressed the program should be looping through the while until i release the button, but it doesn't. I is still incrementing even if the button is pushed. Check the last part of the code. Thanks!

//Pin connected to ST_CP of 74HC595
int latchPin = 9;
//Pin connected to SH_CP of 74HC595
int clockPin = 10;
////Pin connected to DS of 74HC595
int dataPin = 8;
int button = 11;
byte vec[10]={253,112,238,250,115,187,191,241,255,251};
boolean previousButton = 0;
int interval = 200;
int debounceinterval=5;
unsigned long previousMillis = 0;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(button,INPUT);
  Serial.begin(9600);
}

void loop() 
{
   int static i=0;
   if(millis() - previousMillis >= interval)
   {
     if(i>9) i=0;
     digitalWrite(latchPin, LOW);
     shiftOut(dataPin, clockPin, LSBFIRST, ~vec[i]);
     digitalWrite(latchPin,HIGH);
     i++;
     previousMillis += interval;      
   }
   while(digitalRead(button) == HIGH)
   {
      shiftOut(dataPin, clockPin, LSBFIRST, ~vec[i]);
//HERE THE I IS STUCK AT ONE NUMBER, LET'S SAY 6.
   }
// HERE, WHEN I RELEASE THE BUTTON, I CAN SEE ANY NUMBER FROM 0-9... WHY IS NOT GOING FROM 6 ON?
   Serial.println(i);
}

//HERE THE I IS STUCK AT ONE NUMBER, LET'S SAY 6.

Yes it will because you never increment the variable i in this loop.

How do you have your button wired? You should have a pulldown resistor.

Of course i have i pulldown resistor, the problem is in the code... what's wrong in it? why i keeps iterating while i'm in the while loop? =(

alexmath: Of course i have i pulldown resistor, the problem is in the code... what's wrong in it? why i keeps iterating while i'm in the while loop? =(

int static i=0;

or

static int i=0;

If a stranger who knows Arduino programming were to read through your code once do you think he would know what it is trying to do and whether it is correct?

Write your code for that stranger and you will be surprised how much it helps you also.

...R

Let me try again:-

while(digitalRead(button) == HIGH)
   {
      shiftOut(dataPin, clockPin, LSBFIRST, ~vec[i]);
//HERE THE I IS STUCK AT ONE NUMBER, LET'S SAY 6.
   }

What makes you think that the variable 'i' will change when you are in that while loop? As long as the button is held down you keep sending the exact same stuff out to the shift register. The variable 'i' is never changed.

That is what you have written so why are you surprised when it happens?

Grumpy_Mike:
That is what you have written so why are you surprised when it happens?

I believe OP is saying… " why did it exit the while loop and start counting (seen as it is printing serially) from a number different than when it entered the while loop?"

he expects to see:

1
2
3
...while
...while
...while
4
5
6
7

he sees unexpected values when he exits…

perhaps related to the way he declares the integer i.