Salut!
I apologize if I did not write well in English!
I want to control a stepper motor from serial command and the Joystick! I do not know programming at all but what I did to take from the internet is shown in video and in the attached code.
The problem is that I can not get the stepper to spin at a higher speed (with more steps per second) and because in each loop the "step" increases / decrease with a unit. I do not know very well how to use a library and that's why I did not use the much used AccelStepper Library.
Can someone show me how to make better use of the AccelStepper Library properties so I can increase stepper speed and have a number of steps (position)!!!??? Or guides me to sites / tutorials. And yes, I watched at those on youtube!!!
link video: Dropbox - VID_20181203_163145.mp4 - Simplify your life
#include "AccelStepper.h"
AccelStepper stepper(1, 4, 3);
#define home_switch 9
long initial_homing;
long pozitieM;
long np;
#define x_pin A0
//int MS1 =5;
//int MS2=6;
//int MS3=7;
long lim_min_M;
long lim_max_M;
boolean start = true;
volatile boolean e_stop = false; // emergency button
void setup() {
Serial.begin(9600);
// digitalWrite(MS1, LOW);
// digitalWrite(MS2, HIGH);
// digitalWrite(MS3, LOW);
pinMode(x_pin, INPUT);
attachInterrupt( 0, e_stop_ISR, RISING); // emergency button
pinMode(home_switch, INPUT_PULLUP);
delay(5);
initial_homing=-1;
// started homing
while (digitalRead(home_switch)) {
stepper.setMaxSpeed(5000);
stepper.setAcceleration(4000);
stepper.moveTo(initial_homing);
initial_homing--;
stepper.runSpeed();
Serial.print("Nr. pasi buton dezactivat : ");
Serial.println(initial_homing);
}
stepper.setCurrentPosition(0);
initial_homing=1;
while (!digitalRead(home_switch)) {
stepper.moveTo(initial_homing);
stepper.runSpeed();
initial_homing++;
Serial.print("Nr. pasi buton activat : ");
Serial.println(initial_homing);
}
delay(500);
stepper.setCurrentPosition(0);
for (initial_homing = 0; initial_homing <=200; initial_homing++) // 200***
{
stepper.moveTo(initial_homing);
stepper.runSpeed();
Serial.print("Nr. pasi Homing: ");
Serial.println(initial_homing);
// end homing
}
stepper.setMaxSpeed(1000);
stepper.setSpeed(800);
stepper.setCurrentPosition(0);
initial_homing = 0;
}
void loop()
{
lim_max_M = 400;
lim_min_M = -400;
while (Serial.available()>0)
{
//long initial_homing;
Serial.println("");
Serial.print("initial_homing: ");
Serial.println(initial_homing);
Serial.println("");
Serial.print("Noua pozitie: ");
Serial.println(pozitieM);
Serial.println("");
Serial.print("initial_homing + pozitieM: ");
Serial.println(np);
pozitieM= Serial.parseInt();
np= pozitieM + initial_homing;
if(np < lim_min_M || np > lim_max_M)
{
Serial.println("");
Serial.println("Se depaseste intervalul 'lin_min_M & lim_max_M' : ");
Serial.println("");
}
else
{
stepper.moveTo(np);
}
if (( np )>= 0 && (np )<= lim_max_M)
{
if(np > initial_homing)
{
for (initial_homing = initial_homing; initial_homing <= np; initial_homing++)
{
if ( start == true)
{
if (e_stop == false)
{
stepper.moveTo(np);
stepper.run();
Serial.print(" '+' Pasi CW: ");
Serial.println(initial_homing );
}
else
{
Serial.println(" ");
Serial.println("Emergency: ");
start = false;
}
}
} // for
initial_homing= initial_homing - 1;
} // if
else
{
if(np < initial_homing)
{
for (initial_homing = initial_homing; initial_homing >=np; initial_homing--)
{
if ( start == true)
{
if (e_stop == false)
{
stepper.moveTo(np);
stepper.run();
Serial.print(" '+' Pasi CCW: ");
Serial.println(initial_homing);
}
else
{
Serial.println(" ");
Serial.println("Emergency: ");
start = false;
}
}
} // for
initial_homing= initial_homing + 1;
}
}
}
if ((np )>= lim_min_M && (np )<=0)
{
if(np > initial_homing)
{
for (initial_homing = initial_homing; initial_homing <= np; initial_homing++)
{
if ( start == true)
{
if (e_stop == false)
{
stepper.moveTo(np);
stepper.run();
Serial.print(" '-' Pasi CW: ");
Serial.println(initial_homing );
}
else
{
Serial.println(" ");
Serial.println("Emergency: ");
start = false;
}
}
}// for
initial_homing= initial_homing - 1;
}
else
{
if(np < initial_homing)
{
for (initial_homing = initial_homing; initial_homing >=np; initial_homing--)
{
if ( start == true)
{
if (e_stop == false)
{
stepper.moveTo(np);
stepper.run();
Serial.print(" '-' Pasi CCW: ");
Serial.println(initial_homing);
}
else
{
Serial.println(" ");
Serial.println("Emergency: ");
start = false;
}
}
}// for
initial_homing= initial_homing + 1;
}
}
}
// initial_homing = initial_homing -1;
}
while (analogRead(x_pin) >= 0 && analogRead(x_pin) <= 100)
{
if (initial_homing >= lim_min_M && initial_homing <lim_max_M )
{
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(200.0);
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
Serial.print(" Joystick CW: ");
Serial.println(initial_homing);
}
}
while (analogRead(x_pin) > 900 && analogRead(x_pin) <= 1024)
{
if (initial_homing >= lim_min_M && initial_homing <lim_max_M)
{
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(200.0);
stepper.moveTo(initial_homing);
stepper.run();
initial_homing--;
delay(5);
Serial.print(" Joystick CCW: ");
Serial.println(initial_homing);
}
if(initial_homing == lim_max_M)
{
initial_homing=initial_homing-1;
}
if(initial_homing == lim_min_M)
{
initial_homing=initial_homing+1;
}
}
}
void e_stop_ISR(void)
{
detachInterrupt(0);
e_stop = !e_stop;
}
I apologize if you did not post it in the right section!
Thank you!