How to make an interrupt from Sleep , with accelerometer module .

i need to use sleep mode for arduino ,
and wake up the arduino in case accelerometer moves.
is there any way i can do it,?

i succesfully changed the interupt pin2 into 4 with this code

PCICR |= (1 << PCIE2);
** PCMSK2 |= (1 << PCINT20);**
** PCMSK1 |= (1 << PCINT8);**

i tried to add as interupt the analog input 0 PCMSK1 |= (1 << PCINT8);
but it didnt worked.

i am using Arduino UNO and
MMA7361 Accelerometer Module

#include <avr/sleep.h>

int wakePin =4;                 // pin used for waking up
int ledPin =  13;
int sleepStatus = 0;             // variable to store a request for sleep
int count = 0;                   // counter

void setup()
{
  pinMode(wakePin, INPUT);
  pinMode(A0, INPUT);
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
  
  PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT20);
  PCMSK1 |= (1 << PCINT8);

  
 
}

void wakeUpNow()        // here the interrupt is handled after wakeup
{
   detachInterrupt(0);      // disables interrupt 0 on pin 2 so the 
}


void sleepNow()         // here we put the arduino to sleep
{
    Serial.println("sleepNow");
    attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function
    delay(100);     
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
    sleep_enable();          // enables the sleep bit in the mcucr register
    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

    sleep_disable();         // first thing after waking from sleep:
}

void loop()
{
  // display information about the counter
  digitalWrite(ledPin, HIGH);
  Serial.print("Awake for ");
  Serial.print(count);
  Serial.println("sec");
  count++;
  delay(1000);                           // waits for a second
  digitalWrite(ledPin, LOW);

  // check if it should go to sleep because of time
  if (count >= 5) {
      
      Serial.println("Timer: Entering Sleep mode");
      delay(100);     // this delay is needed, the sleep 
                      //function will provoke a Serial error otherwise!!
      
      count = 0;
      sleepNow();     // sleep function called here
  }
}

You say you have changed the interrupt pin to pin 4 but I'm not sure you can use pin change interrupts to wake from sleep, why change it? You seem to have attached the interrupt function to Pin 2

but lower down you attach the interrupt to pin 2

attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function

Which pin have you physically connected the accelerometer to?

It’s seems you don’t fully understand how to apply interrupts.

PCICR |= (1 << PCIE2);
  PCMSK2 |= (1 << PCINT20);
  PCMSK1 |= (1 << PCINT8);

The first line tells the arduino to enable an interrupt on PCMSK2
Then on the second line you tell the arduino which of the pins on PCMSK2 to listen for changes, in your case D4
This code is good for enabling an pin change interrupt on D4 (however i don’t understand why you want to do that since you state you want an interrupt on the analog pin)
However the third line makes no sense. You tell the arduino to listen for changes on PCMSK1 but you have not first told the arduino to enable an interrupt on PCMSK1.

To enable a pin change interrupt on the analog pin all you need is these two lines:

PCICR |= (1 << PCIE1);
  PCMSK1 |= (1 << PCINT8);

You don’t need the AttachInterrupt code you have later in your sketch. These are used for the external interrupts, and not the pin change interrupts you just set.

but I’m not sure you can use pin change interrupts to wake from sleep

Pin Change Interrupts can indeed be used to wake from all sleep modes. As a matter of fact, It’s even possible to enable a pin change interupt on the serial pins which you can use to wake the arduino from a power down state (SLEEP_MODE_PWR_DOWN)
using serial activity. http://playground.arduino.cc/Learning/arduinoSleepCode has it wrong

i am building an alarm , and i am using , an accelerometer and a RF Wireless Modules 433MHz with a remote in order to activated and deactivated

i want to send arduino into sleep when i hit my remote wich is in port 4 , and Wake up when there is movement, accelerometer is connected through A0 A1 A2 , 3,3V GND

by the way this worked

void setup()
    {
      PCICR |= (1 << PCIE2);
      PCMSK2 |= (1 << PCINT20);
      
      PCICR |= (1 << PCIE1);
      PCMSK1 |= (1 << PCINT8);
}

thanks racquemis

i wonder if there is a way to disengage an interrupt pin

Engaging

PCICR |= (1 << PCIE1);
PCMSK1 |= (1 << PCINT8);

disengaging??

for the record i managed to include all the required analog inputs of accelerometer as interrupt pins

      PCICR |= (1 << PCIE1);
      PCMSK1 |= (1 << PCINT8)| (1<<PCINT9)| (1<<PCINT10);

:smiley:

Glad you got it working.
To disable the interrupt all you have to do is this:

Cli(); 
PCICR = 0x0;
PCMSK2 = 0x0;
PCMSK1 = 0x0;
sei();

Cli() prevents any interrupt from occuring (clears global interrupt flag) while you return PCICR and PCMSK# to their default values.and when your done call sei() to set the global interrupt flag to it's previous state.
You might get away with not using cli and sei(), if your sure no interrupt can occur. But I should just use it, just in case.
Good luck.

thanks again !