camera slider control wih android

hi
I’m sorry about my bad English
I,m new in Arduino
recently we made slider camera with nema 17 - uno - cnc shield ver3 and a4988
we design may app with 5 button 1 slide to left 2 slide to right 3- set A point 4 set B point 5 run A to B
all of the first four button work well but run not work correctly and motor shake
this is my code

#include<SoftwareSerial.h>
#include <AccelStepper.h>
const int pinSTEP = 2; // Arduino Pin 2 connected to STEP pin of Easy Driver
const int pinDIR = 5; // Arduino Pin 3 connected to DIR pin of Easy Driver
const byte enablePin = 8;
AccelStepper Xaxis(1, pinSTEP, pinDIR);  // Setup of the AccelStepper Library
SoftwareSerial Bluetooth(4, 3);
long in_position = 0;
long out_position = 0;
int move_left = 0;
int move_right = 0;
int state;
int start_cycle = 0;
int set_speed = 900;

void setup() {
  // put your setup code here, to run once:
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, LOW);
  Serial.begin(9600);
  Bluetooth.begin(9600);
 Xaxis.setMaxSpeed(12800);
    Xaxis.setAcceleration(100);
}
void loop() {
  if ( Bluetooth.available() > 0)
  {
    state = Bluetooth.read();
    Serial.println(state);
  }
  if (state == '1') //left_btn Press
  {
    move_left = 1;
     
     Xaxis.setSpeed(900);


  }

  if (state == '2')    {  // Release Move Left Button
    move_left = 0;
     
  }

  if (state == '3') //left_btn Press
  {
    move_right = 1;
   
    Xaxis.setSpeed(900);


  }

  if (state == '4')    {  // Release Move Left Button
    move_right = 0;
    
  }

  if (state == '5')  //SET_A_BTN Press
  {

    in_position = Xaxis.currentPosition();
    // in_position = Bluetooth.read();
    Serial.println(in_position);
  }
  if (state == '6')  //SET_B_BTN Press
  {

    out_position = Xaxis.currentPosition();
    // out_position = Bluetooth.read();
    Serial.println(out_position);
  }
  if (state == '7')  //Run Press
  {
    
    Xaxis.setSpeed(900);
    Xaxis.moveTo(in_position);
    Xaxis.runSpeedToPosition();
    Xaxis.setSpeed(900);
    Xaxis.moveTo(out_position);
    Xaxis.runSpeedToPosition();
  

  }

  if (move_left == 1) {  // move the slider to the left while holding the left arrow on Nextion
    Xaxis.moveTo(Xaxis.currentPosition() + 75);
    Xaxis.runSpeedToPosition();
  }

  if (move_right == 1) {  // move the slider to the right while holding the right arrow on Nextion
    Xaxis.moveTo(Xaxis.currentPosition() - 75);
    Xaxis.runSpeedToPosition();
  }



}

please check it

omid3115:
all of the first four button work well but run not work correctly and motor shake

I don't understand that. You say the buttons work well and that they don't work correctly - both can't be true.

Have you written a short program just to make sure that each motor can operate properly - without any Bluetooth input?

Post a link to the datasheet for your stepper motors and tell us what stepper motor power supply you are using (volts and amps).

These links may help
Stepper Motor Basics
Simple Stepper Code

...R

hi
thanks a lot
dear ronbin
we have 5 button in my app
button 1& 2 move the slider left and right and work correctly
button 3 and 4 Save A & B position and work correctly
now button 5 my problem
I want when press this button
slider first go to A position then move to B
this code :
if (state == '7') //Run Press
{

Xaxis.setSpeed(900);
Xaxis.moveTo(in_position);
Xaxis.runSpeedToPosition();
Xaxis.setSpeed(900);
Xaxis.moveTo(out_position);
Xaxis.runSpeedToPosition();

}
when i press this button motor start to shaking and doesn't move but if change code to :

if (state == '7') //Run Press
{

Xaxis.setSpeed(900);
Xaxis.moveTo(in_position);
Xaxis.runSpeedToPosition();

}
motor correctly move to A position

my bluethooth code is here :

SoftwareSerial Bluetooth(4, 3);

Referring to Reply #2

If this code works

if (state == '7') {     //Run Press
    Xaxis.setSpeed(900);
    Xaxis.moveTo(in_position);
    Xaxis.runSpeedToPosition();
}

I can’t think why this does not

if (state == '7') {     //Run Press
    Xaxis.setSpeed(900);
    Xaxis.moveTo(in_position);
    Xaxis.runSpeedToPosition();
    
    Xaxis.setSpeed(900);
    Xaxis.moveTo(out_position);
    Xaxis.runSpeedToPosition();
}

Are you sure you have correctly copied the code from your program - maybe there is an error in the actual program?

Have you tried to see if the second part works on its own?

Are you sure that the variables in_position and out_position have substantially different values? The motor might appear to shake if it only needed to move a few steps.

…R

thanks
but this code

if (state == '7') //Run Press
{

Xaxis.setSpeed(900);
Xaxis.moveTo(out_position);
Xaxis.runSpeedToPosition();

}

work separately
two code wont work together
I think arduino execute two code at same time

omid3115:
I think arduino execute two code at same time

Definitely not.

Did you try my suggestion of adding a delay() ?

Have you investigated my other suggestions?

And please use the code button when posting code - like you did in your Original Post

Also, you say that the code in your Reply #4 works - in your next Reply please post the complete program which demonstrates that.

...R