Adding code to play .mp3

Thread Starter

cmjb13

Joined Mar 18, 2015
35
I have the below code working to spin a stepper motor. I have the arduino connected to an .mp3 card, but need to add the code to get it to play. I have added what I believe is the correct code (see below in red). Any help is appreciated. Thanks in advance


C:
/*
Stepper Motor Control - one revolution

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.

The motor should revolve one revolution in one direction, then
one revolution in the other direction.


Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe

*/

#include <Stepper.h>
int x = 0;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(63);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
if (x <= 10)
{ // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  // delay(500);
x++;
}
  // step one revolution in the other direction:
  // Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
// delay(500);
}
[COLOR=red][B]
void setup(){
  Serial.begin(4800);
}

void loop(){
  Serial.write(0x01);[/B][COLOR=#000000]
EDIT: Using CODE tags makes it easier for others to see your code.
 
Last edited by a moderator:

shteii01

Joined Feb 19, 2010
4,644
Arduino has one setup{} and one loop{}.

The serial.begin(9600) in setup{} is kinda default, but it is not required to be 9600. Change it to 4800. I doubt you will notice any difference.
 

Thread Starter

cmjb13

Joined Mar 18, 2015
35
You need to send data via the TX line into the shield.Data should contain the number of the track.

https://www.arduino.cc/en/Reference/Serial
Understood, but my original question was to make sure I had the correct code to call the .mp3 to play. Hopefully, entries 30 & 45 should do it which pulls track 001.mp3

C:
/*
Stepper Motor Control - one revolution

This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.

The motor should revolve one revolution in one direction, then
one revolution in the other direction.


Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe

*/

#include <Stepper.h>
int x = 0;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(63);
  // initialize the serial port:
Serial.begin(4800);
}

void loop() {
if (x <= 10)
{ // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  // delay(500);
x++;
}
  // step one revolution in the other direction:
  // Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
// delay(500);
Serial.write(0x01);
}
 

be80be

Joined Jul 5, 2008
2,072
Look at the output in the serial monitor don't think your MP player knows what to do with that. And you only want to send one time. Your sending as fast as 4800 can go.
 

shteii01

Joined Feb 19, 2010
4,644
Look at the output in the serial monitor don't think your MP player knows what to do with that. And you only want to send one time. Your sending as fast as 4800 can go.
What be80be is trying to tell you is that the loop{} is going to repeat itself. It is one of the disadvantages of Arduino platform. The Arduino people were planning on people doing repetitive simple things so they designed the main function to simply keep looping infinitely and called it loop.

In your case, the first time the loop function (loop{}) runs, it will start to play File X. But then it will go back to Line 34, run through the code, then go back to Line 34, run through the code, go back to Line 34, and so on Ad infinitum. It will keep starting playing the same file. You will need to add some "control"/check where the program checks the status of the mp3 player so that the program does not restart playing File X, but let the File X finish playing before "requesting"/checking further instructions.
 

be80be

Joined Jul 5, 2008
2,072
You going to have more threads about mp player you'll never keep it strait. Your not sending a hex to your no player.
 
Top