Where in the Loop() to insert code.

HI All.
I'm using the Aduino UNO r3

I have a sketch that has 4 x void() sections.

I want to keep the board asleep for as long as possible to save battery life.
When the board senses an interrupt it has to wake up read and report the values of an ADXL345 accelerometer then go back to sleep.

My question is where do I put the code in the sketch?

On the included code sketch, from the yellow highlighted line that says void loop, onwards is what I have actually working. The board goes to sleep and wakes up with the GREEN highlighted bit being the first thing I see. My sense of logic is saying that the ADXL code should go in just after this line.
The code I think I want to use will be on the lines of; read ADXL X Axis, read ADXL Y Axis, if X Axis is greater than Y Axis turn on the red LED else turn on the green LED. if there is no interrupt (trigger to wake up the board) let the board go back to sleep.

I hope this is clear.

Thanks for looking.

The code is;

// Code to understand Loop()s
// I have a sketch that has the following construction/layout;

#include all the libraries needed

#define variables etc
void setup ()  {

Serial.begin(9600) ;

pinMode

pinMode   etc
}
void loop()  {

// Put board to sleep

void Going_To_Sleep()  {

sleep_enable();         
//Enabling sleep mode
attachInterrupt(0, wakeUp, LOW);       //attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
// Enter full sleep mode
digitalWrite(LED_BUILTIN,LOW);    //turning
LED off
delay(1000);  //wait a second to allow the led to turn off before sleeping
   sleep_cpu();  //activating sleep mode


 (GREEN HIGHLIGHTED BIT)   Serial.println("just woke up!") ;  //next line of code executed after interrupt 

digitalWrite(LED_BUILTIN,HIGH);//turning LED on



  }

void wakeUp()  {

Serial.println("Interrrupt Fired"); //Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(0); //Removes the interrupt from pin 2;
}

consider
but i'm not confident that i understand sleep modes

// Code to understand Loop()s
// I have a sketch that has the following construction/layout;
#include <avr/sleep.h>

#define pinInterrupt  2

void setup ()  {
    Serial.begin(9600) ;
    pinMode (pinInterrupt, INPUT_PULLUP);
}

void wakeUp()  {
    Serial.println("Interrrupt Fired"); //Print message to serial monitor
    sleep_disable();//Disable sleep mode
    detachInterrupt(0); //Removes the interrupt from pin 2;
}

void
going_To_Sleep()  {
    sleep_enable();
    //Enabling sleep mode
    attachInterrupt(0, wakeUp, LOW);       //attaching a interrupt to pin d2

    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    // Enter full sleep mode
    digitalWrite(LED_BUILTIN,LOW);    //turning LED off
    delay(1000);  //wait a second to allow the led to turn off before sleeping
    sleep_cpu();  //activating sleep mode

    Serial.println("just woke up!") ;  //next line of code executed after interrupt
    digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void loop()
{
    going_To_Sleep ();
}

gdthai:
The board goes to sleep and wakes up with the GREEN highlighted bit being the first thing I see. My sense of logic is saying that the ADXL code should go in just after this line.

That looks correct to me.

isn't loop() missing a closing brace?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.