Combining 2 separate working sketches into 1

I have 2 separate sketches

The first sketch has a push button that allows me to toggle the Arudino LED light on and off

//Button test

int button = 3; //button pin, touch to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(300);  //delay for debounce
}

Second code allows for a stepper motor to spin

/*
 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);
}

I’ve been trying to combine codes in order to allow the push button to be pushed, the stepper motor spins/runs through the program and then requires the push button to be pushed again for the process to repeat. I’ve tried different methods of combining the code with no luck so far. It may be very simple as I’m just not seeing it. Push button diagram attached.

Thank you in advance for any help.

diagram.jpg

Your toggle code is unnecessarily complex. You can make use of the fact that HIGH == true and LOW == false, and simply have

   if(press == LOW)
   {
       toggle = !toggle;
       digitalWrite(13, toggle);
   }

Your problem appears to be in the fact that you are testing whether the switch IS pressed, rather that determining that the switch HAS BECOME pressed.

Look at the state change detection example. When the switch HAS BECOME pressed, make the motor do whatever it is you want.

Commented out code is not executed. There is no reason to post code containing commented out code, and expect us to wade through the useless chatter. Exercise your delete key before posting code.

Exercising the Tools + Auto Format menu item is a good idea, too.

Thank you for the link you have provided. I believe I’m almost there…

If I have button state = LOW, I have to press and hold the button in order for it to keep spinning and release it for the spinning to stop.

If I have button state = HIGH, the motor spins right away, pressing the button stops the spinning, but resumes, unless I hold the button down.

What am I missing to toggle where I can press the button once (release/not hold), and it will spin X number times?

#include <Stepper.h>
int x = 0;
//define our variables
const int buttonPin = 3;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
 
void setup() { //this code will only run once
  pinMode(3, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(60); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function
 
void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == LOW) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(300);//do nothing waiting for button press
  }
} //end of loop function
 
void steppermotorfunction() {
  //This code runs as soon as the button is pressed
   if (x <=20)
    Serial.println("clockwise"); //print that we are running clockwise
    Serial.print("Turn #");
    myStepper.step(stepsPerRevolution);
    //delay(500);
} //end of steppermotorfunction

Thank you for the link you have provided. I believe I'm almost there...

To bad you didn't actually look at the example.

What am I missing to toggle where I can press the button once (release/not hold), and it will spin X number times?

You are looking to see if the switch IS PRESSED. If it is, you do something. If it isn't, you don't.

You need to see that the state of the switch has changed (HAS BECOME pressed or HAS BECOME released). if it has, you can then determine which change happened (to pressed or to released) and set a flag, needToStep to true if the switch HAS BECOME pressed.

As I said, the state change detection example shows how to do that.

Then, completely independent from caring about the switch, you need to care about the flag. If needToStep is true, then you need to see if it is time to step again. The blink without delay example shows how to determine that.

If you need to step, and it is time to step, take another step, reset the lastStepTime variable to now (as reported by millis()) and see if another step is needed (have you stepped all the steps necessary?). If not, clear the needToStep flag (set it to false).

As it stands, when the button is pressed and released once, the stepper motor spins.

It spins a fixed amount based on this code entry

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution

Wouldn't the issue be more of a randomizer at this point?

As it stands, when the button is pressed and released once, the stepper motor spins.

Did you make changes? Because that is not what you said earlier.

Wouldn't the issue be more of a randomizer at this point?

I don't understand the question.

My apologies for any confusion. Existing code below.

Current setup: Button is pressed once (and released) and stepper motor spins a fixed amount based on “const int stepsPerRevolution = 200” entry

Goal: Button is pressed once (and released) and stepper motor spins a random amount, not just an amount I set (in this instance 200)

#include <Stepper.h>
int x = 0;
//define our variables
const int buttonPin = 3;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
 
void setup() { //this code will only run once
  pinMode(3, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(60); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function
 
void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == LOW) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(300);//do nothing waiting for button press
  }
} //end of loop function
 
void steppermotorfunction() {
  //This code runs as soon as the button is pressed
   for (int i=0; i < 10; i++) { //turn the stepper 10 times
    Serial.println("clockwise"); //print that we are running clockwise
    Serial.print("Turn #");
    myStepper.step(stepsPerRevolution);
   }
 //end of steppermotorfunction
   }

Goal: Button is pressed once (and released) and stepper motor spins a random amount, not just an amount I set (in this instance 200)

    myStepper.step(random(0, stepsPerRevolution));

That will step some portion of a turn. If you want to step some number of whole revolutions:

    myStepper.step(random(0, 10) * stepsPerRevolution));

That will rotate 0 to 9 times, each time the switch is pressed.

You should be able to use these ideas to step however many times you want.

Changing myStepper.step(stepsPerRevolution);[/b] in the code below
** **void steppermotorfunction() {  //This code runs as soon as the button is pressed   for (int i=0; i < 10; i++) { //turn the stepper 10 times    Serial.println("clockwise"); //print that we are running clockwise    Serial.print("Turn #");    myStepper.step(stepsPerRevolution);   }** **
to myStepper.step(random(0, 10) * stepsPerRevolution);[/b] in the code below
**__ <strong>**void steppermotorfunction() {  //This code runs as soon as the button is pressed   for (int i=0; i < 10; i++) { //turn the stepper 10 times    Serial.println("clockwise"); //print that we are running clockwise    Serial.print("Turn #");     myStepper.step(random(0, 10) * stepsPerRevolution);   }**</strong> __**
…makes the stepper motor start by pressing a button, but spins infinitely. Shouldn’t it stop on a random stop?

   for (int i=0; i < 10; i++) { //turn the stepper 10 times

Why are you still using a for loop?

makes the stepper motor start by pressing a button,

I’d STILL like to see how the switch is wired (or the button is sewn on), AND see the code that deals with the switch. I’m suspecting that you have a floating pin, because the switch is not wired correctly, so the function is being called more times than you think it is.

First off, I want to thank you for all the assistance you have provided. You have been most helpful.

It appears I must have posted an incorrect sketch earlier, but the sketch below is starting & stopping properly, but does not appear to be random.

Do I understand correctly that the (10,15 - I changed it to this interval) is a minimum/maximum number of steps and the random code will “randomly” stop somewhere in that range?

The stepper motor is actually a “Price is Right” slot machine bonus wheel. The wheel has 20 numbers physically on it in 5 cent increments (.05/.10/.15/.20/.25/.30/.35/.40/.45/.50/.55/.60/.65/.70/.75/.80/.85/.90/.95/1.00)

As a test, I pushed the button ten times and got these results, and they don’t appear to be random at all.
5 times stopped at .65
5 times stopped at .80

#include <Stepper.h>
int x = 0;
//define our variables
const int buttonPin = 3;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
 
void setup() { //this code will only run once
  pinMode(3, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(60); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function
 
void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == LOW) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(300);//do nothing waiting for button press
  }
} //end of loop function
 
void steppermotorfunction() {
  //This code runs as soon as the button is pressed
   myStepper.step(random(10, 15) * stepsPerRevolution);
   }

I’ve attached a wiring diagram to show how the arduino & relevant cables are steup. I have an external .mp3 player that I was having previous difficulty getting to work. It will really be a problem if this becomes random since that .mp3 file was a set length based on a set spin interval. But, I’d rather have this working randomly without sound.

So, you apply 5V to the digital pin when the switch is pressed. You do nothing about pulling the pin down to ground when the switch is not pressed, so, you do indeed have a floating pin. You need a resistor from pin 5 to ground.

The pin will be HIGH when the switch is pressed, and LOW when it is not.

You have NOT paid any attention though I have told you twice to go look at the state change detection example, to see how to do something when the switch BECOMES pressed, NOT IS pressed.

You are choosing a random number between 10 and 14, and completing that number of revolutions. The wheel should ALWAYS stop at exactly the same place, unless stepsPerRevolution applies to the stepper rather than the wheel.

It seems to me that you want to choose two random numbers. The first would be between 10 and 20 (so, an upper limit of 21) while the second would be between 0 and 19. The first would tell how many complete turns of the wheel, while the second would cause a partial turn.

But, I am done until you read AND APPLY the state change detection example (and wire your switch correctly).