Trying to merge two sketches

Hi,

What I'm trying to do is this : control the speed of a stepper with a potentiometer AND using a button to toggle between 5 steeping modes (full step, half step...).

My sketch to control the speed works perfectly, same for the one with the button to switch modes, but when I'm trying to merge them I can only control one function, depending on where I paste the code. And I can't understand why !

The code for the speed control with potentiometer :

// Defines pins numbers
const int stepPin = 9;
const int dirPin = 8;
int customDelay,customDelayMapped; // Defines variables

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {

customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

The code for switching between different stepping modes :

const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int MS1 = 10;
int MS2 = 11;
int MS3 = 12;

// Variables will change:
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
//Serial.println("off");
}
// 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;

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter > 5 ) {
buttonPushCounter = 0;
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter == 1 && buttonState == HIGH){
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 2 && buttonState == HIGH){
Serial.println("HALF STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 3 && buttonState == HIGH){
Serial.println("1/4 STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 4 && buttonState == HIGH){
Serial.println("1/8 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 5 && buttonState == HIGH){
Serial.println("1/16 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,HIGH);

}

delay(50);
}

My attempt to merge the sketches :

/* Simple Stepper Motor Control Exaple Code
*

*/

// Defines pins numbers
const int stepPin = 9;
const int dirPin = 8;

int customDelay,customDelayMapped; // Defines variables

const int buttonPin = 7; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
int MS1 = 10;
int MS2 = 11;
int MS3 = 12;

// Variables will change:
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode (MS1,OUTPUT);
pinMode (MS2,OUTPUT);
pinMode (MS3,OUTPUT);

digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);

// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println("FULL STEP");
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 25,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;

// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
//Serial.println("off");
}
// 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;

if (buttonPushCounter > 5 ) {
buttonPushCounter = 0;
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (buttonPushCounter == 1 && buttonState == HIGH){
Serial.println("FULL STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 2 && buttonState == HIGH){
Serial.println("HALF STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,LOW);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 3 && buttonState == HIGH){
Serial.println("1/4 STEP");
digitalWrite(MS1,LOW);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 4 && buttonState == HIGH){
Serial.println("1/8 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,LOW);

}
if (buttonPushCounter == 5 && buttonState == HIGH){
Serial.println("1/16 STEP");
digitalWrite(MS1,HIGH);
digitalWrite(MS2,HIGH);
digitalWrite(MS3,HIGH);

}

delay(50);

}

It looks like the second part of the code is ignored by the arduino, why ??

Thanks !

int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 25,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;
 
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
     // Serial.println("on");
      Serial.print("number of button pushes:  ");

Do you KNOW what return does? It is pointless to put code after the unconditional return statement.

It is not at all obvious why you added code to the function without renaming the function to indicate that it now does a lot more than it used to.

It is not at all obvious why you put all that code in that function regardless of its name.

I got the potentiometer code from this tutorial, and no I don't fully understand what this return function is doing there.

I saw on the Arduino website that is means "Terminate a function and return a value from a function to the calling function, if desired", but what it means in that context I don't know.

About the button state function, it comes from the tutorial you mentioned the other day in a previous thread, about state change detection. I used this code to detect when a button is pressed, create a counter and use it to switch between stepping modes.

but what does it mean in that context I don't know.

It means "hey, we're done here. Lets go back". Nothing after the return statement is executed.

Create another function. Call it foo(), until you determine a better name. Put all the code in the speedUp() function that is after the return statement in the new function.

Then, you need to call the new function from somewhere. You will need to determine where.

The code in loop(), after the call to speedUp(), makes the stepper step once. Put that code in a function called step(), and call that function from loop().

It would appear, though, that you have two incompatible methods of connecting and controlling the stepper. One diddles with three pins, while the other expects a stepper driver with a step pin and a direction pin. I can't figure out what the three pin code is supposed to do to make a stepper step.

It is also not clear what you want the resulting program to do.

Sorry for not being clear enough. I'm using the a4988 stepper driver which has 5 stepping modes, depending on the state of the pins MS1, MS2 and MS3 :

The potentiometer program didn't need to specify the state of these pins because low low low means full steps.

All I want to do is control the speed of the stepper and being able to change the stepping mode with a button, instead of changing the state of the pins in the sketch.

Thanks A LOT for helping me, I'm new to this and it's hard to make sense out of what the programmers wrote in their examples.

I have to go now but I will do what you advised in a couple of hours.