Skip to main content

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.

Full Code

So, for this sketch, the full code would be as below:

import processing.serial.*;
import processing.embroider.*;
PEmbroiderGraphics E;

Serial myPort;  // Create object from Serial class
String val = "0";

void setup() {
  size(600, 600); 
  E = new PEmbroiderGraphics(this, width, height);
  
  // change this line you want a different file type
  String outputFilePath = sketchPath("filename.pes"); 
  E.setPath(outputFilePath); 
  
  println(Serial.list());
  String portName = Serial.list()[6]; //change the 0 to a 1 or 2 etc. to match your port
 
  
  myPort = new Serial(this, portName, 9600);
  
}

void draw(){
  background(200);
  E.beginDraw(); 
  E.clear();
  E.noFill();
  
  // If data is available, rad and store in val
  if ( myPort.available() > 0) {  
    val = myPort.readStringUntil('\n'); 
  }
  
  
  float r = map(float(val),0,800,0,300);
  E.circle(width/2,height/2,r);
  

  //----------  
  // Visualization and export:
  // NOTE: Leave optimize() and endDraw() commented out,
  // until you are ready to export the embroidery file! 
  // Don't forget to un-comment them when you want to export!
  //
  // E.optimize(); // VERY SLOW, but essential for file output! 
  E.visualize(true, true, false);   // Display the embroidery path on-screen.
  // E.endDraw();  // Actually writes out the embroidery file.
}

-->