Help with Drawing Machine Code
in
Integration and Hardware
•
18 days ago
Hi,
A couple of months ago I made a drawing machine using processing to control my arduino that is hooked up to two continuous rotation servos. I'm trying to use it again but I can't get the processing code to control the arduino. Please help!
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);
//draw record burrons
pushMatrix();
translate(width/2, 50);
//red button
fill(255, 0, 0);
text("down", 0, 0);
rect(0, 0, 30, 30);
fill(0, 255, 0);
text("up", 40, 0);
rect(0+30, 0, 30, 30);
fill(0, 0, 255);
text("stop", 60, 0);
rect(0+60, 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();
}
void mousePressed () {
if (mouseX>width/2 && mouseX<width/2+30 && mouseY>50 && mouseY<80) {
println("up");
port.write(65);
/*topRecorded=true;
currentAngleLeft = 0;
currentAngleRight = 0;*/
}
if (mouseX>width/2+30 && mouseX<width/2+60 && mouseY>50 && mouseY<80) {
println("down");
port.write(66);
/*if (topRecorded) {
bottomRecorded = true;
maxAngleLeft = currentAngleLeft;
maxAngleRight = currentAngleRight;
println("Max Angle Left: " + maxAngleLeft + ", Max Angle Right: " + maxAngleRight);
}*/
}
if (mouseX>width/2+60 && mouseX<width/2+90 && mouseY>50 && mouseY<80) {
println("stop");
port.write(67);
/*if (topRecorded) {
bottomRecorded = true;
maxAngleLeft = currentAngleLeft;
maxAngleRight = currentAngleRight;
println("Max Angle Left: " + maxAngleLeft + ", Max Angle Right: " + maxAngleRight);
}*/
}
}
Arduino Code
#include <Servo.h>
Servo servo_0;
Servo servo_2;
int loopCount=0;
boolean maxRecorded = false;
int maxLoopCount=150;
//String dir = "down";
int dir = 0;
//unsigned int pos[] = {10,90,170,90}; // variable to store the servo position
void setup()
{
pinMode(1,OUTPUT);
servo_0.attach(14); // attaches the servo on pin 0 to the servo object
servo_2.attach(15);
Serial.begin(9600);
}
void loop()
{
if (Serial.available()>0) {
// read the most recent byte (which will be from 0 to 255):
char r = Serial.read();
Serial.println(r);
if (r == 'B') {
dir=-1;
if (maxRecorded == false) {
//maxLoopCount=loopCount;
}
}
else if (r=='A') {
dir=1;
}
else if (r=='C'){
dir=0;
}
}
//servo_0.write(170);
if (dir==1) {
servo_0.write(78);
servo_2.write(98);
if (loopCount < maxLoopCount) {
loopCount++;
}
else {
dir=-1;
Serial.println("reached bottom");
}
}
else if (dir==-1){
if (loopCount>0) {
loopCount--;
servo_0.write(98);
servo_2.write(78);
}
else {
dir=1;
Serial.println("reached top");
}
}
else {
servo_0.write(89);
servo_2.write(89);
}
Serial.println("Loopcount: " + String(loopCount));
//servo_0.write(170);
delay(100);
/*for(int i=0;i<=170;i++){
servo_0.write(100);
//100 makes it go slowly right (down)
delay(25);
}*/
}
/*void moveServoZero () {
if (dir==1) {
servo_0.write(78);
servo_2.write(98);
if (loopCount < maxLoopCount) {
loopCount++;
} else {
dir=-1;
Serial.println("reached bottom");
}
} else if (dir==-1){
if (loopCount>0) {
loopCount--;
servo_0.write(98);
servo_2.write(78);
} else {
dir=1;
Serial.println("reached top");
}
} else {
servo_0.write(89);
servo_2.write(89);
}
*/
1