Serial monitor stops when triggering switches

Read this before creating new topic:

  1. Add details :
    • Hardware model + communication type. ESP32
    • Andriod Blynk
    • Blynk server region US
    • Blynk Library version 1.3.2
    • Post your serial monitor output when experiencing some issues
#define BLYNK_TEMPLATE_ID "TMPL2ccrUxOP1"
#define BLYNK_TEMPLATE_NAME "Star tracker"

#define BLYNK_PRINT Serial
#include <Arduino.h>
#include <TMCStepper.h>
#include "FastAccelStepper.h"
#include <BlynkSimpleEsp32.h>  // Include Blynk library for ESP32

#define SERIAL_PORT_X Serial1 // HardwareSerial port pins 9 & 10
#define DRIVER_ADDRESS 0b00   // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f         // Match to your driver

TMC2209Stepper driver_x(&SERIAL_PORT_X, R_SENSE, DRIVER_ADDRESS);

FastAccelStepperEngine engine = FastAccelStepperEngine();
#define DIR_PIN_X  33
#define STEP_PIN_X 32
FastAccelStepper *motor_x = NULL;


// Virtual pins
#define MOTOR_POWER_VPIN V2
#define MOTOR_SPEED_VPIN V0
#define MOTOR_DIRECTION_VPIN V3

int microsteps = 64;   // Microsteps set to 32
int steps_per_revolution = 400 * microsteps;
int trackin_speed = steps_per_revolution / (86164/3); // Set tracking speed
int quick_turn = steps_per_revolution / 90; // 1 revultion in 90 seconds for quick turn
bool motor_power = false;  // Motor power state
int motor_speed = trackin_speed;    // Default motor speed in Hz
bool motor_direction = true;  // Motor direction (true = forward, false = reverse)

BlynkTimer timer;  // Create a BlynkTimer object


void setup()
{
    Serial.begin(115200);
    SERIAL_PORT_X.begin(115200);
    delay(1000);

    driver_x.begin();

    driver_x.toff(5);
    driver_x.rms_current(600); // NOTE: 600 initially
    driver_x.microsteps(microsteps);   // Set initial microsteps
    driver_x.pwm_autoscale(true);
    driver_x.en_spreadCycle(true);

    engine.init();

    motor_x = engine.stepperConnectToPin(STEP_PIN_X);
    motor_x->setDirectionPin(DIR_PIN_X);
    motor_x->setSpeedInHz(motor_speed);
    motor_x->setAcceleration(5000);

    // Print initial settings
    Serial.println("Motor initialized with the following settings:");
    Serial.print("Microsteps: ");
    Serial.println(microsteps);
    Serial.print("Initial Speed (Hz): ");
    Serial.println(motor_speed);

    // Initialize Blynk
    Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
    timer.setInterval(1000L, loop);  // Call handleMotorControl() every 100 ms
}

// Blynk function to control motor power
BLYNK_WRITE(MOTOR_POWER_VPIN) {
    motor_power = param.asInt();  // Read virtual button state (0 or 1)
}

// Blynk function to control motor speed
BLYNK_WRITE(MOTOR_SPEED_VPIN) {
    motor_speed = param.asInt();  // Read slider value (e.g., 100 to 2000 Hz)
}

// Blynk function to control motor direction
BLYNK_WRITE(MOTOR_DIRECTION_VPIN) {
    int buttonState = param.asInt();  // Read virtual button state (0 or 1)
    if (buttonState == 0) {           // Button is OFF
        digitalWrite(DIR_PIN_X, LOW);     // Set direction to one way
    } else if (buttonState == 1) {    // Button is ON
        digitalWrite(DIR_PIN_X, HIGH);    // Set direction to the other way
    }
}

void loop()
{
    Blynk.run();  // Run Blynk
    timer.run();  // Run the timer

    if (motor_power) {
        motor_x->setSpeedInHz(motor_speed);
        Serial.printf("Motor speed set to %d Hz\n", motor_speed);
        motor_x->setDirectionPin(motor_direction);
        motor_x->move(400);  // Move 400 steps
    } else {
        motor_x->stopMove();  // Stop the motor
    }
}

GPIO 9 & 10 are special pins connected to the SPI flash memory.
To use Serial1 you need to re-map the UART to different pins.

Read this…

Pete.