Go Down

Topic: Atmega328P with grove mp3 player (Read 1 time) previous topic - next topic

luofan

Hi!

i have some problem on playing audio file of mp3 player with Atmega328P:

Grove mp3 player is working fine with Arduino board.

However when i take out the atmega328p(use as standalone),connecting it with grove mp3 player
it is not able to function.

TX(Grove mp3 player)-->pin7(Arduino pin)       RX(Grove mp3 player)-->pin8(Arduino pin)

Crystal is already added to atmega328p.

please help, thanks!

mart256

Schematics of the circuit and code.

luofan

I don't have the schematic diagram currently, but it is basically base on "Crystal ball" example
and then i add a led,lcd,mp3 playerand distance sensor

led lcd and distance sensor is working with atmega328
only mp3 player not able to function

Here is my code :

/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial mp3(9, 10);

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("No.of Push Ups");
  pinMode(14,INPUT);//  DISTANCE SENSOR
  mp3.begin(9600);
   Serial.begin(9600);
   //delay(100);
  pinMode(6,OUTPUT);   //LED
}
void loop() {
  unsigned long currentMillis = millis();
  if(currentMillis<=60000){
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print(millis()/1000);
  buttonState5=digitalRead(14);
  if(buttonState5 != lastButtonState);
  {
   if(buttonState5==LOW)
   {
     buttonPushCounter++;
     lcd.print(buttonPushCounter);
     delay(1000);
     SetPlayMode(0x00);
     //delay(50);
     playNextSong();
     //delay(50);
   }
   SetVolume(0x1F);
    }
  }
  if(currentMillis>50000 && currentMillis<60000)
  {
    digitalWrite(6,HIGH);
    lcd.setCursor(8, 1);
    lcd.print("10s left");
    lcd.noDisplay();
    delay(500);
    lcd.display();
    delay(500);
  }
  if(currentMillis>60000)
  {
    digitalWrite(6,LOW);
    lcd.setCursor(7, 1);
    lcd.print("time's up");
    SetMusicPlay(00,81);
    delay(5000);
  }
}
boolean SetVolume(uint8_t volume)
{
 mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA7);
 mp3.write(volume);
 mp3.write(0x7E);
 //delay(10);
 while(mp3.available())
 {
 if (0xA7==mp3.read())
 return true;
 else
 return false;
 }
}

boolean SetPlayMode(uint8_t playmode)
{
  if (((playmode==0x00)|(playmode==0x01)|(playmode==0x02)|(playmode==0x03))==false)
  {
    Serial.println("PlayMode Parameter Error! ");
    return false;
  }
   mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA9);
 mp3.write(playmode);
 mp3.write(0x7E);
 //delay(10);
 while(mp3.available())
 {
 if (0xA9==mp3.read())
 return true;
 else
 return false;
 }
}
boolean SetMusicPlay(uint8_t hbyte,uint8_t lbyte)
{
   mp3.write(0x7E);
   mp3.write(0x04);
   mp3.write(0xA0);
   mp3.write(hbyte);
   mp3.write(lbyte);
   mp3.write(0x7E);
        delay(10);
    while(mp3.available())
   {
   if (0xA0==mp3.read())
   return true;
   else
   return false;
        }
}
void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
  byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
  mp3.write(0x7E);                        //Start Code for every command = 0x7E
  for(byte i = 0; i<MsgLEN+1; i++){
    mp3.write(codeMsg);                //Send the rest of the command to the GROVE MP3 player
  }
}

void playNextSong(){                                                        // Play the next song
  writeToMP3(0x02, 0xA5, 0x7E, 0x00, 0x00, 0x00, 0x00);
}

mart256

Use code tags, that code is not readable without tags.

luofan

Code: [Select]
/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial mp3(8, 9);

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("No.of Push Ups");
  pinMode(14,INPUT);//  DISTANCE SENSOR
  mp3.begin(9600);
Serial.begin(9600);
//delay(100);
  pinMode(6,OUTPUT);   //LED
}
void loop() {
  unsigned long currentMillis = millis();
  if(currentMillis<=60000){
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print(millis()/1000);
  buttonState5=digitalRead(14);
  if(buttonState5 != lastButtonState);
  {
   if(buttonState5==LOW)
   {
     buttonPushCounter++;
     lcd.print(buttonPushCounter);
     delay(1000);
     SetPlayMode(0x00);
     //delay(50);
     playNextSong();
     //delay(50);
   }
   SetVolume(0x1F);
    }
  }
  if(currentMillis>50000 && currentMillis<60000)
  {
    digitalWrite(6,HIGH);
    lcd.setCursor(8, 1);
    lcd.print("10s left");
    lcd.noDisplay();
    delay(500);
    lcd.display();
    delay(500);
  }
  if(currentMillis>60000)
  {
    digitalWrite(6,LOW);
    lcd.setCursor(7, 1);
    lcd.print("time's up");
    SetMusicPlay(00,81);
    delay(5000);
  }
}
boolean SetVolume(uint8_t volume)
{
 mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA7);
 mp3.write(volume);
 mp3.write(0x7E);
 //delay(10);
 while(mp3.available())
 {
 if (0xA7==mp3.read())
 return true;
 else
 return false;
 }
}

boolean SetPlayMode(uint8_t playmode)
{
  if (((playmode==0x00)|(playmode==0x01)|(playmode==0x02)|(playmode==0x03))==false)
  {
    Serial.println("PlayMode Parameter Error! ");
    return false;
  }
   mp3.write(0x7E);
 mp3.write(0x03);
 mp3.write(0xA9);
 mp3.write(playmode);
 mp3.write(0x7E);
 //delay(10);
 while(mp3.available())
 {
 if (0xA9==mp3.read())
 return true;
 else
 return false;
 }
}
boolean SetMusicPlay(uint8_t hbyte,uint8_t lbyte)
{
mp3.write(0x7E);
mp3.write(0x04);
mp3.write(0xA0);
mp3.write(hbyte);
mp3.write(lbyte);
mp3.write(0x7E);
        delay(10);
while(mp3.available())
{
if (0xA0==mp3.read())
return true;
else
return false;
        }
}
void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
  byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
  mp3.write(0x7E);                        //Start Code for every command = 0x7E
  for(byte i = 0; i<MsgLEN+1; i++){
    mp3.write(codeMsg[i]);                //Send the rest of the command to the GROVE MP3 player
  }
}

void playNextSong(){                                                        // Play the next song
  writeToMP3(0x02, 0xA5, 0x7E, 0x00, 0x00, 0x00, 0x00);
}

mart256

So, you say it works with Arduino board, both lcd and mp3?
It could be a lack of decoupler caps. Is the standalone on a breadboard? Dis you connect botch vcc, avcc, and all gnd? Reset pull up resistor? Powered from 5 v regulated? Crystal with 22 pf caps?

luofan

@mart256

Thank you very much!!!
I replace with crystal with 22pf caps and it is working !


Go Up