Groove, sorry I had to post the code in two parts as I got an error that I'm exceeding the number of characters allowed (9000)
//attachInterrupt(digitalPinToInterrupt(interruptPin), ADXL_ISR, RISING); // Attach Interrupt
//void setup()
pinMode(wakePin, INPUT);
pinMode(blinkLed, OUTPUT); //sets blinkLed high as output
Serial.begin(9600);
/* Now it is time to enable an interrupt. In the function call
attachInterrupt(A, B, C)
A can be either 0 or 1 for interrupts on pin 2 or 3.
B Name of a function you want to execute while in interrupt A.
C Trigger mode of the interrupt pin. can be:
LOW a low level trigger
CHANGE a change in level trigger
RISING a rising edge of a level trigger
FALLING a falling edge of a level trigger
In all but the IDLE sleep modes only LOW can be used.
*/
//***Line below read "attachInterrupt(0, wakeUpNow, Low); //use interrupt 0 (pin 2) and run function"
//wakeUpNow when pin 2 gets LOW;
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
void sleepNow(); // here we put the arduino to sleep
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
Empowering Innovation | Microchip Technology on page 35
there is a list of sleep modes which explains which clocks and
wake up sources are available in which sleep mode.
In the avr/sleep.h file, the call names of these sleep modes are to be found:
The 5 different modes are:
SLEEP_MODE_IDLE -the least power savings
SLEEP_MODE_ADC
SLEEP_MODE_PWR_SAVE
SLEEP_MODE_STANDBY
SLEEP_MODE_PWR_DOWN -the most power savings
For now, we want as much power savings as possible, so we
choose the according
sleep mode: SLEEP_MODE_PWR_DOWN
*/
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin }
/* Now it is time to enable an interrupt. We do it here so an
accidentally pushed interrupt button doesn't interrupt
our running program. if you want to be able to run
interrupt code besides the sleep function, place it in
setup() for example.
In the function call attachInterrupt(A, B, C)
A can be either 0 or 1 for interrupts on pin 2 or 3.
B Name of a function you want to execute at interrupt for A.
C Trigger mode of the interrupt pin. can be:
LOW a low level triggers
CHANGE a change in level triggers
RISING a rising edge of a level triggers
FALLING a falling edge of a level triggers
In all but the IDLE sleep modes only LOW can be used.
*/
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
// wakeUpNow when pin 2 gets LOW
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:
// disable sleep...
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
int val = 0;
//Insert Line 92 to 165 from ADXL I2C
/*** MAIN CODE **/
/ Accelerometer Readings and Interrupt */
void loop();
// Accelerometer Readings
int x, y, z;
adxl.readAccel(&x, &y, &z); // Read the accelerometer values and store them in variables declared above x,y,z
// Output Results to Serial
/* UNCOMMENT TO VIEW X Y Z ACCELEROMETER VALUES */
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
delay(500);
//MY OWN STATEMENT TO RUN TEST OF CODE
//***Change in X Axis value ledPin13 turns Optocoupler on and sets alarm
if (x > 3)
digitalWrite(ledPin, HIGH);
//***Alarm remains off without change in X Axis value
else
digitalWrite(ledPin, LOW);
ADXL_ISR();
// You may also choose to avoid using interrupts and simply run the functions within ADXL_ISR();
// and place it within the loop instead.
// This may come in handy when it doesn't matter when the action occurs.
}
/*** ISR **/
/ Look for Interrupts and Triggered Action */
void ADXL_ISR()
// getInterruptSource clears all triggered actions after returning value
// Do not call again until you need to recheck for triggered actions
byte interrupts = adxl.getInterruptSource();
// Free Fall Detection
if (adxl.triggered(interrupts, ADXL345_FREE_FALL)) {
Serial.println("*** FREE FALL ***");
//add code here to do when free fall is sensed
// Inactivity
if (adxl.triggered(interrupts, ADXL345_INACTIVITY)) {
Serial.println("*** INACTIVITY ***");
//add code here to do when inactivity is sensed
// Activity
if (adxl.triggered(interrupts, ADXL345_ACTIVITY)) {
Serial.println("*** ACTIVITY ***");
//add code here to do when activity is sensed
// Double Tap Detection
if (adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)) {
Serial.println("*** DOUBLE TAP ***");
//add code here to do when a 2X tap is sensed
// Tap Detection
if (adxl.triggered(interrupts, ADXL345_SINGLE_TAP)) {
Serial.println("*** TAP ***");
//add code here to do when a tap is sensed
//void loop()
// display information about the counter
Serial.print("Awake for ");
Serial.print(count);
Serial.println("sec");
count++;
delay(1000); // waits for a 1second
// compute the serial input
if (Serial.available())
int val = Serial.read();
if (val == 'S')
Serial.println("Serial: 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
// check if it should go to sleep because of time
if (count >= 10)
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
digitalWrite(blinkLed, HIGH); //turns blinkLed on
delay(3000);
digitalWrite(blinkLed, LOW); //turns blinkLed off
delay(500);
}