Slot machine with Sound

Hello,
I have an issue with a project I am working on and can't seem to figure it out so I'm hoping someone with more Arduino experience here can maybe help out a bit.

I have an old slot machine that a friend wants to use as a party prop. He wants the reels to spin along with a slot machine sound to play, then the reels and sound stop. This should repeat in a loop, but every 5th time or so it would play a "jackpot" sound instead.

The reels run on some older Japanese stepper motors, and I am running three Big Easy Drivers off of a Sparkfun Redboard with e AccelStepper library to handle the motors. For the sound, I have an MDFly SD Card MP3 Player Module RS232-TTL being powered by the 5v out of the Redboard, and I am using the SerMP3 library I found here.

So I put this together in two stages; the motors first, and then then sound. each piece works fine, individually. The motors will spin for about 5 seconds all together, then stop, then spin, then stop, etc. The first MP3 will play 5 times, then the Jackpot sound will play once and then do it all over again. But no matter what I try, I cannot seem to get them to work together. As soon as I add the MP3 parts to my code the motors will not spin at all. If I comment the MP3 lines out of the loop section of my code, the motors start spinning again. I've tried using the "Multiple thing at the same time" structure (which I have used in other programs successfully), I've tried triggering the MP3 first, I've tried triggering the motors first...they will not play together nicely. Any pointers anyone could give me on this would be greatly appreciated! I've attached my current code, which I tried to simplify just to get something working, so it only tries to play the first sound and spin the reels right now. Thanks!

sketch_Slot_Machine-2.ino (1.22 KB)

There seems to be a mistake near line 42 of your code.

I've attached my current code,

Where?

This gives you some idea about merging two programs:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

But the first thing you must do is to see that both sets of hardware do not require the same pin connections on any pin.

Sorry, the forum rejected my original attachment and then would not let me post for 5 minutes, but it still posted the message without the code. It is there now.

Whandall, what error are you seeing? This is the beginning of the loop section, right?

As I wrote the Reply, there was no attachment. :wink:

BTW if the code is not too big, it is better to post it in code tags (</> button).

Some of your 'mistakes'

    delay(1000);
    delay(1000);
    delay(1000);
delay(5000);
  delay(8000);

Thanks, Whandall, I'll try that out. I know the delay(1000) bits were form an example for using AccelStepper and I just left them in there because it all seemed to work fine as a standalone piece. I'm still new to Arduino, so can ask what is particularly wrong with those in this instance if the spinning reels worked on their own with these in there before?

Thanks!

You have to switch to a non-blocking strategy, both for the MP3 and the steppers.
Get rid of all delays in loop or in functions called from loop.

Maybe there are non-blocking examples for both.

For basics see: Blink without delay, severval things at a time, etc.

ok, so I removed all the delay lines form my code. Now I see the LED light on the MP3 player, but no sound comes out, and then the reels start to turn VERY slowly....they seem to turn and pause, turn and pause in an infinite loop. If i load a new program as soon as the program starts compiling the sound plays. I tried adding a delay in after I trigger the sound and it will play as long as i have a delay afterward, but if I have the delay in there the reels don't spin!

Is this MDFLY MP3 board not a good choice maybe? I assumed if it got a trigger input it would just play the designated track until it was done, but it seems like it needs to have a continuous input in order to play.

Thanks!

So post your new modified code that exhibits that behaviours in a new post. Also a schematic would help make sense of it all.

Below is my current code. As I mentioned, the light on the MDFly board lights up like it has been triggered to play, but it does not play. The reels now spin very slow and choppy, and the sound only plays if I load a new program and the board temporarily disconnects from USB, or I add some delay after the playSound1() call, but this stops the reels from working.

I’ve been looking at some examples online for using millis() as a timer instead of delay. Do you think adding a timer in somewhere in the playSound1() routine might work? I don’t quite see why the player will not just play when triggered without the program being paused. Maybe I’m just not getting something about serial outputs as opposed to digital?

#include <AccelStepper.h>
#include <serMP3.h>


// Define a stepper and the pins it will use

AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);
AccelStepper stepper3(1, 5, 4);

int pos1 = 5000;
int pos2 = 5000;
int pos3 = 5000;

serMP3 MP3(10,11); //TX pin, Busy Pin
int spinCount;

void setup()

{  

  stepper1.setMaxSpeed(5000);

  stepper1.setAcceleration(3000);

  stepper2.setMaxSpeed(5000);

  stepper2.setAcceleration(3000);

  stepper3.setMaxSpeed(5000);

  stepper3.setAcceleration(3000);

  spinCount = 0;
  MP3.begin(31);
  Serial.begin(9600);

}


void loop(){

      playSound1();
      spinReels();
      
//      stepper1.run();
//      stepper2.run();
//      stepper3.run();

}
//===============================
void spinReels(){

  if (stepper1.distanceToGo() == 0)

  {

//    delay(1000);
    stepper1.setCurrentPosition(0);
    stepper1.moveTo(pos1);

  }

  if (stepper2.distanceToGo() == 0)

  {

//    delay(1000);
    stepper2.setCurrentPosition(0);
    stepper2.moveTo(pos2);

  }

    if (stepper3.distanceToGo() == 0)

  {

//    delay(1000);
    stepper3.setCurrentPosition(0);
    stepper3.moveTo(pos3);

  }
      stepper1.run();
      stepper2.run();
      stepper3.run();
}
//================================
void playSound1(){
 
MP3.play(001);
//delay(5000);
}

//================================
void playSound2(){
  
  MP3.play(002);
//delay(8000);
}
MP3.play(001);

You sure that is correct and it shouldn't be

MP3.play("001");

Because it is it a number then why the leading zeros 1 is just the same as 001.

001 is not an actual name of the file. This particular player has a little quirk where it creates an index for the files based on the order in which they were added to the SD card. So the first file you add is "001", the second is "002", etc. If you want to change the order you need to format the card and add the files in the new order you want.

eanderso13:
001 is not an actual name of the file. This particular player has a little quirk where it creates an index for the files based on the order in which they were added to the SD card. So the first file you add is "001", the second is "002", etc. If you want to change the order you need to format the card and add the files in the new order you want.

My MP3 module requires 4 digits (as a string) in front of the file name (so I can play 9999 files max). Are you sure yours is not the same (given that they are probably constructed in the same Far Eastern sweat work shop).

Well, I could try it. I think the serMP3 library I'm trying to use showed it as three digits, and I'm still baffled by why it works by itself and not inline with the rest of my code, but I'll give it a shot and see.

Thanks.

eanderso13:
Well, I could try it. I think the serMP3 library I'm trying to use

You really need to refer to the datasheet for the module/player that you have. The library itself may not give you the required information unless it was specifically written for your module.

In my case, I just pass it commands via Tx/Rx so I don't actually need or require a library. It runs standalone too (with umpteen different buttons). Yours may therefore be different.