// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *


/*  Example for MobaTools
    Moving a stepper back and forth
*/
#include <MobaTools.h>
#include <TM1637Display.h>

// Adjust pins, steps and time as needed
const byte stepPin = 3;
const byte dirPin  = 2;
const int stepsPerRev = 400;   // Steps per Revolution ( example with 1/4 microsteps )
const long  targetPos = 1600;         // stepper moves between 0 and targetpos
int enable = 10;
long nextPos;
long Speed;
long PotVal;
long JoyStick = A2;
long Direction;
long speed;
long ActualStep;



MoToStepper myStepper ( stepsPerRev, STEPDIR );
MoToTimer stepperPause;                    // Pause between stepper moves
bool stepperRunning;

// Define the CLK and DIO pins for the TM1637 display
const int CLK = 7;  // Clock pin
const int DIO = 8;  // Data pin
TM1637Display display(CLK, DIO);


void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Setup-Start");
  myStepper.attach( stepPin, dirPin );
  myStepper.setSpeed( 600 );  // 60 Rev/Min ( if stepsPerRev is set correctly )
  myStepper.setRampLen( 50 );
  stepperRunning = true;
  pinMode(enable, OUTPUT);
  pinMode(PotVal, INPUT);
  digitalWrite(enable, LOW);
  display.setBrightness(0x0f);
}

void loop() {

  Direction = analogRead(JoyStick);         //read value of Joystick
  ActualStep = myStepper.read();            //read the actual step of the motor

  //Serial.println(ActualStep);
  //dbgc("ToL",ActualStep);
  dbgc("ToL",Direction);
  if (Direction <= 500) {                   //move the stepper CW infinitively
    speed = (500 / Direction) * 4;
    dbgc("Dir< 500",speed);
    myStepper.setSpeed(speed);
    myStepper.rotate(1);
  }
  else if (Direction >= 520) {              //move the stepper CCW infinitively
    speed = (500 / (1023 -Direction) ) * 4;
    dbgc("Dir >= 520",speed);
    myStepper.setSpeed(speed);
    myStepper.rotate(-1);

  }
  else {
    myStepper.stop();
  }

  display.showNumberDec(speed, false);
}  
A4988
4-Digit Display