Task: Make a Processing Etch a Sketch
Hardware:
First off, I need to connect the potentiometer to the Arduino so that Arduino can send serial code values to processing in order for the potentiometer to effect values in processing rather than straight into Arduino.
Reference code: Continuous lines (on Processing)
void setup() {
size(640, 360);
background(102);
}
void draw() {
stroke(255);
if (mousePressed == true) {
line(mouseX, mouseY, pmouseX, pmouseY);
}
}
Coding Process:
After connecting the potentiometer and serial values into processing, its time to code what happens with those serial values in the program. To do this, I used a reference code put in the recitation 8 website.
In this video I’m checking if the potentiometer serial works/connects with processing by using an example from class.
At first there were flaws with my code because I defined my px and py as 0 which changed px = x and py = y values. What I did to change this is that instead of doing px = x and py = y I just coded them up in float to float px & float py. After this everything started working perfectly!
My Final Code:
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
float px;
float py;
void setup() {
size(500, 500);
String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 9600);
// If the Arduino sketch sends a different number of values, modify the number
// `2` on the next line to match the number of values that it sends.
serialRecord = new SerialRecord(this, serialPort, 2);
px = 0;
py = 0;
}
void draw() {
serialRecord.read();
int value1 = serialRecord.values[0];
int value2 = serialRecord.values[1];
stroke(255);
float x = map(value1, 0, 1024, 0, width);
float y = map(value2, 0, 1024, 0, height);
line(x,y, px, py);
px = x;
py = y;
}
Reflection:
Overall, the most difficult part of this recitation for me personally was fixing the bug in the code I mentioned previously. Connecting the potentiometer to processing initially was difficult but once you figure out how to use the serial values there is lots of potential to do cool things with it!
Leave a Reply