Hi I'm working on a sketch that plays audio files off an sd card on a pro mini 328 5v . The project is going inside a stuffed animal for my nephew and is going to run on batteries so power consumption is very important. Currently it is coded to go to sleep when it is not playing a track and is supposed to wake up when you press a button. Right now it goes through and goes to sleep. When you press the button it outputs my going to sleep message again, it will do it a second time as well but then it will never respond again until it's reset. Here's my code
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
#include <avr/power.h>
#include <avr/sleep.h>
TMRpcm tmrpcm; // create an object for use in this sketch
const int buttonPin = 5; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int track = 1;
String str;
int wakePin = 2;
int Speaker = 8; // variable for reading the speaker status
unsigned long time = 0;
void setup() {
pinMode(wakePin, INPUT);
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
pinMode(Speaker, OUTPUT);
digitalWrite(Speaker, HIGH);
Serial.begin(9600);
pinMode(SD_ChipSelectPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
digitalWrite(SD_ChipSelectPin, HIGH);
while (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
digitalWrite(SD_ChipSelectPin, LOW);
delay(500);
digitalWrite(SD_ChipSelectPin, HIGH);
SD.begin(SD_ChipSelectPin);
}
tmrpcm.play("1");
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == LOW) {
Play();
}
}
}
lastButtonState = reading;
delay(100);
if (!tmrpcm.isPlaying()) {
digitalWrite(Speaker, LOW);
digitalWrite(SD_ChipSelectPin, LOW);
sleepNow();
}
}
void Play () {
digitalWrite(SD_ChipSelectPin, HIGH);
digitalWrite(Speaker, HIGH);
Serial.print("\nButton Pressed\n");
if (!tmrpcm.isPlaying()) {
char b[5];
str = String(track); //converting integer into a string
str.toCharArray(b, 5);
if (SD.exists(b)) {
tmrpcm.play(b);
track++;
} else {
track = 1;
char b[5];
str = String(track); //converting integer into a string
str.toCharArray(b, 5);
tmrpcm.play(b);
track++;
Serial.print("\nDoesn't exist\n");
}
} else {
tmrpcm.stopPlayback();
delay(100);
char b[5];
str = String(track); //converting integer into a string
str.toCharArray(b, 5);
if (SD.exists(b)) {
tmrpcm.play(b);
track++;
} else {
track = 1;
char b[5];
str = String(track); //converting integer into a string
str.toCharArray(b, 5);
tmrpcm.play(b);
track++;
Serial.print("\nDoesn't exist\n");
}
}
}
void sleepNow(void)
{
Serial.print("\nGoing to Sleep\n");
// Set pin 2 as interrupt and attach handler:
attachInterrupt(0, Wake, LOW);
delay(100);
//
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//
// Set sleep enable (SE) bit:
sleep_enable();
//
// Put the device to sleep:
sleep_mode();
//
// Upon waking up, sketch continues from this point.
sleep_disable();
}
void Wake ()
{
detachInterrupt(0);
}
I have no idea what I'm doing wrong. Any help here would be great. Thanks!