Task #1
Step 1: Use Arduino to send two analog values to Processing
To send two analog values from Arduino to Processing via serial communication using potentiometers, I first build the circuit. There is nothing hard or special in this step, so I moved on quickly. Then, I opened the “SendMultiple value” example and write a sketch that reads the two serial values. I uploaded the code to the Arduino board and tested whether the code and the potentiometers are working. The result showed that both of them are working; when I twisted each of the potentiometers, the serial values shown on the screen changed.
Code
#include "SerialRecord.h" // Change this number to send a different number of values SerialRecord writer(2); void setup() { Serial.begin(9600); pinMode(A0, INPUT); pinMode(A1, INPUT); } void loop() { int sensorValue1 = analogRead(A0); int sensorValue2 = analogRead(A1); writer[0] = sensorValue2; writer[1] = sensorValue1; writer.send(); // This delay slows down the loop. This can make it easier to debug the // program. delay(10); }
Step 2: Processing Sketch that reads two analog values from Arduino (A Circle)
This step went fast and smoothly too. I wrote a Processing sketch that modify the the circle’s x and y values based on the input values of potentiometers from Arduino by using “RecieveMultipleValue” example. The interaction in this task took place when I, as a user, twisted the potentiometers to change the position of the circle on the screen. After I twisted the potentiometer, the serial values on Arduino was sent to Processing, the Processing then process the values and modified the position of the circle through x and y of the circle.
Code:
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; void setup() { fullScreen(); 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); } void draw() { background(0); serialRecord.read(); int value1 = serialRecord.values[0]; int value2 = serialRecord.values[1]; float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); fill(255); circle(x, y, 60); }
Step 3: Modify code to use a line instead
I was a little confused on how to code this step by using the reference code given on the recitation webpage since we do not have line values that has a similar concept as pmouseX and pmouseY. Fortunately, our learning assistant clarified the coding method for this step, and I learned that I have to set and use previous values for the first x and y value of the line. The rest of the coding process went smoothly for me. The interaction in this step is similar to what we had with the circle. I twisted the potentiometers, then Arduino send the values to Processing, and after processing the values, I was able to draw the lines on the screen using the potentiometers.
Code:
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; float preValuex; float preValuey; void setup() { fullScreen(); preValuex = 0; preValuey = 0; background(0); 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); } void draw() { serialRecord.read(); int value1 = serialRecord.values[0]; int value2 = serialRecord.values[1]; float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); stroke(255); strokeWeight(20); line(preValuex, preValuey, x, y); preValuex= x; preValuey= y; }
Task#2
I worked on the Processing code for Task#2 with Melissa during the class, however, we didn’t get to finish the task, so I paired up with Smile after the recitation to finish task#2. Coding process for bouncing ball on Processing went really well as I have done something similar as this for my previous homework assignment. However, Smile and I faced a few problems with the code for the servo on Arduino. First, we tested the initial code we wrote on the Arduino and one of the servos went crazy. We asked help from Iris, and she pointed out that we forgot to add “serialRecord.send()” at the end of the code. After changing the code and running it again, we found that only one of the servo was moving. So it took us a few more minutes to figure out this problem. It turns out that Arduino does not read the negative values for “servo write”. We modify the code by changing the negative value into positive, and the code finally worked. The interaction in this step took place between the Arduino and Processing. The values form the Processing of the circle was sent to Arduino, and Arduino moved the servo according to the values. This interaction is fun and interesting for me, each time the circle touched the screen, it bounced back at the same time when the arm of the servo swiped once, and it looked just like the arms of the servo is playing bounce ball with each other.
Code for Processing:
int s= 5; int x= 500; import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; void setup() { fullScreen(); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { background(0); x=x+s; circle(x, height/2, 145); if (x==width){ s = -5; serialRecord.values[1]= 0; } if(x != width){ serialRecord.values[1]= 1; } if (x==0){ s = 5; serialRecord.values[0]=0; } if (x != 0){ serialRecord.values[0]=1; } serialRecord.send(); }
Code for Arduino:
#include "SerialRecord.h" #include // Change this number to the number of values you want to receive SerialRecord reader(2); Servo servo1; Servo servo2; void setup() { Serial.begin(9600); servo1.attach(8); servo2.attach(9); servo1.write(0); servo2.write(0); } void loop() { if (reader.read()); { if (reader[0] == 0) { servo1.write(120); delay(200); servo1.write(0); } else { servo1.write(0); } if (reader[1] == 0) { servo2.write(120); delay(200); servo2.write(0); } else { servo2.write(0); } } }