Adding code to play .mp3

Discussion in 'Embedded Systems and Microcontrollers' started by cmjb13, Nov 30, 2015.

  1. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    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


    Code (C):
    1.  
    2. /*
    3. Stepper Motor Control - one revolution
    4.  
    5. This program drives a unipolar or bipolar stepper motor.
    6. The motor is attached to digital pins 8 - 11 of the Arduino.
    7.  
    8. The motor should revolve one revolution in one direction, then
    9. one revolution in the other direction.
    10.  
    11.  
    12. Created 11 Mar. 2007
    13. Modified 30 Nov. 2009
    14. by Tom Igoe
    15.  
    16. */
    17.  
    18. #include <Stepper.h>
    19. int x = 0;
    20. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    21. // for your motor
    22.  
    23. // initialize the stepper library on pins 8 through 11:
    24. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
    25.  
    26. void setup() {
    27.   // set the speed at 60 rpm:
    28.   myStepper.setSpeed(63);
    29.   // initialize the serial port:
    30.   Serial.begin(9600);
    31. }
    32.  
    33. void loop() {
    34. if (x <= 10)
    35. { // step one revolution in one direction:
    36.   Serial.println("clockwise");
    37.   myStepper.step(stepsPerRevolution);
    38.   // delay(500);
    39. x++;
    40. }
    41.   // step one revolution in the other direction:
    42.   // Serial.println("counterclockwise");
    43. // myStepper.step(-stepsPerRevolution);
    44. // delay(500);
    45. }
    46. [COLOR=red][B]
    47. void setup(){
    48.   Serial.begin(4800);
    49. }
    50.  
    51. void loop(){
    52.   Serial.write(0x01);[/B][COLOR=#000000]
    EDIT: Using CODE tags makes it easier for others to see your code.
     
    Last edited by a moderator: Nov 30, 2015
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,397
    497
    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.
     
  3. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
  4. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
  5. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    What mp3 shield are you using?
     
  6. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
  7. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
  8. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    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

    Code (C):
    1.  
    2. /*
    3. Stepper Motor Control - one revolution
    4.  
    5. This program drives a unipolar or bipolar stepper motor.
    6. The motor is attached to digital pins 8 - 11 of the Arduino.
    7.  
    8. The motor should revolve one revolution in one direction, then
    9. one revolution in the other direction.
    10.  
    11.  
    12. Created 11 Mar. 2007
    13. Modified 30 Nov. 2009
    14. by Tom Igoe
    15.  
    16. */
    17.  
    18. #include <Stepper.h>
    19. int x = 0;
    20. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    21. // for your motor
    22.  
    23. // initialize the stepper library on pins 8 through 11:
    24. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
    25.  
    26. void setup() {
    27.   // set the speed at 60 rpm:
    28.   myStepper.setSpeed(63);
    29.   // initialize the serial port:
    30. Serial.begin(4800);
    31. }
    32.  
    33. void loop() {
    34. if (x <= 10)
    35. { // step one revolution in one direction:
    36.   Serial.println("clockwise");
    37.   myStepper.step(stepsPerRevolution);
    38.   // delay(500);
    39. x++;
    40. }
    41.   // step one revolution in the other direction:
    42.   // Serial.println("counterclockwise");
    43. // myStepper.step(-stepsPerRevolution);
    44. // delay(500);
    45. Serial.write(0x01);
    46. }
     
  9. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    Well it should work just make sure you wire everything properly.
     
  10. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    You set first off
    Code (Text):
    1. Serial.begin(4800)
    2.  
    Then you send commands
    Code (Text):
    1. Serial.print(1,HEX)
     
  11. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    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.
     
    ISB123 likes this.
  12. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,397
    497
    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.
     
  13. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    His output is square boxes about 480 a second. Not a 0x01.
     
  14. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    His output is a square box about 480 of them a second. Not the 0x01 he wants.
     
  15. ISB123

    Well-Known Member

    May 21, 2014
    1,239
    527
    He could use a delay in aprox. length of the audio file.
     
  16. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
  17. be80be

    Senior Member

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