[SOLVED}Key press interrupt

Hi all,
I'm using a shift register to up keep a keyboard with only 3 buttons. (please don't try to find the sense in it, it has none)

I want Arduino to sleep until pis state change is detected.

When no buttons are pressed, button matrix output is LOW, when any button is pressed BUTTON_PIN changes to HIGH. I've tested everything with a voltmeter just to verify it's connected property and that it works as expected.

I'm using Nick Gammon's tutorial for my code.
EDIT: Used the wrong pin change vector in ISR, now code is correct.

ISR (PCINT0_vect)
{
  PCICR = 0;
}

//buttonArray is global up-to-date array of buttons size of BUTTON_COUNT
uint8_t buttonArray[3] = {LOW};
void loop() {
  // put your main code here, to run repeatedly:
  Buttons(true);
  goToSleep();
}

//Define pins
#define BUTTON_PIN 10
#define SERIAL_IN 11
#define SERIAL_CLK 12
#define REGISTER_CLK 13//Latch

  pinMode(BUTTON_PIN, INPUT);
  pinMode(SERIAL_IN, OUTPUT);
  pinMode(REGISTER_CLK, OUTPUT);
  pinMode(SERIAL_CLK, OUTPUT);

//############# Power Menagement #############
void goToSleep ()
{
  // set up to detect a keypress
  digitalWrite(REGISTER_CLK, LOW);
  shiftOut(SERIAL_IN, SERIAL_CLK, LSBFIRST, 7);
  digitalWrite(REGISTER_CLK, HIGH);

  pinMode (BUTTON_PIN, OUTPUT);
  digitalWrite (BUTTON_PIN, LOW); //Redundant, but just to make sure.

  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  byte old_ADCSRA = ADCSRA;
  // disable ADC to save power
  ADCSRA = 0;

  Serial.println("Sleeping");delay(50);  // Just for debugging

  power_all_disable ();  // turn off various modules

  PCMSK0 |= bit (PCINT2); // Select pin 10
  PCIFR  |= bit (PCIF0);   // clear any outstanding interrupts
  PCICR  |= bit (PCIE0);   // enable pin change interrupts

  sleep_cpu ();

  // cancel sleep as a precaution
  sleep_disable();
  power_all_enable ();   // enable modules again
  ADCSRA = old_ADCSRA;   // re-enable ADC conversion

  // put keypad pins back how they are expected to be
  setupButtons();
  
  delay(50);Serial.println("Buttons ready.");   // Just for debugging

}  // end of goToSleep

For reasons I cant understand the above code sends the Arduino sleeping, and never wake up.
Maybe someone would comment on my electronic scheme please?

Does the code work properly if you don't put the Arduino to sleep?

Where is the Buttons() function?

Why do you have pinMode() calls that are not in a function?

Post your REAL code THAT COMPILES!

Gladly, I assumed ppl will only read the code, not run it. But it’s my habits:

#include <avr/sleep.h>
#include <avr/power.h>

#define DEBUG_SERIAL 1
#define DEBUG_BUTTONS_1 0
#define DEBUG_BUTTONS_2 0
#define SERIAL_BAUD 74880

ISR (PCINT2_vect)
{
  PCICR = 0;
}  // end of PCINT2_vect

void setup() {
  // put your setup code here, to run once:
  setupSerial();
  setupButtons();
}

//buttonArray is global up-to-date array of buttons size of BUTTON_COUNT
uint8_t buttonArray[3] = {LOW};
void loop() {
  // put your main code here, to run repeatedly:
  Buttons(true);
  goToSleep();
}

//############# Serail #############
void setupSerial() {
  Serial.begin(SERIAL_BAUD);
#if DEBUG_SERIAL
  Serial.println("Serial started at " + String(SERIAL_BAUD) + "; Buffer size is " + String(Serial.getRxBufferSize()) + ".");
#endif
}

//############# Buttons #############
//Define pins
#define SERIAL_IN 11
#define SERIAL_CLK 12
#define REGISTER_CLK 13//Latch

#define BUTTON_PIN 10

void setupButtons() {
  pinMode(BUTTON_PIN, INPUT);
  pinMode(SERIAL_IN, OUTPUT);
  pinMode(REGISTER_CLK, OUTPUT);
  pinMode(SERIAL_CLK, OUTPUT);
}

int bitArrayToInt(uint8_t arr[])
{
  int ret = 0;
  for (int i = 0; i < 7; i++)
    ret |= arr[i] << (6 - i);
  return ret;
}

int find2Pow(int x) {
  if (x == 1)
    return 2;
  return  2 * find2Pow(x - 1);
}

//ButtonArray size defines number of buttons - defined in Loop()
//uint8_t buttonArray[3];
#define BUTTON_COUNT sizeof(buttonArray)
//Debounce prametter
#define DEBOUNCE 40
#define counter find2Pow(BUTTON_COUNT)-1

void Buttons(bool isSerialTransmit) {
  uint8_t permutaitonState[counter] = {LOW};
  uint8_t ps;
  static uint8_t lastPS = 0;

  for (uint8_t i = 1; i <= counter; i++) {
    digitalWrite(REGISTER_CLK, LOW);
    shiftOut(SERIAL_IN, SERIAL_CLK, LSBFIRST, i);
    digitalWrite(REGISTER_CLK, HIGH);

    permutaitonState[i - 1] = digitalRead(BUTTON_PIN);
    //Serial.print(analogRead(A0) * (5.0 / 1023.0));
    //Serial.print(permutaitonState[i - 1]);
    //Serial.print(",");
  }
  //Serial.println();
  delay(DEBOUNCE);

  ps = bitArrayToInt(permutaitonState);
  if (ps == lastPS)
    return;
  switch (ps) {
    case 85:
      buttonArray[0] = HIGH; buttonArray[1] = LOW; buttonArray[2] = LOW;
      break;
    case 51:
      buttonArray[1] = HIGH; buttonArray[0] = LOW; buttonArray[2] = LOW;
      break;
    case 15:
      buttonArray[2] = HIGH; buttonArray[1] = LOW; buttonArray[0] = LOW;
      break;
    case 17:
      buttonArray[0] = buttonArray[1] = HIGH; buttonArray[2] = LOW;
      break;
    case 5:
      buttonArray[0] = buttonArray[2] = HIGH; buttonArray[1] = LOW;
      break;
    case 3:
      buttonArray[1] = buttonArray[2] = HIGH; buttonArray[0] = LOW;
      break;
    case 23:
      buttonArray[0] = buttonArray[1] = buttonArray[2] = HIGH;
      break;
    case 0:
      buttonArray[0] = buttonArray[1] = buttonArray[2] = LOW;
      break;
    default:
      return;
  }

  lastPS = ps;

  if (isSerialTransmit) {
    String msg = "<KPS:";
    for (int i = 0; i < BUTTON_COUNT; i++)
      msg += (buttonArray[i] == HIGH) ? "1#" : "0#";

    msg[msg.length() - 1] = '>';
    Serial.println(msg);
  }

#if DEBUG_BUTTONS_1
  for (int i = 0; i < sizeof(permutaitonState) / sizeof(permutaitonState[0]); i++) {
    Serial.print(permutaitonState[i]);
    Serial.print(",");
  }
  Serial.println();
#endif
#if DEBUG_BUTTONS_2
  for (int i = 0; i < BUTTON_COUNT; i++) {
    Serial.print(buttonArray[i]);
    Serial.print(",");
  }
  Serial.println();
#endif
}

//############# Power Menagement #############
void goToSleep ()
{
  // set up to detect a keypress
  digitalWrite(REGISTER_CLK, LOW);
  shiftOut(SERIAL_IN, SERIAL_CLK, LSBFIRST, 7);
  digitalWrite(REGISTER_CLK, HIGH);

  pinMode(BUTTON_PIN, OUTPUT);
  digitalWrite(BUTTON_PIN, HIGH);

  // now check no pins pressed (otherwise we wake on a key release)

  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  byte old_ADCSRA = ADCSRA;
  // disable ADC to save power
  ADCSRA = 0;
  Serial.println("Sleeping");delay(50);
  power_all_disable ();  // turn off various modules

  PCMSK0 |= bit (PCINT2); // Select pin 10
  PCIFR  |= bit (PCIF0);   // clear any outstanding interrupts
  PCICR  |= bit (PCIE0);   // enable pin change interrupts

  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);

  sleep_cpu ();

  // cancel sleep as a precaution
  sleep_disable();
  power_all_enable ();   // enable modules again
  ADCSRA = old_ADCSRA;   // re-enable ADC conversion

  // put keypad pins back how they are expected to be
  setupButtons();
  delay(50);
  Serial.println("Buttons ready.");

}  // end of goToSleep