Stepper motor backward movement - I'm lost

Hi all,

I have a fairly simple task and worked quite a bit on it, but not successful, so I figured I'll try my luck here. I have a nema stepper with 1.8°/step attached to an adafruit shield (v2 I think) and an arduino (uno). The motor is supposed to move fwd or bkwd depending on the sign of the number I input and possibly the fastest possible way, so if in = 345°- > in = - 15° or vice versa if in = -190° -> in = 170°. Does anyone see an obvious mistake, the motor moves only in one direction?
Thx in advance.

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}


int i = 0;
int i2 = 0;
int i3 = 0;
int i4 = 0;
int result = 0;
int steps = 0;
int sign = 1;
void loop() {
 int result = 0;
 i2= 0;
sign = 1;
 while (Serial.available() > 0) 
 {  
    i = Serial.read();
    if ((i >= 48)&&(i <=57))
    {
      i = i-48;
      result = result*10+i;
      Serial.print("I received: ");
      Serial.println(result, DEC);
      i2 = 1;
    }
else if (i == 45)
	{sign = -1;}
    
    delay(50);
 }
 if (result <0)
 {
  result = 0;
  i = 0;
 }
 if (i2 == 1)
 {	while (result > 360) // only single rotations
	{result = result - 360}

	if((result > 180) && (sign ==-1)) // e.g. -190° = 170° -> faster
	
	{result = result + 360;}
	
	else if ((result > 180) && (sign == 1)) // e.g. 210° = -150° -> faster

	{result = result - 360;}
	else
	{result = result;}
	
	result = result*sign;
    result = result/1.8; //conversion from degree to step
    steps = result;
    
    if(steps == 0)
    {
      Serial.print("won't move");
    }
    if (steps > 0)
    {
      //Serial.println(steps, DEC);
      Serial.print('forward');
      myMotor->step(steps, FORWARD, MICROSTEP);
    }
    else
    {
      steps = steps *(-1);
      Serial.print('backward');
      //Serial.println(steps, DEC);
      myMotor->step(steps, BACKWARD, MICROSTEP);
    }
 }
}

Please provide a link to the motor you are using. Not all - even not most - stepper motors are adequate for the adafriut V2 motorshield. The word 'nema' doesn't say anything about the technical data of the motor.
An post a wiring scheme how you connected it.

Double quotes here.

Make it easy on yourself

    i = Serial.read();
    if ((i >= '0')&&(i <='9'))
    {
      i -= '0';

Ok, apparently it works after all. I'll look up the specs to be complete, but thanks.

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