Arduino - Progamming issue my first post here.

Anyway I'm working on a Simon Says game.
I have a weird bug with the function that reads button input.

This is the code:

int deBounce(int count){
  int pressed=-1;
  int laastButtons[3]; // save the previous deBounce read.
  for(int x; x&lt;4; x++)
  {
    laastButtons[x] = lastButtons[x];
    lastButtons[x] = !digitalRead(button[x]);
    if(lastButtons[x] == true)
    {
      delay(50);
      lastButtons[x] = !digitalRead(button[x]);
      if(lastButtons[x] == 1){ pressed = x;}
      nt("Pressed!, Button ID: ");nt(x); nt(" pressed:  ");nt(pressed); nt(" state "); ntln(lastButtons[x]);
    }
  }
  if((lastButtons[0] + lastButtons[1] + lastButtons[2] + lastButtons[3]) &gt; 1)
  {
    ntln("ERROR: dont press more than one button at a time. ");
  }
  if( ((lastButtons[0] + lastButtons[1] + lastButtons[2] + lastButtons[3]) == 1 ) &amp;&amp; !laastButtons[pressed] &amp;&amp; (pressed != -1) ) { 
    pressButtons[count] = pressed;
    nt("Pressed!, ID: ");nt(count); nt(" Button ID ");ntln(pressed);
    return count++;
  }else return count;
}


This is what I recieve in the Serial Monitor if I press one of the buttons.


  Pressed!, Button ID: 3 pressed: -1 state 1


How is that possible? Note the lines where that line is printed and the line above it.
Seems like that if should run, but the line pressed = x; doesn't function.
Note that, nt and ntln are just a short for Serial.print.
I have this weird bug and sometimes it seems like the chip is glitching!

Another question is- I tried attaching interrupt and it didn't worked,
Is it possible that my board is not compatiable with them? (It's not original).

Thanks.

EDIT:

After Majenko helped me figure it out,
The thing that created the bug was that I expected a x==0, but I didn't define it in the for loop.

Before:

for(int x; x&lt;4; x++)


After:

for(int x=0; x&lt;4; x++)


Thanks again Majenko!
int laastButtons[3]; // save the previous deBounce read.
for(int x; x&lt;4; x++)
{
   laastButtons[x] = lastButtons[x];


Well, there's a common bug right away.  An array with three slices, numbered 0, 1 and 2, and you're accessing them with a loop that counts from 0 to 3.

Spot the error.

Also, in the for loop, what number does it start counting from?

And now for your "bug".  This block of code:

if(lastButtons[x] == true)
{
  delay(50);
  lastButtons[x] = !digitalRead(button[x]);
  if(lastButtons[x] == 1){ pressed = x;}
  nt("Pressed!, Button ID: ");nt(x); nt(" pressed:  ");nt(pressed); nt(" state "); ntln(lastButtons[x]);
}


I think you have it slightly wrong.  Let's convert it into plain english:


If button is pressed:

Delay 50 ms
Read button again
If button is pressed:

Set pressed to the button number

Print the button message



Note the indenting, which highlights what gets executed for what IF statement.  The print is going to happen regardless of the setting of the pressed variable.

I think the structure you wanted is:


If button is pressed:

Delay 50 ms
Read button again
If button is pressed:

Set pressed to the button number
Print the button message




In that layout the second If is controlling both the setting of the pressed variable as well as whether or not to print the Pressed! message.

if(lastButtons[x] == 1){ pressed = x;}


That line is saying "If last buttons X is 1, then set pressed to X. That is all it is doing. Nothing more, nothing less.