Interrupt problems

Hi,

I am having some strange problems with my interrupt I am trying to get to work. I have attached a 4X4 keypad that wakes the arduino when it is aslepp by pressing a button. So fine, everything is working and the keypress puts my project into "Programming" mode.

The strange thing is that when the arduino wakes up, it seems like the interrupt gets triggered? The result is that it is being forced into my "programming" mode again?

Any suggestions? This is the first time I am trying to solve this so please be patient :slight_smile:

#include <LowPower.h>


#include <Keypad.h>

// ---------------------------------
// KEPAD DEFINITIONS AND VARIABLES
// ---------------------------------

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};


byte rowPins[ROWS] = {10, 9, 8 , 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// ---------------------------------
//VARIABLES
// ---------------------------------

  //Programming variables
  boolean enterProgramming = false;
  boolean isWaked = false;
  char key;
  
  //Interupt / sleep
  int wakePin = 2; 
  
void setup()
{
  Serial.begin(115200);
  Serial.println("Starting up....");
  delay(1000);
  pinMode(wakePin, INPUT);
}  
void loop()
{  
 
   
   //First check if we are getting a keystroke to enter programming mode
   key = keypad.getKey();
   delay(100);
    
   String Temp;
  
 
  // Arduino is been waked up by the key press - now enter programming mode!
  if(isWaked==true){
         enterProgramming = true;
         isWaked = false;
         Serial.println("");
         Serial.println("Entering programming mode");
         delay(500);
  }
  
  if (enterProgramming==true){
    
   
    //We are in programming mode
    
    
    if(key == 'A'){ //Jumps out of programming mode and goes to "work" mode.
  
            enterProgramming=false;     
            Serial.println("");
            Serial.println("Exiting programming mode");
          }
        else
          {
           Serial.println(key);
          }
      }else{
      //enterProgramming = false;
      }
      
      
 
  
  
  // Main loop when we are not in programming mode - just our work mode.
  
  if(enterProgramming==false){
    
   delay(500);
   Serial.println("Running mode"); 
   attachInterrupt(0,wakeUpNow, HIGH);
   
   delay(100);
   Serial.println("Going to sleep...");
   delay(100);
   // Kör sleep koden
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
   delay(100);
   Serial.println("Sleep is over...");
   delay(200);
   detachInterrupt(0); 
    
  }
}

void wakeUpNow() {
  isWaked = true;
}

"iswaked" should be declared volatile. All var's changed in an interrupt routine should be declared as volatile. But that's not your problem.

I think you button/switch is bouncing look at de-bouncing in the playground.

Mark

What if you swap these 2 lines?

delay(200);
detachInterrupt(0);

Then you can't be interrupted during the delay, which should debounce the switch also.

Well, the problem is not the "bounce". The problem is that something else is triggering the interrupt when the board wakes up. The interrupt done by a key press is just working fine. Problem is that something happens when he/she wakes up?

I guess that it has something to do with my signals. I was looking at another thread where there was a keypad and some diode's - so I am trying that approach but somehow I am reading somewhere around 0.3 -> 0.4 and I am not sure if that is equal to low? Shouldn't low be equal to Zero? I am maybe using wrong diodes, should maybe try 1n4148 instead?

Joakim

LOW is anything below 0.3*Vcc, so for 5V that's <1.5V.

Well, the problem is not the "bounce".

How do you know this. Are you going to post you're latest code? Where is your circuit diagram?

Mark