Merging Programs

This is all quite new to me, so still fumbling around a bit.

I have a mega 2560 R3. Current setup and program, every time a push button is pressed, a counter goes up by 1, on a 2 digit seven segment display (using a max7219 chip), and a jingle plays (using Dfplayer Mini MP3 Player Module, powered by separate power module). When it reaches 30, it flashes twice, and resets back to Zero. Working correctly.

I have a second setup to the mega. 28BYJ-48 5V Stepper Motor + ULN2003 Motor Driver Board. For this program, the motor turns, pauses, and reverses back to starting position. Working correctly.

What I would like to do is combine the 2 programs, so that once the counter reaches 30, the motor perfoms this movement. I can get as far as the motor moving just 1 single step, but that is it. Could some one please help:

// AccelStepper - Version: Latest 
#include <AccelStepper.h>
#include <MultiStepper.h>

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000);
  stepper1.moveTo(-4000);
  

}//--(end setup )---

void loop() {

  //Change direction when the stepper reaches the target position
  if (stepper1.distanceToGo() == 0) {
    delay(1000);
    stepper1.moveTo(-stepper1.currentPosition());
  }
  stepper1.run();
}
// DFRobotDFPlayerMini-1.0.3 - Version: Latest 
#include <DFRobotDFPlayerMini.h>

// DFPlayer_Mini_Mp3 - Version: Latest 
#include <DFPlayer_Mini_Mp3.h>

#include <DFRobotDFPlayerMini.h>

/* the function */
void buttonWait(int buttonPin){
  int buttonState = 0;
  while(1){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }
  }
}


// LedControl - Version: Latest 
#include <LedControl.h>

/*
  Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
  pin 12 is connected to the DataIn
  pin 11 is connected to the CLK
  pin 10 is connected to LOAD
  We have only a single MAX72XX.
*/
LedControl lc = LedControl(12, 11, 10, 1);

/* we always wait a bit between updates of the display */
unsigned long delaytime = 1000;

void setup() {
  
  Serial.begin (9600);
 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module 
 mp3_set_volume (30);
 
  /*
    The MAX72XX is in power-saving mode on startup,
    we have to do a wakeup call
  */
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 8);
  /* and clear the display */
  lc.clearDisplay(0);
}


/*
  This method will display the characters for the
  word "Arduino" one after the other on digit 0.
*/
void writeArduinoOn7Segment() {
  
  lc.setChar(0, 0, '0', false);
  lc.setChar(0, 1, '0', false);  
  buttonWait(2); // wait for button press on pin 2
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '0', false);
  lc.setChar(0, 1, '1', false);  
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '1', false);
  lc.setChar(0, 1, '0', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  
  lc.setChar(0, 1, '1', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '2', false);
  lc.setChar(0, 1, '0', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  
  lc.setChar(0, 1, '1', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (2); //play 000.mp3
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(250);
 
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(500);
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(500);
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  
  delay(500);
  lc.clearDisplay(0);
  
  
  delay(1000);
  
}

void loop() {
  writeArduinoOn7Segment();

}

This Simple Merge Demo may give you some ideas.

Before trying to merge the programs go through each of them carefully to make sure they are not trying to use the same resource - for example an I/O pin. If you find conflicts change something in one of the separate programs and make sure it still works with the change.

If you need more help please post the program that represents your best attempt at merging the programs and tell us exactly what happens when you run it and what you want it to do that is different.

...R

Thanks for your reply! All the hardware is connected at once, just alternating between uploading the 2 different programs, which individually work correct. The stepper motor performs 2 full revolutions, pauses for a second, then reverses back to original position.

pins 0-1 (Rx-Tx) - mp3 player module
pin 2 - push button for counter
pins 3-6 - stepper motor driver board
pins 10-12 - Max7219

Once merged, I can only get the motor to move 1 step, which is so small, it can’t even be seen, only felt if holding the pin on the motor. I would like, for when the counter reaches 30, it performs the same movement as in the original program, 2 full turns, pause, then reverse 2 full turns. Had a look at the demo, I think i’m getting stuck at steps 3 and 4

// AccelStepper - Version: Latest 
#include <AccelStepper.h>
#include <MultiStepper.h>

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

// DFRobotDFPlayerMini-1.0.3 - Version: Latest 
#include <DFRobotDFPlayerMini.h>

// DFPlayer_Mini_Mp3 - Version: Latest 
#include <DFPlayer_Mini_Mp3.h>

#include <DFRobotDFPlayerMini.h>

/* the function */
void buttonWait(int buttonPin){
  int buttonState = 0;
  while(1){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }
  }
}


// LedControl - Version: Latest 
#include <LedControl.h>

/*
  Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
  pin 12 is connected to the DataIn
  pin 11 is connected to the CLK
  pin 10 is connected to LOAD
  We have only a single MAX72XX.
*/
LedControl lc = LedControl(12, 11, 10, 1);

/* we always wait a bit between updates of the display */
unsigned long delaytime = 1000;

void setup() {
  
  
  Serial.begin (9600);
 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module 
 mp3_set_volume (30);
 
  /*
    The MAX72XX is in power-saving mode on startup,
    we have to do a wakeup call
  */
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 8);
  /* and clear the display */
  lc.clearDisplay(0);
}


/*
  This method will display the characters for the
  word "Arduino" one after the other on digit 0.
*/
void writeArduinoOn7Segment() {
  
  lc.setChar(0, 0, '0', false);
  lc.setChar(0, 1, '0', false);  
  buttonWait(2); // wait for button press on pin 2
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '0', false);
  lc.setChar(0, 1, '1', false);  
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '1', false);
  lc.setChar(0, 1, '0', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  
  lc.setChar(0, 1, '1', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 0, '2', false);
  lc.setChar(0, 1, '0', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  
  lc.setChar(0, 1, '1', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '2', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '3', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '4', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '5', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '6', false);
  buttonWait(2); 
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '7', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '8', false);
  buttonWait(2);
  delay(250);
  mp3_play (1); //play 0001.mp3
  lc.setChar(0, 1, '9', false);
  buttonWait(2);
  delay(250);
  mp3_play (2); //play 000.mp3
  


  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(250);
  
  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000);
  stepper1.moveTo(4000);
  
 //Change direction when the stepper reaches the target position
   if (stepper1.distanceToGo() == 0){
    delay(1000);
    stepper1.moveTo(-stepper1.currentPosition());
   }
  stepper1.run();
   
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(500);
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  delay(500);
  lc.clearDisplay(0);
  delay(500);
  lc.setChar(0, 0, '3', false);
  lc.setChar(0, 1, '0', false);
  
  delay(500);
  lc.clearDisplay(0);
  
  
  delay(1000);
  
}

void loop() {
  writeArduinoOn7Segment();

}

Don't do that...

  while(1){

buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      return;
    }

Your code is totally locked up waiting for a single button push. One day you're going to need two buttons on one of your projects: how can it wait for both?

It also looks like you've wired the button to 5V instead of wiring it to ground and using INPUT_PULLUP mode. That's a whole lot easier to wire up, but it means that LOW==pressed.

You need to totally re-engineer your code to eliminate constructs like while(1) and delay(). Think of the loop repeating over and over very fast: thousands of times per second. If it discovers that the button which was not-pressed on the previous loop is now is-pressed, add 1 to the counter and play the sound.

Once the counter reaches 30, you enter another state which is going to drive the motor forwards by 2 turns. When it reaches that position, you have a "pause' state, then the motor drives back to the beginning. When it arrives at that position, what happens next? Reset the counter to zero or keep counting to 60?

During the time that the motor is running, do you want to look at the button? Maybe you don't care or maybe you do want to include counts from the button while the motor is still moving. By not locking up the processor in while() or for(), the main loop can continue checking the button at the same time as controlling the motor.

I haven't found a tutorial online which explains state machines the way I understand them but the best tutorial I know is written by one of the regulars here: State Machine

Why is there code for moving a stepper motor inside a function called writeArduinoOn7Segment()

If you keep the different parts of the code in different single-purpose functions it will be much easier to debug a program.

Separately, there is an incredible amount of repetition in your LCD code. It could probably all be done a few lines with a FOR loop. And when you have figured that out you can drop the FOR and allow loop() to do the repetition.

There are also a large number of delay()s in your program which will make it very unresponsive. The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R
Planning and Implementing a Program

Thanks MorganS, for this specific project, only 1 button will ever be needed. Would I gain from reversving the polarity of the switch, going from 0 to 5v, instead of 5 - 0v, if all I need is an input ? Once the counter reaches 30, it goes back to 00 as I need.

Robin2 - The stepper motor function got put there, as after several hours of trying different things and combinations, this is the closest I got to it working. I agree with the repetition, but unfortunately, although I have heard of doing FOR LOOP, my knowledge doesn't extend to writing and putting it in.
There is as well a lot of delays - What I found was, without the delays, even just tapping the button, the counter would jump several digits, as though I was holding the button down. I found putting the delay in, stopped this from happening, and the counter would go up by only 1 digit with every press.

This was my first ever program, and most of it is actually copied and pasted from what I found on the internet. I am starting to understand bits and pieces of it, but I'm still unsure of what most of the parts do. I found the counter program and got it to the numbers I needed, managed to successfully merge that with the mp3 player program I found on the internet, and now trying to merge with the motor program I found on the internet aswell, which isn't going so well. So please bear with me :slight_smile:

araab:
What I found was, without the delays, even just tapping the button, the counter would jump several digits, as though I was holding the button down. I found putting the delay in, stopped this from happening, and the counter would go up by only 1 digit with every press.

I understand the need for code that has the effect of slowing the response to your finger. But you can achieve that without using the delay() function. Admittedly the program will need some significant changes.

...R

Research "debounce". Avoid any example that uses delay().

So.…. starting straight from scratch, and going back to the beginning. After looking up Debouncing, I arrived at this site:

http://www.arduinotutorialonline.com/2018/09/make-simple-game-scoreboard-with-7.html

Going to work this one bit at a time again, so haven’t got to the mp3 module or motor yet, but think I am going in the direction as suggested. Took out everything I didn’t think I needed, as the setup was for 2 sets of displays and buttons. It seems to be working perfectly, but just need to limit it to 30 instead of 99 before going back to 00.

// LedControl - Version: Latest 
#include <LedControl.h>

LedControl lc=LedControl(12,11,10,1);  //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int button=0;


// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchOne 2


bool bPress = false;

int buttonState = 0;         // current state of the button
int lastbuttonState = 0;     // previous state of the button


void setup() {
  
  pinMode(switchOne,INPUT_PULLUP);
  
  lc.shutdown(0,false);  // Wake up MAX7219

  lc.setIntensity(0,7);  // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup
  
  lc.setDigit(0,0,0,false);  // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0,1,0,false);

}


void loop() { 

   buttonState = digitalRead(switchOne);

  // If switch 1 is clicked
  if (ButtonPress()) {
    
    button++;  // Increase by 1
  
    // convert whole number to single digits
    rightdigit=button%10;
    leftdigit=button%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,1,rightdigit,true);
    lc.setDigit(0,0,leftdigit,false);
   
    delay(5);  // Small delay to debounce the switch
  }


}

bool ButtonPress()
{
   bool isPress = false;
   // compare the buttonState to its previous state
  if (buttonState != lastbuttonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;

    } 
    
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastbuttonState = buttonState;
  return isPress;
}

Two delays "for debouncing"?

So when Button reaches 30, do the special thing.

MorganS:
Two delays "for debouncing"?

I really don't know :-[ I copied and pasted from that website, and took out everything which I thought was for a second button and second display. I'm so far out my depth, i'm now just guessing. The idea was, once reaching 30, the motor turned on for 2 revolutions and back again to it's original position as per the original motor program, and the counter then reset to 0. A jingle was supposed to play after each press, using the mp3 player module.

araab:
I'm so far out my depth, i'm now just guessing.

Just as with swimming you will find it much more productive to start over in the shallows. Study the examples that come with the Arduino IDE so that you understand how Arduino programs work.

...R

Can someone please just tell me specifically what is wrong with what I have. It’s all good apart from when I get to the motor, It will only turn 1 single step. I did some kind of ‘do while loop’ (which I haven’t been able to repeat), which got the motor working as it should do, but I got then got stuck in that one single loop of the motor going forwards and backwards, and the program wouldn’t go any further. I have looked at tutorials, googled websites, youtube, looked up accelstepper library instructions.

I have been at this for over a month, stuck on this one piece, regularly spending several hours a day and now just fed up with it, and I can’t go any further till I get this sorted. Thanks

// AccelStepper - Version: Latest
#include <AccelStepper.h>
#include <MultiStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


// DFPlayer_Mini_Mp3 - Version: Latest
#include <DFPlayer_Mini_Mp3.h>

// LedControl - Version: Latest
#include <LedControl.h>



LedControl lc = LedControl(12, 11, 10, 1); //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int button = 0;


// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchOne 2


bool bPress = false;

int buttonState = 0;         // current state of the button
int lastbuttonState = 0;     // previous state of the button


void setup() {

  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000);
  stepper1.moveTo(4000);

  Serial.begin (9600);
  mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
  mp3_set_volume (30);

  pinMode(switchOne, INPUT_PULLUP);

  lc.shutdown(0, false); // Wake up MAX7219

  lc.setIntensity(0, 7); // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #

  // Put zeros on both displays at startup

  lc.setDigit(0, 0, 0, false); // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0, 1, 0, false);

}


void loop() {

  buttonState = digitalRead(switchOne);

  // If switch 1 is clicked
  if (ButtonPress()) {

    button++;  // Increase by 1
    //////////////////////////////////////////////////////////////

    if (button >= 4) {

      mp3_play (2);

      //Change direction when the stepper reaches the target position
      if (stepper1.distanceToGo() == 0) {
        delay(1000);
        stepper1.moveTo(-stepper1.currentPosition());
      }
      stepper1.run();

      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);
      delay(500);
      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);
      delay(500);
      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);

      delay(500);
      lc.clearDisplay(0);

      delay(1000);
      button = 0;
    }

    //////////////////////////////////////////////////////////////////

    // convert whole number to single digits
    rightdigit = button % 10;
    leftdigit = button % 100 / 10;

    // Display extracted digits on the display
    lc.setDigit(0, 1, rightdigit, true);
    lc.setDigit(0, 0, leftdigit, false);

    delay(5);  // Small delay to debounce the switch

    // Play Mp3 file 1
    if (button >= 1) {
      mp3_play (1);
    }
  }


}

bool ButtonPress()
{
  bool isPress = false;
  // compare the buttonState to its previous state
  if (buttonState != lastbuttonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;

    }

    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastbuttonState = buttonState;
  return isPress;
}

If you use the AutoFormat tool to indent your code consistently the different blocks will be easy to see.

You have a huge chunk of code (from about line 76 to line 130) which includes the stepper code all governed by

if (ButtonPress()) {

which probably means that that chunk of code will only run once for every button press.

I suspect the end of that IF should be at line 79.

...R

So, edited the last post with the indents. And moved the '{' as below to line 79, however, the stepper remains the same, but now '1.mp3' now plays on a continous loop after first presss, regardless if the button is pressed or not, and only stops once it gets to 4 presses and plays '2.mp3'.

  // If switch 1 is clicked
  if (ButtonPress()) 

    button++;  // Increase by 1
    //////////////////////////////////////////////////////////////
    {
    if (button >= 4) {

araab:
So, edited the last post with the indents.

Please post the complete program with your changes in your next Reply. I do hope you have NOT updated the code in Reply #12 to which I referred in Reply #13.

Making changes to earlier Replies (other than to correct Typos) makes the Thread very difficult to follow. For example it is useful to be able to compare the new and the old code.

...R

no, post #12 was just updated with indentation. The only difference between that code, and the code below is moving ‘{’ from line 76 to line 80 as you suggested. and the result I posted in my last post.

// AccelStepper - Version: Latest
#include <AccelStepper.h>
#include <MultiStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


// DFPlayer_Mini_Mp3 - Version: Latest
#include <DFPlayer_Mini_Mp3.h>

// LedControl - Version: Latest
#include <LedControl.h>



LedControl lc = LedControl(12, 11, 10, 1); //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int button = 0;


// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchOne 2


bool bPress = false;

int buttonState = 0;         // current state of the button
int lastbuttonState = 0;     // previous state of the button


void setup() {

  stepper1.setMaxSpeed(1000.0);
  stepper1.setAcceleration(1000.0);
  stepper1.setSpeed(1000);
  stepper1.moveTo(4000);

  Serial.begin (9600);
  mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
  mp3_set_volume (30);

  pinMode(switchOne, INPUT_PULLUP);

  lc.shutdown(0, false); // Wake up MAX7219

  lc.setIntensity(0, 7); // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #

  // Put zeros on both displays at startup

  lc.setDigit(0, 0, 0, false); // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0, 1, 0, false);

}


void loop() {

  buttonState = digitalRead(switchOne);

  // If switch 1 is clicked
  if (ButtonPress()) 

    button++;  // Increase by 1
    //////////////////////////////////////////////////////////////
    {
    if (button >= 4) {

      mp3_play (2);

      //Change direction when the stepper reaches the target position
      if (stepper1.distanceToGo() == 0) {
        delay(1000);
        stepper1.moveTo(-stepper1.currentPosition());
      }
      stepper1.run();

      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);
      delay(500);
      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);
      delay(500);
      lc.clearDisplay(0);
      delay(500);
      lc.setChar(0, 0, '3', false);
      lc.setChar(0, 1, '0', false);

      delay(500);
      lc.clearDisplay(0);

      delay(1000);
      button = 0;
    }

    //////////////////////////////////////////////////////////////////

    // convert whole number to single digits
    rightdigit = button % 10;
    leftdigit = button % 100 / 10;

    // Display extracted digits on the display
    lc.setDigit(0, 1, rightdigit, true);
    lc.setDigit(0, 0, leftdigit, false);

    delay(5);  // Small delay to debounce the switch

    // Play Mp3 file 1
    if (button >= 1) {
      mp3_play (1);
    }
  }


}

bool ButtonPress()
{
  bool isPress = false;
  // compare the buttonState to its previous state
  if (buttonState != lastbuttonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;

    }

    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastbuttonState = buttonState;
  return isPress;
}

I forgot to say in my previous Reply that you cannot use delay() if you want to use stepper.run() because it must be called much more frequently than the rate at which steps should happen.

If you are content for the program to mark time until all the steps are complete use the function stepper.runToPosition()

...R

This…

if (ButtonPress())

button++; // Increase by 1

Is the same as writing this…

 if (ButtonPress())  button++;  // Increase by 1

Is the same as writing this…

 if (ButtonPress()) 
  {
    button++;  // Increase by 1
  }

Can you see the error now?

MorganS:
Can you see the error now?

No.

...R