UNO - SLEEPing Beauty effect - need a PRINCE ;-)

I am doing a test to insert a TILT sensor in a project to make the circuit sleep while the object is vertical and come back to life when in my hand (approx horizontal like... a chronograph).

The code attached is very barebone. The TILT sensor by itself works switching a LED off when moved to vertical position. Stop.

I inserted the code for SLEEP with sleep_mode PWR_DOWN and it goes to sleep but never wakes. I wonder what did I copy wrong, I have checked all references. Have also tried with a simple button (push closed to function and release to put it to sleep) and it works a few times before freezing. I suspect it has something to do with "bouncing" but the interrupt wake-up can't be programmed to avoid it.

As for the interrupt attach parameters I used FALLING and then CHANGE. The TILT is on a PULLUP and goes to 0 when closed (FALLING). But if SLEEP means that all is switched OFF probably only the RISING can be detected. I should then invert the state using a PULL DOWN 10K resistor?

I saw this article Sleep Learning where it says that the level interrupt must be sustained but did not grasp what I could practically do about it.

If I change the mode to IDLE, it apparently works but very strangely AND my purpose is to minimize consumption of the battery, avoid false reading from buttons and keep the display OFF.

P.S. I used a DO because in my project I have to read several buttons in this way - ALSO I user a "test variable" only to be sure that my variables will stays put while on SLEEP
PS/2 The tilt sensor is the one that comes with the Arduino Kit a four contacts ball type.

#include <avr/sleep.h>
#include <avr/power.h>

const int tiltPin = 2;      // tilt sensor pin is connected to pin 2
const int ledPin = 12;      // built-in LED is connected to pin 13

int testVar = 0;
int tiltState;               // the current reading from the sensor
int button_delay_t = 0;
unsigned long debounceDelay = 100; // the debounce time, increase if the output flickers

void setup() {
  Serial.begin(9600);

  pinMode(tiltPin, INPUT_PULLUP);       //attivazione TILT switch
  pinMode(ledPin, OUTPUT);      // Set LED pin as an OUTPUT pin
}

void loop() {
  Serial.print("test Variable  = "); 
  Serial.println(testVar);

  do {
    tiltState = digitalRead(tiltPin);    //tiltState = 0 ON (when vertical) and 1 OFF
    digitalWrite(ledPin, !tiltState);
    // Serial.print("tiltState  = "); 
    // Serial.println(tiltState);

  } while (!tiltState || ((millis()-button_delay_t) < debounceDelay));

  button_delay_t = millis();
  testVar++;
  enterSleep();
}

// sleep test
void pin2Interrupt(void) {
  detachInterrupt(digitalPinToInterrupt(tiltPin));
  digitalWrite(ledPin, HIGH);
}

void enterSleep(void) {
  attachInterrupt(digitalPinToInterrupt(tiltPin), pin2Interrupt, CHANGE);
  delay(100);
  
  digitalWrite(ledPin, LOW);
  Serial.println("Sleep!");
  delay(200);
  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  // set_sleep_mode(SLEEP_MODE_IDLE);
  
  sleep_enable();  
  sleep_mode();
  // sleep_cpu();
  /* Il programma riprende qui. */
  sleep_disable(); 
  Serial.println("Wake up!");

}

Please post a link to the tilt sensor, and post a wiring diagram (hand drawn preferred), with parts and pins clearly labeled.

This should be done in setup(), using FALLING. There is no need to use detachInterrupt().

  attachInterrupt(digitalPinToInterrupt(tiltPin), pin2Interrupt, CHANGE);

A pullup resistor (10K) to the interrupt pin, with switch contacts going from the pin to GND should work very reliably to generate a falling edge.

BTW always check the docs for your processor, to make sure which types of interrupt are supported by the chosen interrupt pin.
Capture

TILT sensor

tiltSleep

Sorry, not Picasso, I am not on my tablet with the pen... as you see it is very very basic.
Flavio

I was using the internal pull-up (also with the four buttons in the project to avoid extra components and gain space - but if I have to.. I can use the pull-down and go to the 5V).
Thanks
Flavio

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