2

I am trying to use a keypad to trigger an audio file using an Arduino Mega and Adafruit VS1053 Music Maker. I have gotten the music files to play independently, and when the correct number is entered into the keypad a red LED flashes to green, but I have no idea how to make the music file play as a result of my combination.

What works is that the music file plays, but the problem is that it plays automatically and not upon a combination input.

I have tried putting musicPlayer.playfullfile("track002.mp3") in the loop with an if statement, and when I press the keypad the serial reader tells me it is playing the music file, but nothing is actually playing :

char key = keypad.getKey();
if (key) {
  Serial.println(key);
  if (key =='9') {
      playMusic("track001.mp3");
  }
}

I've done experiments with playmusic outside of the loop and it works fine and finds the file no problem. So the problem occurs when I try and put it into the loop as part of a keypad combination, it returns immediately without playing.

This is from the Serial reader:

>Playing music file 9  
>Playing music file 9  
>Playing music file 9  
>Playing music file

Any help would be tremendously appreciated.

My full code is below:

#include <Keypad.h>
#include <SD.h>                                           
#include <SPI.h>
#include <Adafruit_VS1053.h>

#define SD_ChipSelectPin 53

String password = "123";
String answer = "";
int position = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 51, 49, 47, 53 };
byte colPins[COLS] = { 45, 43, 41, 39 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, 
COLS);
int R = 27; // 3rd yellow wire in GROUND
int G = 29;

// define the pins used
//#define CLK 13       // SPI Clock, shared with SD card
//#define MISO 12      // Input data, from VS1053/SD card
//#define MOSI 11      // Output data, to VS1053/SD card
// Connect CLK, MISO and MOSI to hardware SPI pins. 
// See http://arduino.cc/en/Reference/SPI "Connections"

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin 
(output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin 
(output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see 
http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, 
BREAKOUT_DCS, 
DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

void setup() {
  if (! musicPlayer.begin()) {
    // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("VS1053 found"));
  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
    // Set volume for left, right channels. lower numbers == louder volume!
    musicPlayer.setVolume(20,20);
    pinMode(R, OUTPUT);
    pinMode(G, OUTPUT);
  }
  musicPlayer.playFullFile("track003.mp3");
}

void loop() {
  char key = keypad.getKey(); // init. variable key keypad is non blocking
  while (key == NO_KEY) {
    // we got no key press? -> do again
    key = keypad.getKey();
    delay(100);
  }
  answer.concat(key); // add key to answer
  if (answer.equals(password)) {
    //password OK
    musicPlayer.playFullFile("track002.mp3");
    Serial.println(F("Playing track 001"));
    LockedPosition(false);
    answer="";
  } else {
    LockedPosition(true);
  }
  if (answer.length() > password.length()) {
    // answer is different and bigger than password
    // can't become the right answer
    answer = "";
  }
  if (key == '*' || key == '#') {
    LockedPosition(true);
    answer="";
  }
}

void LockedPosition(int locked) {
  if (locked) {
    digitalWrite(R, HIGH);
    digitalWrite(G, LOW);
  } else {
    digitalWrite(R, LOW);
    digitalWrite(G, HIGH);
    delay(10000);
    digitalWrite(G,LOW);
  }
}
2
  • Hey @josecanuc, that part is from a previous code attempt to show what I've tried so far. What I was trying to do with that attempt was get the audio to play when I press '9' using the code below. The serial reader read Playing Music File, but did not actually play anything: char key = keypad.getKey(); if (key) { Serial.println(key); if (key =='9') { playMusic("track001.mp3"); } }
    – Rick
    Commented Dec 20, 2017 at 0:23
  • I've narrowed down the circumstances where this happens. I put "SD.exists("file")" at the top of the loop and it consistently returns true until the first time the keypad is used. From that point on it returns false, which explains why the musicPlayer doesn't play it. It just can't find it. Any thoughts how how the keypad could be interfering with SD?
    – Rick
    Commented Dec 24, 2017 at 18:20

1 Answer 1

0

The sketch has a pins conflict.

SD card is on SPI bus. SPI pins on Arduino Mega are pins 50, 51, 52 and 53. These pins are available on the ICSP/SPI header too. Mega compatible shields use the SPI pins on the ICSP header. Pin 53 must be set as output if the Mega is SPI master and is usual to use it as CS pin for a slave.

According to sketch the SD CS pin is 53.

According to sketch the keypad uses pins 51 and 53. 51 is SPI pin used by the shield and pin 53 is CS pin for SD card.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.