For task1, I write the codes in the arduino to get the value from the circuit and send to processing like below.
#include "SerialRecord.h"
SerialRecord writer(2);
void setup() {
Serial.begin(9600);
}
void loop() {
int valuex = analogRead(A0);
int valuey = analogRead(A1);
writer[0] = valuex;
writer[1] = valuey;
Serial.println(valuex);
Serial.println(valuey);
writer.send();
delay(10);
}
I also write the codes in the processing that can get the values from arduino to control the position of the circle.
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
void setup() {
size(600, 600);
background(176,131,250);
String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 9600);
serialRecord = new SerialRecord(this, serialPort, 2);
}
void draw() {
serialRecord.read();
int valuex = serialRecord.get(0);
int valuey = serialRecord.get(1);
float x1 = map(valuex,0,1023,0,580);
float y1 = map(valuey,0,1023,0,580);
circle(x1,y1,20);
}
However, it works like below that two circles appears and one of them seems to be the shadow of another one.
I asked help from fellows and professor Rudi and I found that it is because I printed the value in arduino first so the processing is actually receiving extra values from arduino. When errors happen, the circle is on another place. Then I deleted the codes for printing and it works like below.
For drawing lines, I did not know how to store a value first so I asked for help from professor Margret. I finally knew that I need to declare a global variable at the beginning and then use it in the loop, and then I added the below codes before setup().
float px1;
float py1;
I also added these in the loop.
px1 = x1;
py1 = y1;
stroke(255);
strokeWeight(5);
line(x1,y1,px1,py1);
It works well like below. This interaction actually provides users to draw with their hands by rotating the two potentiometers. However, I found that it is difficult for me to draw an entire picture here because it is hard to control how long to draw and when to change directions.
For task2, I worked with my partner Amelia to do this task. I found this interesting that it requires the processing to send messages to arduino instead of sending from arduino to processing. I wrote the codes, while Amelia built the circuits and also helped me with codes. Here is the codes in processing. In this code, I adopted the experiences from drawing lines in the task1 to declare global variables at the beginning. I also used another variable “spd” to change the moving direction of the circle when it touches the edge of the sketch.
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
float x = 0;
float spd=10;
void setup() {
size(600, 600);
String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 9600);
serialRecord = new SerialRecord(this, serialPort, 1);
}
void draw() {
background(176,131,250);
circle(x,height/2,20);
x+=spd;
int move = int (map(x,0,width,0,width));
if (x>=width){
spd= -spd;
}
if (x<=0){
spd= -spd;
}
serialRecord.values[0] = move;
serialRecord.send();
}
Here are the codes in the arduino. I first wrote the code to make servo move back to the original angle inside the if statement and I found that did not work so I put it outside the if statement and it worked well.
#include "SerialRecord.h"
#include
SerialRecord reader(1);
Servo servo1;
Servo servo2;
void setup() {
Serial.begin(9600);
servo1.attach(9);
servo2.attach(10);
pinMode(9, OUTPUT);
pinMode(10,OUTPUT);
}
void loop() {
reader.read();
servo1.write(50);
if (reader[0]>580){
servo1.write(180);
};
servo2.write(180);
if (reader[0]<10){
servo2.write(90);
};
It works like this. The interaction between real-life servo moving combined with balls moving on computer screens is really fascinating. Virtual things are interacting with things on hand.