# Dont get a continious movement of stepper

I use a stepper motors 1.8 step angle unipolar, with uln2003a drivers. I will controllated the motors with the monitor serial, for example if i pulse 1 the motorZ is moved one pulse counterclockwise.
The problem is in case 6, i want a continious movement 180º counterclockwise and back, but now only send one pulse no continious. i cant get it. Someone can help me? ty so much

long angX=0;
long angZ=0;
int paso=0;

int motor1Pin1=8;
int motor1Pin2=9;
int motor1Pin3=10;
int motor1Pin4=11;
int motor2Pin1=3;
int motor2Pin2=4;
int motor2Pin3=5;
int motor2Pin4=6;

void setup() {
Serial.begin(115200);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
}
void loop(){
uint8_t c;
while(1){
if (Serial.available() > 0){
switch (c){

case ‘1’:
paso++;
if (paso==4) paso=0;
sacapaso(paso);
break;

case ‘2’:
paso–;
if (paso<0) paso=3;
sacapaso(paso);
break;

case ‘3’:
paso++;
if (paso==4) paso=0;
sacapaso2(paso);
break;

case ‘4’:
paso–;
if (paso<0) paso=3;
sacapaso2(paso);
break;

case ‘5’:
sacapaso3;
break;

case ‘6’:
if (angZ==0) angZ=0;
paso++;
if (paso==4) paso=0;
sacapaso(paso);
angZ=angZ+1.8;

if (angZ==180) angZ=180;
paso–;
sacapaso2(paso);
angZ=angZ-1.8;
break;

default:
Serial.println(“Elige”);
Serial.println("‘1’ MOTOR Z movimiento antihorario");
Serial.println("‘2’ MOTOR Z movimiento horario");
Serial.println("‘3’ MOTOR X movimiento antihorario");
Serial.println("‘4’ MOTOR X movimiento horario");
Serial.println("‘5’ STOP");
Serial.println("‘6’ A tomar medidas");
break;

}
}
}
}
void sacapaso (int j){
switch(j){
case 0:
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 1:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
break;

case 2:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 3:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
}
}
void sacapaso2 (int l){
switch(l){
case 0:
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 1:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
break;

case 2:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 3:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
break;
}
}

void sacapaso3 (){
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);

digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
}

``````void loop(){
uint8_t  c;
while(1){
``````

You clearly do not understand that loop() is called in an infinite loop. Get rid of your infinite loop.

``````        if (angZ==180) angZ=180;
``````

Why on earth do you think THIS is useful?

You need to separate the receiving of serial data from using the serial data.

Your sacapaso() and sacapaso2() functions look identical. Why do you have two functions that do the same thing?

Each causes the stepper to step ONCE. If you want continued motion, you have to call the function over and over.

In first, how can i get rid of the infinite loop.

can u explain me more about "You need to separate the receiving of serial data from using the serial data".

Yes, the functions sacapaso-2 are the same but i suppose that i need two bcs i have two stepper motors to move.

how can i call the function continious ?

if (angZ==180) angZ=180; This ofc that its stupid but idk other way to change the movement when i get 180º

if (angZ==180) angZ=180; This ofc that its stupid but idk other way to change the movement when i get 180º

You are not changing anything. If you get 180º, you set angZ to the value it currently contains. Just delete the statement.

can u explain me more about "You need to separate the receiving of serial data from using the serial data".

Create two functions, getSerialData() and useSerialData(). There must be code in each one. It should be pretty obvious which of your existing code goes in which function.

These are the ONLY two functions that should be called by loop().

I dont know how to use in correct way useSerialData() can u help me?

I did some changes in the code, for make that the function POG_MODE was called everytime but now said that sacapaso its not defined in then scope.

long angX=0;
long angZ=0;
int paso=0;

int motor1Pin1=8;
int motor1Pin2=9;
int motor1Pin3=10;
int motor1Pin4=11;
int motor2Pin1=3;
int motor2Pin2=4;
int motor2Pin3=5;
int motor2Pin4=6;

enum rangeType_T
{
POG_MODE,
NPOG_MODE,
};

void setup() {
Serial.begin(115200);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
}
void loop(){
uint8_t c;
rangeType_T easyMode = POG_MODE;
while(1){
if (Serial.available() > 0){
switch (c){

case ‘1’:
paso++;
if (paso==4) paso=0;
sacapaso(paso);
break;

case ‘2’:
paso–;
if (paso<0) paso=3;
sacapaso(paso);
break;

case ‘3’:
paso++;
if (paso==4) paso=0;
sacapaso2(paso);
break;

case ‘4’:
paso–;
if (paso<0) paso=3;
sacapaso2(paso);
break;

case ‘5’:
sacapaso3;
break;

case ‘6’:
easyMode = POG_MODE;
break;

case ‘7’:
easyMode = NPOG_MODE;
break;

default:
Serial.println(“Elige”);
Serial.println("‘1’ MOTOR Z movimiento antihorario");
Serial.println("‘2’ MOTOR Z movimiento horario");
Serial.println("‘3’ MOTOR X movimiento antihorario");
Serial.println("‘4’ MOTOR X movimiento horario");
Serial.println("‘5’ STOP");
Serial.println("‘6’ A tomar medidas");
break;

}
}
switch (easyMode){
case POG_MODE:
//Mode spin 180º degree clockwise and 180º counterclockwise motorZ, in each rev the motorX have to increase one step
if (angZ==0)
paso++;
if (paso==4) paso=0;
sacapaso(paso);
angZ=angZ+1.8;
delay(250);

if (angZ==180)
paso–;
if (paso<0) paso=3;
sacapaso(paso);
angZ=angZ-1.8;
delay(250);
break;

case NPOG_MODE:
if (angZ==0)
paso++;
if (paso==4) paso=0;
sacapaso(paso);
angZ=angZ+1.8;
delay(50);

if (angZ==180)
paso–;
if (paso<0) paso=3;
sacapaso(paso);
angZ=angZ-1.8;
delay(50);
break;

default:
angZ=0;
angX=0;
break;
}
if (angZ)
{Serial.println(angZ);
Serial.println(angX);
}
}
void sacapaso (int j){
switch(j){
case 0:
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 1:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
break;

case 2:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 3:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
}
}
void sacapaso2 (int l){
switch(l){
case 0:
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 1:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
break;

case 2:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 3:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
break;
}
}

void sacapaso3 (){
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);

digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
}

Use code tags (<>) not quote tags to post code.

ok ty

Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF.

Use Tools + Auto Format to properly indent your code.

It will become perfectly apparent what the problem is.

GET RID OF THE INFINITE WHILE LOOP IN loop().

I need again the help. Now i get the movement desired but it dont appear nothing on the serial monitor, if have to appear angZ and angX bcs its on the value is changing…

``````int angZ=0;
int angX=0;
int paso=0;

int motor1Pin1=8;
int motor1Pin2=9;
int motor1Pin3=10;
int motor1Pin4=11;
int motor2Pin1=3;
int motor2Pin2=4;
int motor2Pin3=5;
int motor2Pin4=6;

void setup() {
Serial.begin(115200);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
}
void loop(){
uint8_t  c;

while(1){
if (Serial.available() > 0){
switch (c){

case '1':
paso++;
if (paso==4) paso=0;
sacapaso(paso);
break;

case '2':
paso--;
if (paso<0) paso=3;
sacapaso(paso);
break;

case '3':
paso++;
if (paso==4) paso=0;
sacapaso2(paso);
break;

case '4':
paso--;
if (paso<0) paso=3;
sacapaso2(paso);
break;

case '5':
sacapaso3;
break;

case '6':
while(angZ<=200){
paso++;
if (paso==4) paso=0;
sacapaso(paso);
angZ=angZ+1;
delay(25); // 4 Hz
if(angZ==200){
paso++;
if (paso==4) paso=0;
sacapaso2(paso);
angX=angX+1;
}
}
}
else{
while(angZ>=0.0){
paso--;
if (paso<0) paso=3;
sacapaso(paso);
angZ=angZ-1;
delay(25);
if(angZ==0){
paso++;
if (paso==4) paso=0;
sacapaso2(paso);
angX=angX+1;
}
}
}
}

default:
Serial.println("Elige");
Serial.println("'1' MOTOR Z movimiento antihorario");
Serial.println("'2' MOTOR Z movimiento horario");
Serial.println("'3' MOTOR X movimiento antihorario");
Serial.println("'4' MOTOR X movimiento horario");
Serial.println("'5' STOP");
Serial.println("'6' A tomar medidas");
break;

}
}
if (paso)
{Serial.println(angZ*1.8);
Serial.println(angX*1.8);
}
}
}
void sacapaso (int j){
switch(j){
case 0:
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 1:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
break;

case 2:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
break;

case 3:
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
}
}
void sacapaso2 (int l){
switch(l){
case 0:
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 1:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, HIGH);
break;

case 2:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
break;

case 3:
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, HIGH);
digitalWrite(motor2Pin4, LOW);
break;
}
}

void sacapaso3 (){
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);

digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor2Pin3, LOW);
digitalWrite(motor2Pin4, LOW);
}
``````

I don't know what CASE 6 is intended to achieve.

All your print statements are inside the default CASE which means they will only be printed if the C in switch (c){ is none of '1' to '6'

I suspect your print statements should be inside one of the other CASE statements or completely outside of the SWITCH block.

...R

Robin2: I don't know what CASE 6 is intended to achieve.

All your print statements are inside the default CASE which means they will only be printed if the C in switch (c){ is none of '1' to '6'

Not quite: case '6' is a fall-through

AWOL - why is a fall-through?

ROBIN2 - The part of the information with numbers its just information, np when apper. The problem is:

``````if (paso)
{Serial.println(angZ*1.8);
Serial.println(angX*1.8);
``````

on top of the first void, bcs if the value of paso is changing, it have to appear on the monitor serial a new value of ang... and its out of default but not of the switch...

Case '6' has no "break;"

mofalafel: ROBIN2 - The part of the information with numbers its just information, np when apper.

I have no idea what that means, but maybe I don't need to.

The problem is:

``````if (paso)
{Serial.println(angZ*1.8);
Serial.println(angX*1.8);
``````

on top of the first void, bcs if the value of paso is changing, it have to appear on the monitor serial a new value of ang... and its out of default but not of the switch...

First, they are not "voids" they are "functions" Second, I can't see an if (paso) at the top of any function. It appears at the bottom of the unnecessary WHILE loop inside the loop() function. Third, get rid of the while (1) and let loop() do what is intended for. Fourth, you have not explained the purpose of CASE 6

Now to look at your question ...

It is not clear to me in what circumstance you do NOT want those two lines to print which make me wonder whether the if (paso) is required at all?

...R