//Controlling rotation angle/steps of a stepper motor from serial port
#include <AFMotor.h>
int steps = 32;
int ext_step = steps*64;
AF_Stepper motor(ext_step,2);
int Reset = 1024;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("BinuZकेHobbieZ");
motor.setSpeed(10);
motor.step(Reset, FORWARD,SINGLE);
Serial.println("Motor angle reset to centre");
}
void loop()
{
// put your main code here, to run repeatedly:
if(Serial.available() > 0);
{
if (Serial.peek()== ('R'))
{
Serial.read();
int data = Serial.parseInt();
int NegAngle = map(data, 0, 360, 0, 2048);
motor.step(NegAngle, BACKWARD,SINGLE);
Serial.print("Motor turns right ");
Serial.print(data);
Serial.print(" Degrees and steps ");
Serial.println(NegAngle);
}
if (Serial.peek()== ('L'))
{
Serial.read();
int data = Serial.parseInt();
int PosAngle = map(data, 0, 360, 0, 2048);
motor.step(PosAngle, FORWARD,SINGLE);
Serial.print("Motor turns Left ");
Serial.print(data);
Serial.print(" Degrees and steps ");
Serial.println(PosAngle);
}
}
}