control stepper motor angle

I need to control stepper motor with arduino and processing. I designed an interface with processing. I can move the stepper motor CW and CCW with processing but it doesn’t turn as much as angle value I want, I entered the value on interface but it doesn’t work. It always turn fixed angle like 180. What can I do to fix this? (interface control by mouse) ( I use Arduino Uno, motor shield, Nema 17 stepper motor)


#include <AFMotor.h>
AF_Stepper motor(200, 2);
int i=0;
void setup() {

void loop(){
if(Serial.available() > 0) {
  int value  = Serial.parseInt();
char stepState =;
if(stepState == 'a'){
// for(int j=0;j<=value;j++)
motor.step(value,FORWARD, SINGLE);

if(stepState == 'b'){
// for(int j=0;j<=value;j++)
motor.step(value, BACKWARD, SINGLE);




import controlP5.*;
import processing.serial.*;
Serial myPort;
String myText="";
boolean bas=false;
boolean acibas=true;
int rectX, rectY; // Position of square button
int rectSize = 120;     // Diameter of rect
color rectColor, circleColor, baseColor;
color rectHighlight, circleHighlight;
color currentColor;
boolean rectOver = false;
boolean rectOver2 = false;
int yon=0;
int adim=100;
String aci="200";
int i=0;
 char[] dizi;
ControlP5 cp5;

void setup() {
 size(600, 550);
 rectColor = color(0);
 rectX = 350;
 rectY = 80;
 rect(rectX, rectY, rectSize, rectSize);
 rect(130, 80, rectSize, rectSize);
 cp5 = new ControlP5(this);
 cp5.addTextfield("ANGLE").setPosition(75, 300).setSize(200, 80).setFont(createFont("arial",32)).setAutoClear(false);
 text("C-CW", 130, 60);
 text("CW", 350, 60);
 cp5.addBang("SUBMIT").setPosition(325, 300).setFont(createFont("arial",32)).setSize(160, 80); 
 myPort = new Serial(this, "COM3", 9600);

void draw() {
 update(mouseX, mouseY);
 myPort.write("b" + adim);
  myPort.write("a" + adim);
void serialEvent (Serial myPort){
myText = myPort.readStringUntil('\n');
void Submit() {
 String a = cp5.get(Textfield.class,"ANGLE").getText();
int IntfromCharArray(char[] myChar) {
 int total=0;
 int carpim=1;
 for (int i=myChar.length-1;i>0;i++){
   if (myChar[i]!=' '){
   for(int j=0;j<i;j++)
return total;

void update(int x, int y) {
 if ( overRect(rectX, rectY, rectSize, rectSize) ) {
   rectOver = true;
   rectOver2 = false;
 } else if( overRect(130, 80, rectSize, rectSize) ){
   rectOver2 = true;
   rectOver = false;
 } else {
  rectOver = rectOver2 = false;

void mousePressed() {
 if (rectOver) {
 }else if (rectOver2) {


boolean overRect(int x, int y, int width, int height)  {
 if (mouseX >= x && mouseX <= x+width && 
     mouseY >= y && mouseY <= y+height) {
   return true;
 } else {
   return false;

And now with code tags, please.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Also use the Arduino AutoFormat tool to indent your code for easier reading.

I don’t know what you are sending to the Arduino but whatever it is this is a crap way of receiving it

    if(Serial.available() > 0) {
        int value  = Serial.parseInt();
        char stepState =;

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

Also, you should separate the business of receiving data from the business of controlling your motor. It will make testing and debugging much easier. Have a look at Planning and Implementing a Program

I don’t know Processing - I use Python.