1

I'd like to make an Arduino play a (2sec).wav file once when it is powered on (via push button) and another when it is switched off i.e. when the push button is pushed there is a 2sec delay and the .wav file plays, then the arduino turns off. What would the best way be to implement this? So far I've come up with:

void loop()
{
//play .wav file

while(1){}
} 

or:

void setup(){
//play .wav file
}

void loop(){
//empty
}

2 Answers 2

1

An Arduino doesn't understand the concept of "powering off". It is either on (running) or off (not able to do anything).

Your first task must be to design a system whereby the Arduino can control its own power so the Arduino can then go "I have been asked by this external stimulus to switch off. Therefore I will play this sound and then perform whatever actions are required to remove power from myself."

Only when you have worked out how you can get the Arduino to control its own power and still have the ability to manually turn the power on can you begin to think about adding things that happen "when it powers off".

5
  • Thanks for your clear explanation Majenko. I'm a novice and am merely looking to play a sound effect when the Arduino switches on and off. Designing a "self-controlled power system" for the Arduino sounds a little daunting at this point in time : ] Commented Nov 6, 2015 at 12:50
  • Nevertheless, it is required. The Arduino can't play the sound once it is turned off, and it doesn't know you're about to turn it off, so you need some way of telling the Arduino that it should turn itself off once it has finished playing the sound. Commented Nov 6, 2015 at 13:00
  • Thanks again Majenko. Is this the type of direction I might take if undertaking it: forum.arduino.cc/index.php?topic=118504.0 Commented Nov 6, 2015 at 13:12
  • Yep, that kind of thing would be good. Commented Nov 6, 2015 at 13:15
  • Ok great, thank you. I'll look into it and see exactly what's involved. Commented Nov 6, 2015 at 13:23
0

Neither way addresses your second requirement. I would do something like:

void setup()
    {
    pinMode (OFF_BUTTON, INPUT_PULLUP);

    //play .wav file
    }

void loop()
  {

  if (digitalRead (OFF_BUTTON) == LOW)  // depending on how it is wired
    {
    delay (2000);   // 2 second delay
    // play .wav file
    // power self off somehow
    }   

}
2
  • Thanks a lot Nick. So placing the .wav instruction within setup() should do the trick for making it play once only, when the Arduino is switched on? Am I understanding your code correctly if I say that you've set the push button as a digital input pullup, which if read in as being low/off will cause a 2 sec delay followed by the .wav file playing and (in theory) then the Arduino will power itself off? I'm trying to understand why you used INPUT_PULLUP as opposed to INPUT electronics.stackexchange.com/questions/67007/… Commented Nov 6, 2015 at 12:40
  • It makes the switch read reliably. See Switches Commented Nov 7, 2015 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.