/* 
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);

int user_input;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  AFMS.begin();  // create with the default frequency 1.6KHz
  myMotor->setSpeed(50);
  Serial.println("Enter # of steps to move");
}

void loop() {
  while(Serial.available()){
    Serial.println("Enter # of steps to move");
    user_input = Serial.parseInt(); //Read user input and trigger appropriate function
    // Serial.println((float)user_input);
    if(user_input > 0) {
      Serial.print("Moving ");
      Serial.print(user_input);
      Serial.println(" steps forward");
      myMotor->step(user_input, BACKWARD, DOUBLE); 
      // microstep
    }
    else if (user_input < 0) {
      Serial.print("Moving ");
      Serial.print(-user_input);
      Serial.println(" steps backward");
      myMotor->step(-user_input, FORWARD, DOUBLE);
    }
    else
    {
      // Serial.println("Invalid option entered.");
    }
    myMotor->release();

  }
}