# For loop skipping numbers?

I modified a code I found online that uses one analog pin to read which button is pressed in a matrix. I have a for loop that’s counting by increments of 1, but the loop seems to do the first few things in the loop, then add one to the counter, start back and the beginning and go all the way through. This is what I got in the Serial Monitor:
Begin pressing keys
i=0
i=1
4i=2

Here, I pressed button 4 after “i=0” appeared, then the rest printed.

``````int buttonPin = 0;   //Analog pin A0
int buttonValue = 0; //Voltage at pin A0
int button[4];       //Stores order of buttons pressed
int key=0;           //Holds the most recently pressed key

void setup() {
Serial.begin(9600);
Serial.println("Begin pressing keys");
}

void loop() {
for (int i=0; i < 4; i=i+1){
Serial.print("i=");
Serial.println(i);
Key();         //Determines which key is pressed; Serial prints key
button[i]=key; //Store a 1x4 array of what keys were pressed
}
Serial.println();
Serial.println("Exited Loop"); //Check to see when the loop is exited.

for (int k=0; k < 4; k=k+1){
Serial.print(button[k]);     //Print the storred order of keys pressed.
}
Serial.println();
}

//Function to get which key is pressed and
//print out pressed key to Serial Monitor
void Key() {
while (analogRead(0)==0){} //Wait till a button is pressed
if (buttonValue > 720) { //Determines which button is pressed
Serial.print('1');
key=1;
while (buttonValue > 720) { //Do nothing while button is held
}
}
else if (buttonValue > 520) {
Serial.print('2');
key=2;
while (buttonValue > 520) {
}
}
else if (buttonValue > 320) {
Serial.print('3');
key=3;
while (buttonValue > 320) {
}
}
else if (buttonValue > 120) {
Serial.print('4');
key=4;
while (buttonValue > 120) {
}
}
delay(25);
}
``````

I tried putting the whole function in the loop without calling it, but nothing changed. Is there an error I made or something that may fix the problem? Thanks in advance.

Also, I read the forum rules but if there’s anything I messed up please let me know.

I don't like:

``````  buttonValue = analogRead(buttonPin);
while (analogRead(0)==0){} //Wait till a button is pressed
``````

Why not:

``````  do {
} while (buttonValue==0); //Wait till a button is pressed
``````

?

If you do it your way you will never see the change in `buttonValue` variable, so, when you go to the `if` sequence `buttonValue` will have the value that hat when the function is called.

And just in case any electrical noise causes analogRead to return small positive values even with no button pressed, maybe make the comparison:

``````} while (buttonValue < 90); // Choose a value that is well below the lowest threshold for a button
``````

I'm not sure why anyone would use analogread for a button.

Also buttons should be debounced

rogerClark: I'm not sure why anyone would use analogread for a button.

Also buttons should be debounced

Well, I'm doing the same (I'm using a analog input for buttons). I can't speak for others, but in my case, I'm doing it this way because the shield that I buy implements the keypad this way. I believe that they do it like this, because they have a LCD in the same shield and this is parallel, they have 5 buttons and if the buttons are implemented with digital inputs, they will left to the user only 8 inputs. This way they save 4 inputs, and the user keep 12 inputs for other uses.

rogerClark: I'm not sure why anyone would use analogread for a button.

Also buttons should be debounced

By using an analog input I can use 1 pin for a matrix keypad. Each button is run through a different overall resistor value, allowing you to determine which was pressed.

luisilva: I don't like:

``````  buttonValue = analogRead(buttonPin);
while (analogRead(0)==0){} //Wait till a button is pressed
``````

Why not:

``````  do {
} while (buttonValue==0); //Wait till a button is pressed
``````

?

If you do it your way you will never see the change in `buttonValue` variable, so, when you go to the `if` sequence `buttonValue` will have the value that hat when the function is called.

I tried this and it didn't fix the problem. After I pressed the button once the loop just kept running through and printing the "i" values. Is there anything that would cause the loop to only go partially through, do i++, then finish the loop and repeat that process?

What you really want to do? Can you please explain that?
I don’t understand very well what you are trying to do by using the 2 `for` cycles in the `loop()`. Maybe is that the problem.

``````  for (int i=0; i < 4; i=i+1){
Serial.print("i=");
Serial.println(i);
Key();         //Determines which key is pressed; Serial prints key
button[i]=key; //Store a 1x4 array of what keys were pressed
}
(...)
``````

This first `for` what do is read 4 keys print the value of the counter “`i`” to the serial line and store de value of the key in the `button` array.

``````(...)
Serial.println();
Serial.println("Exited Loop"); //Check to see when the loop is exited.
(...)
``````

This 2 lines I believe that don’t need explanation.

``````(...)
for (int k=0; k < 4; k=k+1){
Serial.print(button[k]);     //Print the storred order of keys pressed.
}
Serial.println();
``````

This `for` loop writes for the serial line the 4 keys stored before.

EDIT:
If the `Key()` function works like expected, the code will print to the serial line “i=0”, wait until one key is pressed, write the value of the key to the serial line, wait until the key is released, store the value of the key pressed (repeat this 4 times). Then write the 4 keys to the serial line.
What part of this the program does in a wrong way?

Hackscribble: Any difference if you add the change suggested in reply #2?

No, though I did add it. I hadn't had the problem of values jumping over 0 if no button was pushed, but it should be there.

luisilva: What you really want to do? Can you please explain that? I don't understand very well what you are trying to do by using the 2 `for` cycles in the `loop()`. Maybe is that the problem.

I am only using this program to test a 1x4 button matrix I made. I'm using it to help learn a bit for another project I'm working on (robotic bartender). I used to 2 for loops to see if I could simply store the values of which buttons were pressed (the first one to assign values to the array, the second to print them).

If the Key() function works like expected, the code will print to the serial line "i=0", wait until one key is pressed, write the value of the key to the serial line, wait until the key is released, store the value of the key pressed (repeat this 4 times). Then write the 4 keys to the serial line. What part of this the program does in a wrong way?

That is what is supposed to happen. However what happens is "i=0" is printed, then I push a button, then "i=1" is printed, then the button I pressed is printed. I explained what happens in more detail in my first post, and the Serial Monitor results are shown as well.

And something like this, will solve the problem?

``````void Key() {
int buttonDeb;
do {
do {   // cycle for debonce
delay(25);
} while (buttonValue-buttonDeb < 5);

if (buttonValue > 720) { //Determines which button is pressed
Serial.print('1');
key=1;
while (buttonValue > 720) { //Do nothing while button is held
}
}
else if (buttonValue > 520) {
Serial.print('2');
key=2;
while (buttonValue > 520) {
}
}
else if (buttonValue > 320) {
Serial.print('3');
key=3;
while (buttonValue > 320) {
}
}
else if (buttonValue > 120) {
Serial.print('4');
key=4;
while (buttonValue > 120) {