Hello,
I am working on a delta robot controlled with stepper motors through arduino uno.
I found a code online (Code) that allows me to use Arduino processing IDE to control the motors. But I am very new to arduino so I am facing some difficulties.
My problem: I am trying to send position XYZ to my arduino but there is a problem I am facing. when I am not moving the end effector to a new position it keeps sending & printing the old position to my arduino which I no longer need and that causes my stepper motors to keep on moving even when the end effector is stand still at the same orginal position.
The following is a picture the proccessing IDE program and as you can see the end effector is the area highlighted in red:
Right now it is printing the postion it is on several times and when I move the end effector to a new position it keeps on printing and sending this position several times as well until I move to next postion. Here is a photo of the positions being repeatedly printed in the processing IDE:
Here is the proccessing IDE code:
import processing.serial.*;
Serial myPort; // The serial port:
int XXX = 0; // x zero
int YYY = 0; // y zero
int ZZZ = -180; // z zero (the robot is upsidedown)
int serialBegin = 255;
void setup() {
size(600,600);
// open serial port
//myPort = new Serial(this, Serial.list()[1], 9600);
myPort = new Serial(this, "COM4", 9600);
frameRate(100);
noCursor();
}
void draw() {
background(255);
//triangle draw
triangle(width/2, height, 0, 200, width, 200);
strokeWeight(3);
//line draw
line(300,200,mouseX,mouseY);
line(150,400,mouseX,mouseY);
line(450,400,mouseX,mouseY);
// X and y value are scaled in order to fit the robot space work
XXX=round(float((mouseX-300))*2/5);
YYY=round(float((mouseY-300))*2/5);
// if mouse is pressed the Z value change
if (mousePressed && (mouseButton == LEFT)) {
ZZZ -= 1;
}
if (mousePressed && (mouseButton == RIGHT)) {
ZZZ += 1;
}
String xPos;
String yPos;
String zPos;
//write X value in serial port
xPos="X";
if (XXX > 0)
xPos += '+';
else
xPos += '-';
if (abs(XXX) < 100)
xPos += '0';
if (abs(XXX) < 10)
xPos += '0';
xPos += abs(XXX);
// write Y value in serial port
yPos="Y";
if (YYY > 0)
yPos += '+';
else
yPos += '-';
if (abs(YYY) < 100)
yPos += '0';
if (abs(YYY) < 10)
yPos += '0';
yPos += abs(YYY);
// write Z value in serial port
zPos="Z";
if (ZZZ > 0)
zPos += '+';
else
zPos += '-';
if (abs(ZZZ) < 100)
zPos += '0';
if (abs(ZZZ) < 10)
zPos += '0';
zPos += abs(ZZZ);
// write x,y,z in the serial port
myPort.write(xPos);
myPort.write(yPos);
myPort.write(zPos);
// write x,y,z in the monitor
println(xPos);
println(yPos);
println(zPos);
}
I am trying to use if condition to solve this problem and I also tried to search for a solution online but I still can not find one so far. so I thought I would ask here maybe someone have a solution that will help me and save me some time.
Here is my attempt that I am working on currently but of course this attempt wont work yet because variables xPos, yPos, zPos are not initialized.
String xPos;
String yPos;
String zPos;
//write X value in serial port
if(xPos!="X"){
xPos="X";
if (XXX > 0)
xPos += '+';
else
xPos += '-';
if (abs(XXX) < 100)
xPos += '0';
if (abs(XXX) < 10)
xPos += '0';
xPos += abs(XXX);
}
// write Y value in serial port
if(yPos!="y"){
yPos="Y";
if (YYY > 0)
yPos += '+';
else
yPos += '-';
if (abs(YYY) < 100)
yPos += '0';
if (abs(YYY) < 10)
yPos += '0';
yPos += abs(YYY);
}
// write Z value in serial port
if(zPos!="z"){
zPos="Z";
if (ZZZ > 0)
zPos += '+';
else
zPos += '-';
if (abs(ZZZ) < 100)
zPos += '0';
if (abs(ZZZ) < 10)
zPos += '0';
zPos += abs(ZZZ);
}
// write x,y,z in the serial port
myPort.write(xPos);
myPort.write(yPos);
myPort.write(zPos);
// write x,y,z in the monitor
println(xPos);
println(yPos);
println(zPos);
}