I’m trying to control stepper motor by arduino and processing. I want to control the step value of motor which I entered the interface textbox but it doesnt work and program give error always.
Arduino Code
#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'){
motor.step(value, FORWARD, SINGLE);
}
if(stepState == 'b'){
motor.step(value, BACKWARD, SINGLE);
}
}
}
Processing Code
import controlP5.*;
import processing.serial.*;
Serial myPort;
boolean bas=false;
boolean acibas=true;
int rectX, rectY; // Position of square button
int rectSize = 120; // Diameter of rect
color rectColor, baseColor;
color rectHighlight;
color currentColor;
boolean rectOver = false;
boolean rectOver2 = false;
int yon=0;
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() {
int adim;
String Sadim = cp5.get(Textfield.class,"ANGLE").getText();
adim = Integer.parseInt(Sadim);
update(mouseX, mouseY);
if(bas){
if(yon==1){
myPort.write("b");
}
if(yon==2){
myPort.write("a");
}
bas=false;
}
}
void controlEvent(ControlEvent deger) {
if(deger.isAssignableFrom(Textfield.class)) {
println("controlEvent: accessing a string from controller '"+deger.getName()+"': "
+deger.getStringValue()
);
}
}
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;
}
}