File size: 1,950 Bytes
6ca05e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | /// EMBROIDOTRON SETUP ///
import processing.serial.*;
Serial arduino;
int s1 = 0;
int s2 = 0;
/// DEBUGGING BOOLEANS ////
boolean serialConnected = false; // for testing without connection to arduino set to false
void setup() {
size(800, 800);
// SETUP EMBROIDERY COMMUNICATIONS
if (serialConnected) {
try {
//// CHANGE PORT NAME TO UNO PORT ////// <------------------------------------------------ CHANGE HERE ----------------------
String portName = "COM9";
////////////////////////////////////////
//// SERIAL COMMUNICATION SETUP/////////////
arduino = new Serial(this, portName, 115200);
arduino.bufferUntil(10);
}
catch(Exception e) {
println("ERROR WITH SERIAL CONNECTION: check that the device is connected properly and that you are using the correct port name");
println("See the portName variable (~line 55) to update port name");
exit();
}
}
}
void draw() {
// we draw a green circle on top of the current needle down
fill(0, 255, 0);
pushMatrix();
translate(width/2, height/2);
ellipse(s1, s2, 3, 3);
popMatrix();
if (serialConnected==false) {
updatePoints();
delay(500);
}
}
////////////////////// SERIAL COMS HELPERS ///////////////////////////////////////////
void serialEvent(Serial myPort) {
if (serialConnected) {
// read a byte from the serial port:
String inBytes = arduino.readString();
if (inBytes.charAt(0) == 'U') {
write(s1, s2); // send new coordinates to stepper motor
updatePoints(); //update coordinates for next send
} else {
println(inBytes);
}
}
}
void updatePoints() {
if (s1 < 600) {
s1 += 4;
}
if (s1 < 600) {
s2 += 4;
}
}
void write(int s1, int s2) {
String message = str(s1) + " " + str(s2) + "\n";
arduino.write(message);
}
////////////////////// END SERIAL COMS HELPERS /////////////////////////////////////////////////////////
|