/
Embroidotron_LiveStitch_Keypressed.pde
142 lines (117 loc) · 3.27 KB
/
Embroidotron_LiveStitch_Keypressed.pde
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/// EMBROIDOTRON SETUP ///
import processing.serial.*;
Serial arduino;
int s1 = 0;
int s2 = 0;
int totalSteps = 0;
PVector zeroPoint;
/// DEBUGGING BOOLEANS ////
boolean doSend = true; // for testing without actually sending points set to false (motors will not move if false)
boolean serialConnected = true; // for testing without connection to arduino set to false
boolean penPlotter = false;
void setup() {
size(800, 800);
// SETUP EMBROIDERY COMMUNICATIONS
zeroPoint = new PVector(width/2,height/2);
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();
}
} else {
doSend = false;
}
}
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:
//println("\n\nNew Serial Event");
String inBytes = arduino.readString();
if (inBytes.charAt(0) == 'U') {
// send new coordinates to stepper motor
write(s1, s2);
//update coordinates for next send
updatePoints();
} else {
println(inBytes);
}
}
}
String currentKey;
void keyPressed(){
println("keycode : " + keyCode);
if (keyCode == UP) {
currentKey = "UP";
} else if (keyCode == DOWN){
currentKey = "DOWN";
} else if (keyCode == LEFT){
currentKey = "LEFT";
} else if (keyCode == RIGHT){
currentKey = "RIGHT";
}
}
void updatePoints() {
int offset = 20;
if (currentKey == "UP"){
println("up");
println(currentKey);
//this goes up
s2 -=offset;//= //int((needleDown.y-zeroPoint.y));
} else if (currentKey == "LEFT"){
println("left");
println(currentKey);
//goes left
s1-=offset;
} else if (currentKey == "RIGHT"){
println("right");
println(currentKey);
//right
s1+=offset;
} else if (currentKey == "DOWN"){
println("down");
println(currentKey);
//down
s2 +=offset;//= //int((needleDown.y-zeroPoint.y));
}
totalSteps++; //current step
}
void write(int s1, int s2) {
PVector P = new PVector(s1,s2);
int dir = 1;
P.rotate(PI/4);
if(penPlotter){
dir = -1;
}
String writeMe = str(int(P.x)*dir) + " " + str(int(P.y)*dir) + "\n";
if (doSend) {
arduino.write(writeMe);
//println("Sent s1: " + str(s1));
//println("Sent s2: " + str(s2));
} else {
println("would have sent:");
println(writeMe);
}
}
////////////////////// END SERIAL COMS HELPERS /////////////////////////////////////////////////////////