I'm working with an Arduino Mega and I'm trying to run a stepper motor and servo from the same code. There are two buttons: one operates the stepper and the other operates the servo. I'm trying to test my code and only the servo button is doing anything. I checked the serial monitor and the stepper button is registering when pressed, but it's not making the motor do anything. The motor was functioning a week or so ago and I haven't changed the wiring in any way. I'm hoping it's just a problem with my code if someone could take a look at it.

#include <Stepper.h>
#include <Servo.h>

//Also include button state declarations.
const int buttonPin = 8; //Button Pin!!
const int buttonPin2 = 5; //Button Pin 2!!
const int servoPin = 3; //Servo Pin!!
const int stepsPerRevolution = 200;  // change this to fit the number of  steps per revolution

int counter = 0;

//Stepper Variables
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 10, 11, 12, 13);//Pins for Stepper motor driver!!
Servo servo;

void setup()
  //Servo Setup
  servo.attach (servoPin);

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);

  //Stepper Setup

  // set the speed at 60 rpm:
  // initialize the serial port:

void loop()
  int buttonState;  //These can go at the very top.
  int buttonState2;

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState == HIGH)

    // step one revolution  in one direction:
    Serial.println("button 1");
    myStepper.step(-200);//This value needs to be negative. How big its absolute value is will determine the minimum stepper movement size.

  if(buttonState2 == HIGH){
    Serial.print("button 2");
    //The following can all go and  be replaced by code that will flip the servo from 0 to 90.
  if(counter == 0){
    servo.write (0); 
  } // zero degrees
  else if(counter == 1){
  /* else if(counter == 2)
    servo.write (150); 
  else if(counter == 3)  
    servo.write (180); */
    counter = 0;
  delay(150); //You will probably want a much smaller delay. Whatever delay you have here will be the length of pause between stepper motor movements.
  • do you have pull-down on button pins? – Juraj Dec 8 '18 at 7:04
  • @Juraj Yes, I do. – CaptainAmerica16 Dec 8 '18 at 7:07
  • 1
    I fixed the issue, I had an adapter plugged into the driver power supply. It was blocking the flow :P – CaptainAmerica16 Dec 8 '18 at 7:31
  • @jsotola Lol, I was literally just looking at that when you commented. I just deleted a bunch of comments. Thanks for the advice, I'm still at that stage where stuff is functional but incredibly ugly - in reference to my current skill level. – CaptainAmerica16 Dec 8 '18 at 7:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.