Hello everyone,
I am trying to implement code on my Arduino to enter sleep mode whenever the voltage at a pin (A1) is below ~2.5 V. When it is above ~2.5 V I want to display sensor values and other voltages. I am using sleep mode to help save battery life. I found this circuit and tutorial online and got it working his way, then started tweaking it to what I desire.
Here is my code so far
#include <avr/sleep.h>
#include <avr/power.h>
#include <LiquidCrystal.h>
int pin3 = 3;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
/***************************************************
* Description: Service routine for pin3 interrupt
***************************************************/
void pin3Interrupt(void)
{
// This will bring us back from sleep.
detachInterrupt(1);
}
/***************************************************
* Description: Enters the arduino into sleep mode.
***************************************************/
void enterSleep(void)
{
// Set up pin3 as an interrupt and attach handler.
attachInterrupt(1, pin3Interrupt, HIGH);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
// The program will continue from here.
sleep_disable();
}
void setup()
{
// Set up the LCD's number of columns (16) and rows (2)
lcd.begin(16, 2);
// Setup the pin direction.
pinMode(pin3, INPUT);
// Print to the LCD.
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
int sleepVoltage = 0;
void loop()
{
sleepVoltage = analogRead(A1);
if (sleepVoltage < 510)
{
lcd.clear();
lcd.print("Entering sleep");
delay(2000);
lcd.clear();
enterSleep();
}
if (sleepVoltage >= 510)
{
// Print to the LCD.
lcd.setCursor(0, 0);
lcd.print("Awake");
}
}
It works as intended, and enters sleep mode if the voltage is below 2.5 V, and wakes up when it is above. It then stays awake until the voltage dips below 2.5 V again.
The problem is that once I enter sleep mode a 2nd time, I cannot wake it up again by bringing the voltage up. I apologize if this is a simple question, but I cannot find an answer online and I can a beginner with coding as well. I appreciate any help!!