Making a Drawing Machine using processing and an arduino: help with serial connection
in
Integration and Hardware
•
4 months ago
Hello,
I am making a drawing machine like this–
http://www.cameronzotter.com/mica/work_arduino_drawing.html using Processing to control servos connected to an Arduino. I have set up a sketch in Processing that will set up parameters for the servos to rotate within ( so that the pen will draw within a defined area–the paper) I have established a serial connection but I don't know how to make Processing control the servos through the Arduino software. Please help with the Arduino code!
Here is the Processing code:
import processing.serial.*;
Serial port;
/*Drawing Servo Controller*/
//90 is stopping
// 0 means stop
// 1 means rotate left
// 2 means rotate right
//first value for left servo
//second value for right servo
//00
//10
boolean topRecorded = false;
boolean bottomRecorded = false;
int currentAngleLeft = 0;
int currentAngleRight = 0;
int maxAngleLeft;
int maxAngleRight;
void setup () {
size(300, 400);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[0], 9600);
}
void draw () {
//drawing left servo draw left and right controls
background(125);
pushMatrix();
translate(50, height/2);
fill(255);
rect(0, 0, 30, 30);
text("l up", 0, 0);
fill(0);
rect(0+30, 0, 30, 30);
text("l down", 30, 0);
popMatrix();
//drawing right controls
pushMatrix();
translate(200, height/2);
text("l down", 0, 0);
fill(0);
rect(0, 0, 30, 30);
text("l up", 30, 0);
fill(255);
rect(0+30, 0, 30, 30);
popMatrix();
//draw record burrons
pushMatrix();
translate(width/2, 50);
fill(255, 0, 0);
text("rec top", 0, 0);
rect(0, 0, 30, 30);
fill(0, 255, 0);
text("rec bot", 40, 0);
rect(0+30, 0, 30, 30);
popMatrix();
//draw left servo visual
pushMatrix();
ellipseMode(CENTER);
translate(80, height/2-50);
rotate(radians(currentAngleLeft));
fill(255, 0, 0);
arc(0, 0, 30, 30, 0, PI+HALF_PI, PIE);
popMatrix();
//draw right servo visual
pushMatrix();
ellipseMode(CENTER);
translate(200, height/2-50);
rotate(radians(currentAngleRight));
fill(255, 0, 0);
arc(0, 0, 30, 30, 0, PI+HALF_PI, PIE);
popMatrix();
if (mousePressed) {
if (mouseX>50 && mouseX<80 && mouseY>height/2 && mouseY<height/2+30) {
println("left servo rotate left");
//if we are currently testing for top and bottom can move anyway
if (!topRecorded) {
currentAngleLeft -=1;
} else {
//if we are drawing make sure to trap it
if ( currentAngleLeft > 0) {
currentAngleLeft -=1;
}
}
}
if (mouseX>80 && mouseX<110 && mouseY>height/2 && mouseY<height/2+30) {
println("left servo rotate right");
if (!bottomRecorded) {
currentAngleLeft +=1;
} else {
//if we are drawing cannot go over the maxiumum position
if (currentAngleLeft< maxAngleLeft) {
currentAngleLeft +=1;
}
}
}
if (mouseX>200 && mouseX<230 && mouseY>height/2 && mouseY<height/2+30) {
println("right servo rotate left");
currentAngleRight -=1;
}
if (mouseX>230 && mouseX<260 && mouseY>height/2 && mouseY<height/2+30) {
println("right servo rotate right");
currentAngleRight +=1;
}
}
}
void mousePressed () {
if (mouseX>width/2 && mouseX<width/2+30 && mouseY>50 && mouseY<80) {
println("at top");
topRecorded=true;
currentAngleLeft = 0;
currentAngleRight = 0;
}
if (mouseX>width/2+30 && mouseX<width/2+60 && mouseY>50 && mouseY<80) {
println("at bottom");
if (topRecorded) {
bottomRecorded = true;
maxAngleLeft = currentAngleLeft;
maxAngleRight = currentAngleRight;
println("Max Angle Left: " + maxAngleLeft + ", Max Angle Right: " + maxAngleRight);
}
}
}
1