Trying to make my Nano go to sleep

So, I am trying to make my Nano go to sleep if it hasn't been used for 10 seconds.
But how do I wake it up again without resetting it? I can't use an interrupt pin, or a timer. This is what I am following:
https://www.gammon.com.au/power

[code]
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/sleep.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

static const unsigned char PROGMEM image_data_ARDUINOarray[] = {
  // ARDUINO Logo C-File Array
  // This is a logo array, removed because it is irrelevant
};

unsigned long previousMillis = 0;
const long interval = 10000;

void setup() {
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  display.drawBitmap(0, 0, image_data_ARDUINOarray, 128, 64, 1);
  display.display();

  delay(2000);

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(15, 5);
  display.println(F("Checking Battery"));
  display.setCursor(20, 25);
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.print(F("Battery"));
  display.setCursor(25, 45);
  display.print(F("Tester"));
  display.display();

  delay(2000);

  display.clearDisplay();
  display.fillRect(108, 3, 2, 3, SSD1306_INVERSE);
  display.drawRect(110, 0, 18, 10, SSD1306_INVERSE);
  display.display();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    display.clearDisplay();
    display.display();
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_cpu ();
  }
}
[/code]

The above quote covers all 3 ways that I know of waking one up and that (very good)tutorial explains all of them. Why can't you use any of them?

This is a battery tester. It needs to automatically detect when a battery to be tested is attached to the circuit. The problem is that I already ordered the PCBs - before I thought of the sleep idea!

I can definitely relate to that issue and have had to hack the PCB's to make corrections more than once. Hopefully you can do the same.

1 Like

I've tried this:

[code]
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

static const unsigned char PROGMEM image_data_ARDUINOarray[] = {
  // ARRAY IN HERE NORMALLY, REMOVED FOR FORUM
};

unsigned long previousMillis = 0;
const long interval = 60000;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  display.drawBitmap(0, 0, image_data_ARDUINOarray, 128, 64, 1);
  display.display();

  delay(2000);

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(15, 0);
  display.println(F("Checking Internal      Battery Level"));
  display.setCursor(20, 25);
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.print(F("Battery"));
  display.setCursor(25, 45);
  display.print(F("Tester"));
  display.display();

  delay(2000);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    display.clearDisplay();
    display.display();
    ADCSRA = 0;
    power_all_disable ();
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    noInterrupts ();
    sleep_enable();
    MCUCR = bit (BODS) | bit (BODSE);
    MCUCR = bit (BODS);
    sleep_cpu ();
  }
  else {
    display.clearDisplay();
    display.fillRect(108, 3, 2, 3, SSD1306_INVERSE);
    display.drawRect(110, 0, 18, 10, SSD1306_INVERSE);
    display.display();
  }
}
[/code]

But I still have to turn it off and then back on with a power switch next time I go to test a battery...

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