Task 1:
In Task 1, we made a processing Etch-A-Sketch, the mechanism of which is basically to make drawings by using two controllers that controls the x and y coordinates of your pen.
I first studied into the code of SendMultipleValues and ReceiveMultipleValues. After that, I grasped the rules for me to make serial communications.
Building up a circuit that includes two potentiometers allows Arduino to read two analog inputs that stands for the x and y coordinates. After building it, I started to modify the sample code. The process was simple. Simply changing the PIN number and variable would work.
#include "SerialRecord.h" SerialRecord writer(2); void setup() { Serial.begin(9600); } void loop() { int x = analogRead(A0); int y = analogRead(A1); writer[0] = x; writer[1] = y; writer.send(); delay(50); }
For processing, it was a bit more complicated. I added a line function, in which the first parameter is the previous point it reads, and the second parameter is the present point, so that it could constantly draw lines based on the values provided by the two potentiometers.
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; float prev_x = 0; float prev_y = 0; float x = 0; float y = 0; void setup() { size(600, 600); serialPort = new Serial(this, 'COM8', 9600); serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { serialRecord.read(); int value1 = serialRecord.values(0); int value2 = serialRecord.values(1); y = map(value1, 0, 255, 0, height); x = map(value2, 0, 255, 0, width); line(prevx,prevy,x,y); prevx = x; prevy = y; }
Here is what I eventually achieved:
The interaction is really interesting, for it could visualize what the user does to the two potentiometers. However, I think this interaction is not creative enough, for it is mainly an Arduino version of what is known as Etch-A-Sketch.
Task 2:
In this task, I made a more interesting combination between Arduino and Processing, working together with Jessie Nie.
We first created an animation, in which a ball bounces back and forth from the left edge to the right edge of our computer screen. Then, we send the x coordinate of the ball to Arduino. After receiving it, Arduino would make two servos move when the x coordinate reaches its maximum or 0.
This is our code for Processing:
import processing.serial.*; import osteele.processing.SerialRecord.*; float x = 0; float y; int speed = 10; Serial serialPort; SerialRecord serialRecord; void setup() { fullScreen(); y = height/2; serialPort = new Serial(this, "COM8", 9600); serialRecord = new SerialRecord(this, serialPort, 1); } void draw() { background(0); circle(x, y, 20); x = x + speed; if(x > width){ speed = -10; serialRecord.values[0] = 1; serialRecord.send(); } if (x < 0){ speed = 10; serialRecord.values[0] = 0; serialRecord.send(); } delay(50); }
This is our code for Arduino:
#include "SerialRecord.h" #include <Servo.h> Servo myservo1; Servo myservo2; SerialRecord reader(1); void setup() { myservo1.attach(8); myservo2.attach(9); Serial.begin(9600); } void loop() { reader.read(); Serial.println(reader[0]); if(reader[0] == 0){ myservo1.write(90); delay(500); myservo1.write(0); } if(reader[0] == 1){ myservo2.write(90); delay(300); myservo1.write(0); } }
The servo caused us a lot of problems. At first, it did not move at all. We adjusted the code in processing to slow down the pace of sending and receiving values. After this modification, the whole artifact worked well.
I think this interaction is not complete yet. It only involves the computer itself, which is basically Arduino interacting with Processing. Therefore, I think it is not completed.