Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SdFat.h> // need to include the SD library
- //#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
- SdFat sd;
- #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
- #define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- const int button3 = 7; // the number of the pushbutton pin
- int button3State = 0; // variable for reading the pushbutton status
- int lastButton3State = HIGH; // the previous reading from the input pin
- long lastDebounceTime3 = 0; // the last time the output pin was toggled
- const int button2 = 6;
- int button2State = 0;
- int lastButton2State = HIGH; // the previous reading from the input pin
- long lastDebounceTime2 = 0; // the last time the output pin was toggled
- const int buttonPin = 5; // the number of the pushbutton pin
- int buttonState = 0; // variable for reading the pushbutton status
- int lastButtonState = HIGH; // the previous reading from the input pin
- long lastDebounceTime = 0; // the last time the output pin was toggled
- int resetPin = 4;
- int resetCount = 0;
- int sdPower = 3;
- int sleepWait = 0;
- int track1 = 1;
- int track2 = 1;
- int track3 = 1;
- String str;
- int wakePin = 2;
- int Speaker = 8; // variable for reading the speaker status
- int buttonSelect = 0;
- int wasSleeping = 0;
- Sd2Card card;
- SdVolume volume;
- SdFile root;
- unsigned long time = 0;
- int failSafe = 0;
- void setup() {
- digitalWrite(resetPin, HIGH);
- pinMode(sdPower, OUTPUT);
- digitalWrite(sdPower, HIGH);
- pinMode(SD_ChipSelectPin, OUTPUT);
- digitalWrite(SD_ChipSelectPin, HIGH);
- delay(200);
- pinMode(resetPin, OUTPUT);
- pinMode(wakePin, INPUT);
- tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
- pinMode(Speaker, OUTPUT);
- digitalWrite(Speaker, LOW);
- Serial.begin(9600);
- pinMode(buttonPin, INPUT);
- digitalWrite(buttonPin, HIGH);
- while (!sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED)) {
- sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED);
- Serial.println("SD FAILED");
- }
- Serial.println("SD OK");
- }
- void loop() {
- int reading = digitalRead(buttonPin);
- if (reading != lastButtonState) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- Serial.println("button 1 pressed\n");
- }
- 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) {
- Serial.print("\nButton 1 Pressed\n");
- buttonSelect = 0;
- Play();
- }
- }
- }
- lastButtonState = reading;
- int reading2 = digitalRead(button2);
- if (reading2 != lastButton2State) {
- // reset the debouncing timer
- lastDebounceTime2 = millis();
- Serial.println("button 2 pressed\n");
- }
- if ((millis() - lastDebounceTime2) > 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 (reading2 != button2State) {
- button2State = reading2;
- // only toggle the LED if the new button state is HIGH
- if (button2State == LOW) {
- Serial.print("\nButton 2 Pressed\n");
- buttonSelect = 1;
- Play();
- }
- }
- }
- lastButton2State = reading2;
- int reading3 = digitalRead(button3);
- if (reading3 != lastButton3State) {
- // reset the debouncing timer
- lastDebounceTime3 = millis();
- Serial.println("button 3 pressed\n");
- }
- if ((millis() - lastDebounceTime3) > 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 (reading3 != button3State) {
- button3State = reading3;
- // only toggle the LED if the new button state is HIGH
- if (button3State == LOW) {
- Serial.print("\nButton 3 Pressed\n");
- buttonSelect = 2;
- Play();
- }
- }
- }
- lastButton3State = reading3;
- delay(100);
- if (!tmrpcm.isPlaying()) {
- if (sleepWait == 20) {
- digitalWrite(Speaker, LOW);
- digitalWrite(SD_ChipSelectPin, LOW);
- digitalWrite(sdPower, LOW);
- sleepNow();
- }
- sleepWait++;
- delay(50);
- }
- }
- void button2press (int reading) {
- }
- void button1press (int reading) {
- }
- void Play () {
- Serial.println("PLAYING");
- digitalWrite(SD_ChipSelectPin, HIGH);
- digitalWrite(sdPower, HIGH);
- digitalWrite(Speaker, HIGH);
- if (wasSleeping == 1) {
- if (!sd.begin(SD_ChipSelectPin, SPI_HALF_SPEED)) {
- return;
- } else {
- Serial.println("SD OK");
- }
- }
- wasSleeping = 0;
- if (buttonSelect == 0) {
- if (!tmrpcm.isPlaying()) {
- char b[20];
- str = "music/";
- str.concat(track1);
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track1++;
- } else {
- track1 = 1;
- char b[20];
- str = "music/";
- str.concat(track1);
- // str = String(track); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track1++;
- Serial.print("\nDoesn't exist\n");
- }
- } else {
- tmrpcm.stopPlayback();
- delay(100);
- char b[20];
- str = "music/";
- str.concat(track1);
- // str = String(track); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track1++;
- } else {
- str = "music/";
- track1 = 1;
- char b[20];
- str.concat(track1);
- // str = String(track); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track1++;
- Serial.print("\nDoesn't exist\n");
- }
- }
- }
- if (buttonSelect == 1) {
- if (!tmrpcm.isPlaying()) {
- str = "fx/";
- char b[20];
- str.concat(track2);
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track2++;
- } else {
- str = "fx/";
- track2 = 1;
- char b[20];
- str.concat(track2);
- // str = String(track2); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track2++;
- Serial.print("\nDoesn't exist\n");
- }
- } else {
- str = "fx/";
- tmrpcm.stopPlayback();
- delay(100);
- char b[20];
- str.concat(track2);
- // str = String(track2); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track2++;
- } else {
- str = "fx/";
- track2 = 1;
- char b[20];
- str.concat(track2);
- // str = String(track); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track2++;
- Serial.print("\nDoesn't exist\n");
- }
- }
- }
- if (buttonSelect == 2) {
- if (!tmrpcm.isPlaying()) {
- str = "msg/";
- char b[20];
- str.concat(track3);
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track3++;
- } else {
- str = "msg/";
- track3 = 1;
- char b[20];
- str.concat(track3);
- // str = String(track3); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track3++;
- Serial.print("\nDoesn't exist\n");
- }
- } else {
- tmrpcm.stopPlayback();
- delay(100);
- str = "msg/";
- char b[20];
- str.concat(track3);
- // str = String(track3); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- if (sd.exists(b)) {
- tmrpcm.play(b);
- track3++;
- } else {
- str = "msg/";
- track3 = 1;
- char b[20];
- str.concat(track3);
- // str = String(track); //converting integer into a string
- str.toCharArray(b, 20);
- Serial.print(b);
- Serial.print("\n");
- tmrpcm.play(b);
- track3++;
- Serial.print("\nDoesn't exist\n");
- }
- }
- }
- }
- void sleepNow(void)
- {
- Serial.print("\nGoing to Sleep\n");
- // Set pin 2 as interrupt and attach handler:
- digitalWrite(SD_ChipSelectPin, LOW);
- lastButton2State = LOW;
- lastButton2State = LOW;
- lastButtonState = LOW;
- delay(100);
- Serial.flush();
- //
- // Choose our preferred sleep mode:
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- noInterrupts ();
- attachInterrupt(0, Wake, LOW);
- //
- // Set sleep enable (SE) bit:
- sleep_enable();
- //
- // Put the device to sleep:
- interrupts ();
- sleep_cpu();
- //
- // Upon waking up, sketch continues from this point.
- sleep_disable();
- }
- void Wake ()
- {
- sleepWait = 0;
- wasSleeping = 1;
- detachInterrupt(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement