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)
ARDUINO CODES
#include <AFMotor.h>
AF_Stepper motor(200, 2);
int i=0;
void setup() {
motor.setSpeed(48);
Serial.begin(9600);
}
void loop(){
if(Serial.available() > 0) {
int value = Serial.parseInt();
char stepState = Serial.read();
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);
}
}
}
PROCESSING CODES
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);
textSize(32);
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);
myPort.bufferUntil('\n');
}
void draw() {
update(mouseX, mouseY);
if(bas){
if(yon==1){
myPort.write("b" + adim);
}
if(yon==2){
myPort.write("a" + adim);
}
bas=false;
}
}
void serialEvent (Serial myPort){
myText = myPort.readStringUntil('\n');
}
void Submit() {
String a = cp5.get(Textfield.class,"ANGLE").getText();
print(a);
}
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++)
carpim=carpim*10;
total=total+((int)myChar[i]*carpim);
}
}
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() {
bas=true;
if (rectOver) {
yon=2;
}else if (rectOver2) {
yon=1;
}
else{
yon=0;
}
}
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;
}
}