Control 4 stepper motors for Halloween

I decided to be a vending machine on this years Halloween party. The idea is to build box of thick cardboard, use a arduino Mega and four stepper motors and push buttons.
When pushing swith A it will trigger motor A to spin one round.
Switch B trigger motor B and so on. Four buttons and four motors in total.

The problem is that I'm a complete beginner, a total NOOB, on electronics, programming and Arduinos in general.

I've done a few projects from the starter kit book. I've got one stepper motor to spin. But how on earth can I make switch A to control motor A with a controlled amount of steps spinning?

I can probably figure out how to do the wiring but Iwould love some advices on how to code this and make it work.

Oh, less than 48 hours before the guests arrive. Don't you love people with long foresight? :wink:

Try this concept:







This example code will move the X stepper 2000 steps for each button press. You do not have to hold the button, just press and release. To make it work with more motors, create more instances of the AccelStepper class and copy the function xCheckButton() and change the function name and necessary variable names. The example uses the AccelStepper library and the state change detection method. See also the state change detection for active low switches tutorial.

#include <AccelStepper.h>

const byte xStepPin = 2;
const byte xDirPin = 5;
const byte enablePin = 8;  // for CNC shield
const byte  xButtonPin = 9;    // the pin that the pushbutton is attached to

// Define a stepper and the pins it will use
AccelStepper xStepper(AccelStepper::DRIVER, xStepPin, xDirPin);

int moveSteps = 2000; // steps to move for each button press

void setup()
   pinMode(xButtonPin, INPUT_PULLUP);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW); // enable steppers

   xStepper.setMaxSpeed(500); // initial speed only

void loop()

void xCheckButton()
   bool xButtonState = 0;         // current state of the button
   static bool xLastButtonState = 0;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
      timer = millis();
      // read the pushbutton input pin:
      xButtonState = digitalRead(xButtonPin);
      // compare the new buttonState to its previous state
      if (xButtonState != xLastButtonState)
         if (xButtonState == LOW)
            // if the current state is LOW then the button
            // went from off to on:
      // save the current state as the last state,
      //for next time through the loop
      xLastButtonState = xButtonState;
   };  // must be called very often
                   // ideally once every loop iteration

I suspect that given the deadline, the code is the least of your worries. What state is the costume/hardware/wiring at the moment?

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