@kll. Thank you very much for your encourage.
I would like to share my codes for other members who may concern about this matter.
When you change '“x” value in processing, motor will turn the corresponding round as its value.
I use Arduino Uno board + driver module TB6560 + Step motor SUMTOR - 57HS7630A (1.8deg,3A)
Processing code:
import processing.serial.*;
import controlP5.*;
import java.util.*;
Serial port; // Create object from Serial class
ControlP5 cp5;
String a, status, direc, old_direc, new_direc;
boolean order;
int x,count ;
float cycle,b =0;
void setup() {
size(200, 200);
noStroke();
smooth();
background(0);
//frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, Serial.list()[0], 9600);
port.bufferUntil(10);
cp5 = new ControlP5(this);
List l = Arrays.asList("trai", "phai");
cp5.addScrollableList("abc")
.setPosition(10, 10)
.setSize(50, 100)
.setBarHeight(20)
.setItems(l)
;
}
void draw() {
background(255);
status = cp5.get(ScrollableList.class, "abc").getLabel();
cycle = 1.0/9.0;
//x = int(cycle);
//loop 50 times to get stable value of cycle
if (count < 50) {
count = count + 1;
b = b + cycle;
}
x = int(b/50*100);
if (status == "phai") {
fill(204); //change color
direc = "R " + x; // and direction is set to Right
//new_direc = direc;
} else if (status == "trai") {
direc = "L " + x;
} else {
direc = "N 0"; // direction is nothing
}
if (mouseOverRect() == true) { // If mouse is over square,
order = true; // turning order = true if (status == "trai" || status == "phai") {
fill(204); // change color and
//direc = "Ln" + x; // direction is set to Left
old_direc = new_direc;
new_direc = direc;
} else { // If mouse is not over square,
fill(0); // change color and
order = false; // turning order = false
old_direc = new_direc;
}
//send information to Arduino if turning order = true
if ( order == true & old_direc != new_direc) {
if (count == 50){
port.write(direc);
old_direc = new_direc;
count = 0;
b = 0;
println(old_direc, " vs ", new_direc);
}
} else {
port.write("N 0");
println("Non");
}
rect(50, 50, 100, 100); // Draw a square
//println(a);
}
//void serialEvent(Serial p) {
// a = p.readStringUntil('\n');
//}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
Arduino code:
int _step = 0, Distance = 0, count; // Record the number of steps we've taken void setup()
int new_loop, old_loop;
String new_dir, old_dir; // Data received from the serial port
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available())
{ // If data is available to read,
new_dir = Serial.readStringUntil(' '); // read it and store it in val
new_loop = Serial.parseInt();
}
if ((new_dir != old_dir) )
{
old_loop = new_loop;
old_dir = new_dir;
_step = old_loop * 3200 / 100;
// Distance = 0;
if (old_dir == "L")
{
digitalWrite (8, LOW);
}
else if (old_dir == "R")
{
digitalWrite(8, HIGH);
}
} else {
_step = 0;
Distance = 0;
}
count = 0;
for (int x = 0; x < _step; x++)
{
digitalWrite(9, HIGH);
delayMicroseconds(200);
digitalWrite(9, LOW);
delayMicroseconds(200);
Distance++; // record this step // Check to see if we are at the end of our move
if (Distance == _step){
Distance = 0;
break;
}
}
if (new_loop != old_loop) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
}
Hope that it will help somebody who may concern!