Help with stepper motor code

Hello

I built a program to control a stepper motor using the RemoteXY software and Bluetooth HC 06 technology.
Along with adding the code below, I stacked to add a pause button.

My plan is for the stepper motor to operate as follows: when I hit the pause button, it should stop, and when I touch it again, it should resume.

#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 2
#define REMOTEXY_SERIAL_TX 3
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =  // 244 bytes
  { 255, 18, 0, 24, 0, 237, 0, 16, 0, 2, 4, 0, 253, 5, 7, 49, 3, 27, 7, 41,
    164, 209, 1, 8, 55, 44, 17, 17, 18, 15, 12, 12, 2, 31, 32, 32, 70, 111, 114, 119,
    97, 114, 100, 32, 0, 129, 0, 250, 55, 17, 6, 42, 13, 18, 6, 93, 83, 112, 101, 101,
    100, 0, 129, 0, 25, 3, 45, 7, 24, 31, 18, 6, 17, 82, 111, 117, 110, 100, 32, 110,
    117, 109, 98, 101, 114, 0, 129, 0, 32, 23, 33, 6, 34, 29, 18, 6, 17, 83, 116, 101,
    112, 32, 110, 117, 109, 98, 101, 114, 0, 1, 1, 75, 30, 12, 12, 53, 7, 12, 12, 2,
    31, 62, 0, 1, 1, 12, 30, 12, 12, 53, 30, 12, 12, 2, 31, 60, 0, 67, 5, 37,
    31, 24, 8, 19, 15, 20, 5, 2, 26, 11, 67, 5, 52, 11, 24, 8, 50, 16, 20, 5,
    2, 26, 11, 7, 5, 23, 11, 24, 8, 20, 17, 20, 5, 2, 26, 2, 11, 69, 1, 90,
    3, 6, 6, 19, 35, 10, 10, 1, 8, 23, 44, 17, 17, 53, 51, 12, 12, 2, 31, 66,
    97, 99, 107, 119, 97, 114, 100, 0, 1, 0, 92, 47, 12, 12, 53, 48, 12, 12, 2, 31,
    82, 101, 115, 101, 116, 0, 1, 0, 92, 17, 12, 12, 53, 31, 12, 12, 2, 31, 80, 97,
    117, 115, 101, 0 };

// this structure defines all the variables and events of your control interface
struct {

  // input variables
  int8_t slider_speed;   // =0..100 slider position
  uint8_t button_RR;     // =1 if button pressed, else =0
  uint8_t button_r;      // =1 if button pressed, else =0
  uint8_t button_l;      // =1 if button pressed, else =0
  char edit_1[11];       // string UTF8 end zero
  uint8_t button_LL;     // =1 if button pressed, else =0
  uint8_t button_res;    // =1 if button pressed, else =0
  uint8_t button_Pause;  // =1 if button pressed, else =0

  // output variables
  char text_step[11];   // string UTF8 end zero
  char text_round[11];  // string UTF8 end zero
  int16_t sound_1;      // =0 no sound, else ID of sound, =1001 for example, look sound list in app

  // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)


/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

int delaytime = 1000;
unsigned long Msave = 0;
float stepNo = 0;
float roundNo = 0.00;
int resVal = 0;
int PauseVal = 0;
float motorRound = 0.00;
float motorSpeed = 0.00;
int startRR = 0;
int startLL = 0;
void setup() {
  RemoteXY_Init();
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  RemoteXY_Handler();
  unsigned long Mtime = millis();
  unsigned long t = Mtime - Msave;

  roundNo = (stepNo / 200);
  dtostrf(roundNo, 0, 2, RemoteXY.text_round);
  dtostrf(stepNo, 0, 2, RemoteXY.text_step);

  motorRound = atoi(RemoteXY.edit_1);

  int Dtime = 50 - RemoteXY.slider_speed / 2;

  if (RemoteXY.button_res == 1) {
    resVal = 1;
    Msave = Mtime;
  } else if (t >= delaytime) {
    if (resVal == 1) {
      stepNo = 0;
      resVal = 0;
      RemoteXY.sound_1 = 0;
    }
  }


  if (RemoteXY.button_r == 1) {
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    delay(10);
    digitalWrite(9, LOW);
    delay(10);
    stepNo++;
  } else if (RemoteXY.button_l == 1) {
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    delay(10);
    digitalWrite(9, LOW);
    delay(10);
    stepNo--;
  }


  ////////////Spining the stepper to the right
  //////////////
  if (RemoteXY.button_RR == 1 && startRR == 0) {
    startRR = 1;
    Msave = Mtime;
  } else if (startRR == 1 && t >= delaytime) {
    startRR = 2;
  } else if (roundNo <= motorRound && startRR == 2) {
    digitalWrite(8, HIGH);
    if (t >= Dtime) {
      digitalWrite(9, HIGH);
      Msave = Mtime;
      stepNo++;
    }
    if (t >= Dtime) {
      digitalWrite(9, LOW);
      Msave = Mtime;
    }
  } else if (roundNo >= motorRound && startRR == 2) {
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    startRR = 0;
    RemoteXY.sound_1 = 1000;
  }
  /////////////Spining the stepper to the left
  if (RemoteXY.button_LL == 1 && startLL == 0) {
    startLL = 1;
    Msave = Mtime;
  } else if (startLL == 1 && t <= delaytime) {
    startLL = 2;
  } else if (roundNo >= motorRound && startLL == 2) {
    digitalWrite(8, LOW);
    if (t >= Dtime) {
      digitalWrite(9, LOW);
      Msave = Mtime;
      stepNo--;
    }
    if (t >= Dtime) {
      digitalWrite(9, HIGH);
      Msave = Mtime;
    }
  } else if (roundNo <= motorRound && startLL == 2) {
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    startLL = 0;
    RemoteXY.sound_1 = 1001;
  }
}



I moved your topic to an appropriate forum category @muha_zaidi.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

What button ?

  • real hardware ?

or

  • a remoteXY-button?

Thank you for reply, Its a remoteXY - button.

Your code is not very well formatted. You should use Ctrl-T to autoformat your code
instead of hardcoded numbers for IO-pins you should use meaningful names for the io-pins and everything else.

Using meaningful names for everything makes code much easier to understand and to maintain.

So which of the existing RemoteXY-buttons shall be used for the pause/resuming function?
Does the above posted code contain that button?
If yes which button is it?
If the code does not contain this button you should at least provide a code-version where the button is defined.

You seem to create the stepper-motor-pulses with these lines of code

  if (RemoteXY.button_r == 1) {
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH); 
    delay(10);
    digitalWrite(9, LOW);  
    delay(10);
    stepNo++;
  }
  else if (RemoteXY.button_l == 1) {
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH); 
    delay(10);
    digitalWrite(9, LOW);  
    delay(10);
    stepNo--;
  }

I can only assume it because the IO-numbers "8", "9", say nothing about their purpose
You should use a stepper-motor-library.

I recommend that you use the MObaTools-library because this library creates the step-pulses automatically. You can start and stop the stepper-motor at any time and the MobaTools-library counts the steps for you automtically.

Last but not least whih exact type of microcontroller are you using?

So we need additional information from you for decent support

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