Arduino:
For this part, the circuit will sent two parameter to the port. One is analogRead()
from Sliding rheostat, which will later control the process of the video like a slidebar! The other one is digitalRead()
from the bottun, sending 1 or 0 to change the speed level.
/* SendMultipleValues Reads an analog input on pin 0, and sends a record that contains two values: 1. Value of `millis()`, modulo 1024. 2. The analog value that is read from pin 0. If you attach a potentiometer to pin 0, you can control this value by moving the pot. This sketch pairs well with the RecieveMultipleValues example from the Processing SerialRecord library <https://osteele.github.io/Processing_SerialRecord/>. Things to try: - Connect a second potentiometer to the Arduino, and send the values from both potentiometers, instead of send a value that is based on `millis()`. - Send the value from another sensor, such as temperature or proximity. by Oliver Steele, 2020-2022 This example code is in the public domain. */ #include "SerialRecord.h" // Change this number to send a different number of values SerialRecord writer(2); void setup() { Serial.begin(9600); } void loop() { int sensorValue1 = analogRead(A0); int sensorValue2 = digitalRead(A1); writer[1] = sensorValue2; writer[0] = sensorValue1; writer.send(); // This delay slows down the loop, so that it runs less frequently. This can // make it easier to debug the sketch, because new values are printed at a // slower rate. delay(10); }
Processing:
Here is a typical bug I met before. But last time, I focused too much on debugging, so I forgot to record this problem. For the bug, I remember the first time I run the given sample code of “ReceiceMultipleValues”, I did no change on the code but it failed to run. I also failed to find a brief and proper solution by googling.
Therefore, I tried to study the instructions given by Processing itself. When I slided up to the head of console information, I found hints that Processing use [0] [1][2] with the name of ports. Then it suddenly dawned on me that since there are more than one serial ports connected with my laptop, they are automatically grouped as an array. The extra thing I have to do is add the index of the port I want to connect into the code.
Processing code: (the video comes from https://pixabay.com/zh/videos/ )
/** * Example sketch for the SerialRecord library for Processing. * * Receives two integers from the serial port, and uses them to control the x * and y position of a circle on the canvas. */ import processing.serial.*; import osteele.processing.SerialRecord.*; import processing.video.*; Movie myMovie; Serial serialPort; SerialRecord serialRecord; int preValue2=1; float newSpeed=1.0; int isPlaying=1; float preValue1=0.0; void setup() { size(640, 360); String serialPortName = SerialUtils.findArduinoPort(2); 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); //myMovie = new Movie(this, "Jing'An.mp4"); myMovie = new Movie(this, "dog.mp4"); myMovie.loop(); } void draw() { background(0); serialRecord.read(); int value1 = serialRecord.values[0]; int value2 = serialRecord.values[1]; float progress = map(value1, 0, 1024, 0, 1); float bar=map(value1, 0, 1024, 0, width); //float y = map(value2, 0, 1024, 0, height); //circle(x, y, 20); if (myMovie.available()) { myMovie.read(); } if (value1 != preValue1) { //myMovie.jump(0); myMovie.jump(myMovie.duration()*progress); //myMovie.jump(random(myMovie.duration())); } if (value2==1 && preValue2==0) { newSpeed=(newSpeed+1.0)%2.1+0.5; } preValue2=value2; myMovie.speed(newSpeed); preValue2=value2; preValue1=value1; image(myMovie, 0, 0, width, height); float rectWidth=10.; rect(0, height-rectWidth, width, rectWidth); circle(bar, height-rectWidth, rectWidth); } void mousePressed() { if (isPlaying==1) { isPlaying=0; myMovie.stop(); } else { isPlaying=1; myMovie.loop(); } }
Additional:
There is an issue about delay that I failed to find a solution. Hope someone can help me deal with it!