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?