1 (edited by przem.wilk 2024-07-15 18:03:39)

Topic: Stepper motor control and button handling with bluetooth app

Hello everybody,
This is my first time in this forum and was not sure if I chose correct category.
I have issue with button handling by using RemoteXY.Handler With this simple code provided below I want to rotate shaft of stepper motor by definied number of steps, in example 200 steps. I created simple app with to buttons: forward and backward (I wanted to use desired arrows but it did not work). With microstepping 1/16 it should rotate shaft by 1/16 of full revolution. I my case however, after tapping the button (forward or backward) in bluetooth app, it rotates by 400 steps, doubled. I would appreciate for hints why motor is rotating more than required. The execution of accurate number of steps is required by some marco slider.

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP32CORE_BLE

#include <BLEDevice.h>

// RemoteXY connection settings 
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"

#include <RemoteXY.h>

// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 43 bytes
  { 255,2,0,0,0,36,0,17,0,0,0,4,1,106,200,1,1,2,0,1,
  30,29,50,50,0,2,31,226,173,161,0,1,31,101,48,48,0,2,31,226,
  173,163,0 };
  
// this structure defines all the variables and events of your control interface 
struct {
    // input variables
  uint8_t button_forward; // =1 if button pressed, else =0
  uint8_t button_backward; // =1 if button pressed, else =0
    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;   
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define DIR_PIN 12
#define STEP_PIN 14
#define ENABLE_PIN 26 // optional
int flag = HIGH;

void setup() 
{
  RemoteXY_Init (); 
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  digitalWrite(ENABLE_PIN, LOW); // Enable the stepper driver
  // TODO your setup code
}

void stepMotor(int dir) {
  digitalWrite(DIR_PIN, dir);
  for (int x = 0; x < 200; x++) {
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(1000); // Adjust delay for speed control
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(1000); // Adjust delay for speed control
  }
}

void loop() 
{ 
//  check rotation without remotexy library --> it works properly
//  if (flag == HIGH) {
//    digitalWrite(DIR_PIN, LOW);
//    for (int x = 0; x < 3200; x++) { //full revolution by 1/16 step
//      digitalWrite(STEP_PIN, HIGH);
//      delayMicroseconds(1000); // Adjust delay for speed control
//      digitalWrite(STEP_PIN, LOW);
//      delayMicroseconds(1000); // Adjust delay for speed control
//    }
//    flag = LOW;
//  }
  
  RemoteXY_Handler (); 
  if (RemoteXY.button_forward) {
     stepMotor(HIGH); // Move forward
  }
  if (RemoteXY.button_backward) {
    stepMotor(LOW); // Move backward
  }
}

Best regards

2

Re: Stepper motor control and button handling with bluetooth app

Ok, somehow status of buttons is saved twice, so that stepper motor makes one batch of steps and then once again the same. I added one line of code to try to deactivate the second status and so far works as desired:
RemoteXY.button_forward = false; --> before going to stepper motor procedure

3

Re: Stepper motor control and button handling with bluetooth app

1. Your stepMotor function blocks program execution for 400 milliseconds. At this time, the RemoteXY library does not provide communication with the application and your controller does not see that the button is not pressed. You need to change the code and make it non-blocking.
2. You need to catch the front of the button being pressed.