Today, I made a potentiometer controlled controller for the speed of the playback of a video. Depending on the value of the potentiometer, the video would speed up, slow down, or even play backwards. What sounded like an easy task was surprisingly difficult because if even one line of code was wrong, then the whole program wouldn’t work. This lead me to realize how technology that seems simple on the surface is actually made up of many little systems that work together in convoluted ways to make something work. In my project, the technology is the whole system that controls the video playback speed and the little systems are each individual lines of code in both Arduino and Processing, as well as the hardware components from the wires to the potentiometer. If even one system is out of place, the whole technological device would never work.
Processing code:
// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing
/*
* Based on the readStringUntil() example by Tom Igoe
* https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
*/
import processing.serial.*;
import processing.video.*;
Movie mov;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/
void setup() {
size(640, 480);
setupSerial();
frameRate(30);
mov = new Movie(this, “explode.mov”);
mov.speed(4.0);
mov.loop();
}
void draw() {
if (mov.available()) {
mov.read();
}
image(mov, 0, 0);
updateSerial();
printArray(sensorValues);
mov.speed(map(sensorValues[0], 0, 1023, -4.0, 4.0));
}
void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index number of the port.
myPort.clear();
// Throw out the first reading,
// in case we started reading in the middle of a string from the sender.
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;
sensorValues = new int[NUM_OF_VALUES];
}
void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), “,”);
if (serialInArray.length == NUM_OF_VALUES) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}
Arduino code:
// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1 = analogRead(A0);
// keep this format
Serial.print(sensor1);
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}