Assitance with program - Controlling Stepper with MIDI

Hey there.

I've built a circuit and written a code for an Arduino Mega 2560 that reads MIDI messages from a DAW (using a MIDI cable) and controls a stepper motor (Nema 17) and a servo (SG90) depending on what messages are received. I've programmed the stepper to run continously at a constant speed (set by the variable maxStepSpeed) until a MIDI message is received telling it to move a certain amount. It then continues at the speed set by maxStepSpeed. This is my full code so far:

#include <Servo.h>
#include <AccelStepper.h>
#include <MIDI.h>

//stepper variables
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

long stepsPerRev = 3200; //steps per revolution at 1/16

int stepsAMaxSpeed = 1000;
int stepsAAcceleration = 500;

int stepsBMaxSpeed = 1000;
int stepsBAcceleration = 800;

int stepsCMaxSpeed = 2000;
int stepsCAcceleration = 800;

Servo myServo;
int servoPin = 4;
int pos = 0;
int pos1 = 0;

void moveTo(int position, int speed) {
  int mapSpeed = map (speed, 0, 30, 30, 0);
  if (position > pos) {
    for (pos = pos1; pos <= position; pos += 1) {
      pos1 = pos;
  else {
    for (pos = pos1; pos >= position; pos -= 1) {
      pos1 = pos;
      delay (mapSpeed);

float maxStepSpeed=200;

//MIDI Setup & Messages
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); //using RX1
void handleNoteOn(byte channel, byte pitch, byte velocity)
  if (channel == 1 && pitch == 60) //Middle C [C3] MIDI Channel 1 - Servo & StepsA
    moveTo(90, 15); //servo


  else if (channel == 1 && pitch == 62) //D3 MIDI Channel 1 - StepsB


  else if (channel==1&&pitch==64) //E3 MIDI Channel 1 - moveStepsC & Servo

  else {}

void setup() {
  // put your setup code here, to run once:

  myServo.attach(servoPin); //attach servo

  MIDI.begin(MIDI_CHANNEL_OMNI); //listen to all MIDI channels

  moveTo(-90,15); //Servo start position

void loop() {

  while ( == 0) {

I am trying to implement the following but am having trouble figuring it out:

  1. I would like the program to start when the note C3 is received - the servo moves followed by the stepper, then the stepper starts moving at the constant maxStepSpeed. This only happens once.

  2. Following this, the stepper runs at a constant speed (maxStepSpeed) until the note D3 is played. The stepper does this movement and continues at the speed set by maxStepSpeed. This may happen more than once.

  3. When the note E3 is received I would like the stepper to run through the commands set out in the MIDI message function and for the program to return to setup.

I'm just a bit lost and wondering if I'm on the right track using a While loop in the main loop. I'm not sure how to do if statements for MIDI messages within the main loop.

If anyone has suggestions on how to implement the three points above it'd be much appreciated.

Thanks in advance for your help!

Why not continue using that angle of attack?

By the way, sending snippets, cut out code, get no points from any helper.
Maybe You know the code but helpers are complete newbies to Yourl code. Being able to read the entire code is a help to understand the big picture.

Hey Railroader. Thanks for the advice. The full code is now showing - slightly edited for simplicity.

I'm not sure how to use that angle of attack within the main loop.


Too late to do any deep diving into the code.
Set up the condition in an "if" and do what You want to do. Make a try and post the code.

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