Categories
Interaction Lab

Recitation 10

In this recitation class, I used a potentiometer to build a circuit and sent its value to processing to control the speed of the video.

I connected one side of the potentiometer to pin2 and connected the other side to 5V and ground. And I use the example of “Send single value” to send the value of the potentiometer to processing. In the processing part, I use “map” and “myMovie.speed” to control the speed of the video. When the value of the potentiometer is smaller, the speed of the video will be slower; when the value is bigger, the speed will become faster.

The code: (Arduino)

#include "SerialRecord.h"

SerialRecord writer(2);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);

  writer[0] = millis() % 1024;
  writer[1] = sensorValue;
  writer.send();

  delay(20);
}

(Processing):
import processing.video.*;
import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;
Movie myMovie;

void setup() {
  background(0);
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 2);

  size(512, 288);
  myMovie = new Movie(this, "Jing'An.mp4");
  myMovie.loop();
}


void movieEvent(Movie movie) {
  myMovie.read();  
}

void draw() { 
  serialRecord.read();
  int value1 = serialRecord.values[0];
  
  image(myMovie, 0, 0);   
  float newSpeed = map(value1, 0, width, 0.1, 5);
  myMovie.speed(newSpeed);
} 

Leave a Reply

Your email address will not be published. Required fields are marked *