Need help controlling stepper motor

Hello.
I’m a Kinetic Artist & working on a kinetic sculpture.
in this project I’m using ESP32, TB6600 stepper driver, NEMA 17 stepper motor, couple of relays, touch switch & a liner potentiometer.

the concept is to control stepper motor speed using a liner potentiometer while a touch switch is there to reverse the direction of stepper. I was using Arduino nano earlier & just upgraded my system to ESP32 as I wanted it to be connected & controlled by my smartphone & couple of other advantages I can have with ESP32.

here is the code I’m using which works fine with ESP32 & Arduino both.
I’m using AccelStepper library to control stepper.

Now my question is how can I run this this sketch with Blynk?
In addition I would like it to be running with both physical input switches & blynk together.

#include <AccelStepper.h>

#define pulsePin 18
#define dirPin 19
#define speedPin 15
#define touchPin 4
#define led 2

int ppr = 6400;  // microsteps
float maxtpr =  30; // maximum time/rotation
float mintpr =  2; // minimum time/rotation
float maxrpm = (60/mintpr); // maximum rpm
float minrpm = (60/maxtpr); // minimum rpm
float maxpps = ((maxrpm*ppr)/60); // maximum speed in pulse/second
float minpps = ((minrpm*ppr)/60); // minimum speed in pulse/second
float Speed;
bool  direction = true;
const int numReadings = 200;
int readings[numReadings];
int readIndex = 0; 
int total = 0;
int average = 0;
int touchState;             
int lasttouchState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 0;

AccelStepper wave (1,pulsePin,dirPin);

void setup(){
  pinMode (led, OUTPUT);
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }
  Serial.begin  (115200);
  Serial.print    ("Microsteps            =  ");
  Serial.println  (ppr);
  Serial.print    ("Max speed (pps)       =  ");
  Serial.println  (maxpps);
  Serial.print    ("Min speed (pps)       =  ");
  Serial.println  (minpps);
  Serial.print    ("Max speed (rpm)       =  ");
  Serial.println  (maxrpm);
  Serial.print    ("Min speed (rpm)       =  ");
  Serial.println  (minrpm);
  Serial.print    ("Max Time/rotation     =  ");
  Serial.println  (maxtpr);
  Serial.print    ("Min Time/rotation     =  ");
  Serial.println  (mintpr);
  Serial.println  ("");
}
void loop() {
  total = total - readings[readIndex];
  readings[readIndex] = analogRead(speedPin);
  total = total + readings[readIndex];
  readIndex = readIndex + 1;
if (readIndex >= numReadings)
  {
    readIndex = 0;
  }
    average = total / numReadings;
Speed = map(average,0,4095,minpps,maxpps);
int reading = digitalRead(touchPin);
  if (reading != lasttouchState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    if (reading != touchState)
    {
     touchState = reading;
      if (touchState == HIGH)
      {
       digitalWrite (led, HIGH);
       direction = !direction;
      }
     if (touchState == LOW)
     {
      digitalWrite (led, LOW);
     }
    }
  }

if (direction == true)  {Speed = Speed;}
if (direction == false) {Speed = -Speed;}

wave.setMaxSpeed(Speed);
wave.setSpeed(Speed);
wave.runSpeed();
}

The first step (no pun intended :wink:) is to move all of that code out of your void loop.

Pete.