Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Stepper Control with if statement
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Stepper Control with if statement

by Old_Man_Robo on Tue Mar 10, 2020 12:55 pm

Greetings.
I'm attempting to control a stepper. I'm already able to get the stepper to move, however I'm struggling with the 'if' function as a method of control Here is my code:
Code: Select all | TOGGLE FULL SIZE
int Clock_pin=9;
int Dir_pin=8;
int En_pin=7;
int i=0;

void setup() {
  // put your setup code here, to run once:
pinMode(Clock_pin,OUTPUT);
pinMode(Dir_pin,OUTPUT);
pinMode(En_pin,OUTPUT);


digitalWrite(Clock_pin, LOW);
digitalWrite(Dir_pin, LOW);
digitalWrite(En_pin, HIGH);
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly:

if(i!=50,++i)

{
digitalWrite(Clock_pin, HIGH);
delay(3);
digitalWrite(Clock_pin, LOW);
delay(3);
Serial.println(i);
}
else if(i=50)
{
  digitalWrite(Clock_pin, LOW);
  }
}


Using the serial monitor, it appears i is incrementing correctly, however, the motor does not stop at 50.
Where am I going wrong? My understanding is that each time through the loop, i is incremented by 1. The next time through, i is compared to 50. If it is not equal to 50, it increments again and runs the loop. Once it reaches 50, it goes to the else statement, which tells it to just turn Clock_pin LOW.

What am I missing?

Old_Man_Robo
 
Posts: 6
Joined: Wed Feb 26, 2020 1:57 pm

Re: Stepper Control with if statement

by adafruit_support_bill on Tue Mar 10, 2020 1:10 pm

Your logic is overconstrained, and your 'else' clause is an assignment rather than a comparison. So you end up skipping right over 50.

A safer way to code it is like this:

Code: Select all | TOGGLE FULL SIZE
if(i < 50,++i)
{
   digitalWrite(Clock_pin, HIGH);
   delay(3);
   digitalWrite(Clock_pin, LOW);
   delay(3);
   Serial.println(i);
}
else
{
   digitalWrite(Clock_pin, LOW);
}


adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Control with if statement

by Old_Man_Robo on Tue Mar 10, 2020 1:13 pm

Ah! Brilliant. Thank you!

Old_Man_Robo
 
Posts: 6
Joined: Wed Feb 26, 2020 1:57 pm

Re: Stepper Control with if statement

by Old_Man_Robo on Tue Mar 10, 2020 1:20 pm

Hmm. It's still doing it. Here is the change I made:

Code: Select all | TOGGLE FULL SIZE
if(i<50,++i)

{
digitalWrite(Clock_pin, HIGH);
delay(3);
digitalWrite(Clock_pin, LOW);
delay(3);
Serial.println(i);
}
else
{
  digitalWrite(Clock_pin, LOW);
  }
}


The increment continues and the motor continues to turn.

Old_Man_Robo
 
Posts: 6
Joined: Wed Feb 26, 2020 1:57 pm

Re: Stepper Control with if statement

by adafruit_support_bill on Tue Mar 10, 2020 1:26 pm

Code: Select all | TOGGLE FULL SIZE
if(i<50,++i)

Looks like the ++i is the issue. You have 2 parts to your conditional clause separated by a comma. If I recall correctly, that is a somewhat obscure feature of the C++ syntax and it will execute both parts when evaluating the 'if'. And since the ++i is the last part, that is the 'value' of the conditional. And since it is non-zero, it is 'true'.
Better to put the ++i inside the code block:

Code: Select all | TOGGLE FULL SIZE
if(i < 50)
{
   ++i;
   digitalWrite(Clock_pin, HIGH);
   delay(3);
   digitalWrite(Clock_pin, LOW);
   delay(3);
   Serial.println(i);
}
else
{
   digitalWrite(Clock_pin, LOW);
}

adafruit_support_bill
 
Posts: 79109
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Control with if statement

by Old_Man_Robo on Tue Mar 10, 2020 1:38 pm

That did the trick. Thanks!

Old_Man_Robo
 
Posts: 6
Joined: Wed Feb 26, 2020 1:57 pm

Please be positive and constructive with your questions and comments.