Please Help! Sleep mode combined to buzzer and button (Arduino Pro)

Hello,

I’m quite new in this forum,

I need a big help cause I can’t find a way to integrate the sleep mode with another function.

This is the matter:

  • I programmed Arduino Pro to make a beep from a buzzer when I push and then when I release the same button (with only a 250 delay) (FUNCTION NUMBER 1)

  • I connected Arduino with a 60 mAh Lipo Battery

  • It lasts only 4 hour

  • I would like to find a way to put Arduino Pro to sleep (FUNCTION NUMBER 2) when the button is not touched, This should help it to last longer

I found some codes for the sleep mode but the problem is that I don’t know how to integrate the two functions toghether

Thanks for your help and concern,

Santiago

P.s. I attach a code I tryed but worked only for function number 1

sketch_aug20a-s-m-1.ino (2.86 KB)

Hello,

I’m quite new in this forum,

I need a big help cause I can’t find a way to integrate the sleep mode with another function.

This is the matter:

  • I programmed Arduino Pro to make a beep from a buzzer when I push and then when I release the same button (with only a 250 delay) (FUNCTION NUMBER 1)

  • I connected Arduino with a 60 mAh Lipo Battery

  • It lasts only 4 hour

  • I would like to find a way to put Arduino Pro to sleep (FUNCTION NUMBER 2) when the button is not touched, this should help it to last longer

I found some codes for the sleep mode but the problem is that I don’t know how to integrate the two functions toghether

Thanks for your help and concern,

Santiago

P.s. I attach a code I tryed but worked only for function number 1

sketch_aug20a-s-m-1.ino (2.86 KB)

Your sketch is quite close. If you use a LOW on Pin 2 (Interrupt 0) to wake up the processor, you have to make Pin 2 your button pin. Once you do that, you can have the processor sleep until the button pin goes HIGH or LOW. There is no need to keep track of the LastState because the processor only wakes up when there is a change to the new state.

Also, no need to set both pin ‘buz’ (13) and pin LED_BUILTIN (13).

I tested this code on an Arduino UNO and the LED lights up for 250 milliseconds each time the button on Pin 2 is pressed or released:

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


// this constant won't change:
const byte  ButtonPin = 2;    // the pin that the pushbutton is attached to
//   the button must be wired from pin to ground, it's pinmode is input_pullup
const byte BuzzerPin = LED_BUILTIN; // Use LED to simulate buzzer


// The Interrupt Service Routine (ISR) does nothing, but we need an ISR
void WakeISR() {}


// Sleep until the button pin goes HIGH or LOW
void SleepUntil(int state)
{
  attachInterrupt(digitalPinToInterrupt(ButtonPin), WakeISR, state);
  delay(100);


  set_sleep_mode(SLEEP_MODE_PWR_DOWN);


  sleep_enable();


  // Only sleep if the button is not yet in the desired state
  if (digitalRead(ButtonPin) != state)
    sleep_mode();  // Sleep here until selected state causes and interrupt


  sleep_disable();


  detachInterrupt(digitalPinToInterrupt(ButtonPin));
}
// https://forum.arduino.cc/index.php?topic=631378


// beep on press and another beep on release
// this one uses delay()
//  ************* ONLY TESTED WITH AN LED *************


void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println(".... beeps, uses DELAY ....");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);


  // initialize the button pin as a input with pullup so active low
  //    make sure the button is from pin to ground
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(BuzzerPin, OUTPUT);
  digitalWrite(BuzzerPin, LOW);


  Serial.println();
  Serial.println("        *** ONLY TESTED WITH AN LED ***");
  Serial.println("Setup done... press the button to beep, release to beep again");
  Serial.println();
}


void loop()
{
  // Wake up when Pin 2 goes LOW (pressed)
  SleepUntil(LOW);


  // if the current state is LOW then the button was pressed
  Serial.print("Beep on press... ");
  digitalWrite(BuzzerPin, HIGH);
  delay(250);
  digitalWrite(BuzzerPin, LOW);


  // Wake up when Pin 2 goes HIGH (released)
  SleepUntil(HIGH);


  // if the current state is HIGH then the button was released
  Serial.println("Beep on Release");
  digitalWrite(BuzzerPin, HIGH);
  delay(250);
  digitalWrite(BuzzerPin, LOW);


} //loop

You connect your awakening interrupt with pin 2 but it seems that your button pin is 12. So I guess your hardware is not matching your code but that's a wild guess as you failed to provide any detailed information about your hardware setup (wiring diagram)!

I answered this already in one of your three or four other posts were you ask the same question.
https://forum.arduino.cc/index.php?topic=662264.msg4461322#msg4461322

Topics merged

Santy75 - DO NOT start a new topic when you already have one open on the same subject or you run the risk of being banned for a period,

Duplicate topics waste time a spread the replies between different threads which does not help anyone

Hi johnwasser and pylon,

Thanks for your answer, I confirm that my button pin is 12.

Can you send me the code for that?

Can you send me the code for that?

Don't expect us to do your job. I give you another hint: if your button is fixed to pin 12 you must change your code completely, you cannot use the external interrupt feature but must use the pin change interrupt.