Workshop: Using Arduino with PEmbroider
coming soon...
2. Arduino Code
The setup for the arduino code is nice and simple, we just need to read our value then print it to serial. Make sure you are using println() so each value prints on a new line.
void setup() {
Serial.begin(9600);
}
void loop() {
int val = analogRead(A0);
Serial.println(val);
delay(100);
}
Once you have uploaded this, make sure that the Serial Monitor is closed. If left open, processing won't be able to read the data.
3. Processing Code
There are a few things that we need to set up in this part of the code.
Import libraries and create objects
First, we need to import the correct libraries and create our Serial object and a variable to store the value from the Arduino (which will be a string).
import processing.serial.*;
import processing.embroider.*;
PEmbroiderGraphics E;
Serial myPort; // Create object from Serial class
String val = "0"; //Create variable to hold value from Arduino
Setup
Within the setup() function, we will find and connect to the correct serial port.
println(Serial.list());
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
The first line here will print all the available ports. Using this, you can then select the same one that your Arduino is connecting to by changing the number in the square brackets on the second line.
Draw
Inside the draw() function we will then read the data that the Arduino is sending to us.
if(myPort.available() > 0){
val = myPort.readStringUntil('\n');
}
We can then use this value as a variable within our sketch. For example, as the radius of a circle.
float r = map(float(val),0,800,0,300);
E.circle(width/2,height/2,r);
The value is read as a string so we have to cast it to float in order to use it.
-->