Combining two scripts, Button lights LED and initiates motor that pushes shelf and returns to origin position

Hey guys, I need help, I’m working on a project with a deadline and I need to combine two codes. The first is a simple button that lights up a LED, push on for on, it stays on, then push again, and it turns off (or vice versa). I have the script below. The tricky bit for me is the next code (also see below), so when I push that button, I want the next code to also initiate in sequence. Basically, I push a button, thew LED lights up, it runs a motor that pushes a shelf, and then returns to its original position for the next shelf. PLEASE help me guys, I am not a programmer and really need some brains to help me :slightly_smiling_face:

//Button toggle, when the button is pressed and afterwards released
//the LED stays on until the button is pressed once again

int led = 13;
int button = 12;

int ledState = HIGH;
int buttonCurrent;
int buttonPrevious = LOW;

void setup ()
pinMode(button, INPUT);
pinMode(led, OUTPUT);

void loop()
buttonCurrent = digitalRead(button);

if (buttonCurrent == HIGH && buttonPrevious == LOW)
if (ledState == HIGH)
ledState = LOW;
ledState = HIGH;
digitalWrite(led, ledState);

buttonPrevious = buttonCurrent;

Next Code for motor pushing shelf and returning……………..

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
void setup() {

// Sets the two pins as Outputs


void loop() {

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(long int x = 0; x < 244500; x++) {
delay(1000000); // One second delay


Hi noob,

welcome in the forum.

You have written honestly what the situation is:

  • You have a deadline (when is it?)
  • You know nothing about programming
  • you want others to serve a ready to use code

well to make sure that the code will work you have to provide a lot of more information.

I have doubts that 244500 / 200 = 1222,5 rotations of the motor are needed to push the shelf.

The code you have provided runs the motor in just one direction.
So one lesson you should learn for life is:
start very early to get an overview how much time a project will need.

You can have a lot of support on this forum as long as you fullfill one of two conditions:

  1. paying money for full service
  2. testify in every single posting that you put at least some own effort and some learning-progress into finishing this project.

I guess the first option is no option for you.

You are welcome on this forum! You are working on an informatic project and what is most needed in an informatic project is information imagine: do the other users here in the forum have a clear picture of what you are trying to do?

To speed up finishing your project you should invest some time into writing additional information I'm 100% sure that this WILL speed up finishing your project.

So please go through this checklist and if you find an item you haven't posted yet post it

  • did you write which exact type of microcontroller you are using?
  • description of your programming skills and knowledge
  • description of your knowledge about electronics
  • description of the functionality you want to have. Written in normal works avoiding programming terms
  • do you have an oscilloscope? Yes / No
  • do you have a digital multimeter (DMM) Yes / No)
  • your age
  • did you post your complete sketch?
  • if relevant did you post a link to a datasheet of each component you are using?
  • if relevant did you post a handdrawn schematic or your wiring?
  • if you got a compiler-error. Did you post the complete compiler-output into a code-section?

If you don't post all this information because you want a "quick answer" to your detail problem It is very likely to turn out that all that happens is having mutliple waiting-times with mutliple asking back for details until the other users do have a clear picture of what you want to do.

To make it more specific and easier for you:

post pictures of the mechanic of your device.

What is the exact type of stepper-motor you are using? take a look onto the stepper-motor what manufacturer what type can you read on the motor google with this information you youto find a datasheet and provide a link

What is the exact type of stepper-driver you are using. Take a look onto the stepper-motor-driver what manufacturer what type can you read on the motor google with this information to find a datasheet and provide a link.

start reading here
and write your

first attempt

what you guess / assume how the combined sketch would look like and post this attempt here with this method:

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan


// trigger processing when button pressed
#undef MyHW
#ifdef MyHW
const int stepPin = 10;
const int dirPin  = 11;
const int enPin   = 12;
const int led     = 13;
const int button  = A1;

# define PulsePeriod  200
# define NumPulses    10
# define Delay        delay

const int stepPin = 5;
const int dirPin  = 2;
const int enPin   = 8;
const int led     = 13;
const int button  = 12;

# define PulsePeriod  70
# define NumPulses    244500
# define Delay        delayMicroseconds


byte pinsOut [] = { stepPin, dirPin, enPin, led };

int state;
int buttonCurrent;
int buttonPrevious = LOW;

// -----------------------------------------------------------------------------
void moveMotor (
    byte    dir )
    digitalWrite (enPin, HIGH);

    // Enables the motor to move in a particular direction
    digitalWrite (dirPin, dir);

    // Makes 200 pulses for making one full cycle rotation
    for (long int x = 0; x < NumPulses; x++) {
        digitalWrite   (stepPin, HIGH);
        Delay          (PulsePeriod);
        digitalWrite   (stepPin, LOW);
        Delay          (PulsePeriod);

    digitalWrite (enPin, LOW);

// -----------------------------------------------------------------------------
void loop ()
    buttonCurrent = digitalRead (button);
    if (buttonPrevious != buttonCurrent)  {
        buttonPrevious = buttonCurrent;
        delay (10);             // debounce

        if (LOW == buttonCurrent)  {
            state = ! state;
            digitalWrite (led, state);
            moveMotor    (state);

// -----------------------------------------------------------------------------
void setup ()
    Serial.begin (9600);

    pinMode (button, INPUT_PULLUP);
    buttonPrevious = digitalRead (button);

    for (unsigned n = 0; n < sizeof(pinsOut); n++)  {
        digitalWrite (pinsOut [n],   LOW);
        pinMode      (pinsOut [n],   OUTPUT);

The fast and dirty way to 'merge' two (or more) sketches into one is to create this main sketch:

void setup()
  // setup3();
  // setup4();
void loop()
  // loop3();
  // loop4{};

Then, put the two (or more) sketches into 'tabs'. Just put the other sketch files into the same directory as the new 'main' sketch. When you close and re-open the 'main' sketch, the other .ino files in the directory will show up in tabs. You can also add, remove, and rename tabs with the tab menu: the little triangle at the right end of the tab bar.

Rename the setup() and loop() in each tab to setup1()/loop1() in the first tab, setup2()/loop2() in the second, etc.

If you are lucky there will be no problems and both sketches will take turns so fast they will appear to be running 'at the same time'. If you are unlucky, you will have to resolve conflicts and rewrite each sketch that hogs the CPU.

Problems to look for:

Name Conflicts: Two or more sketches defining global variables or functions of the same name. You will have to change the names until there are no conflicts.

Pin conflicts: Two or more sketches using the same pin number for different purposes. You will have to change pin assignments to avoid conflicts.

Hardware Usage Conflicts: Two or more sketches that use the same hardware feature in different ways. For example, using the Serial port at different data rates. If you can get them all to agree on a data rate you can replace the several Serial.begin(rate) calls with one at the top of setup() in the 'main' sketch.

Library Conflicts: Some libraries don't work together because they use the the same hardware for different purposes. For example the Servo library uses Timer1 and so does the IRremote library. If you are lucky you will get an error message when they both try to use the same hardware interrupt. If you are not lucky, things just won't work.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.