Task 1:
This task asks us to send the data from the Arduino to the Processing, which I think is really useful to the final project. It requires to make an Etch-A-Sketch machine by using two potentiometers. So, first I began to build the circuit. Then I stated to write codes. While writing codes, I found that in Arduino codes, the data of each potentiometer corresponding to “writer[1]” and “writer[2]”. Expecting these two codes, there also is a “writer[0]” that can not show the datas of the potentiometers. From this task, I think the most important thing is that I learnt how to transfer datas from the Arduino and sent that to the Processing.
Here are videos and photos:
Here are codes:
Arduino:
#include “SerialRecord.h”// Change this number to send a different number of valuesSerialRecord writer(3);void setup() {Serial.begin(9600);}void loop() {int sensorValue1 = analogRead(A0);int sensorValue2= analogRead(A3);writer[0] = millis() % 1024;writer[1] = sensorValue1;writer[2] = sensorValue2;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(100);}
Processing:
import processing.serial.*;
import osteele.processing.SerialRecord.*;Serial serialPort;
SerialRecord serialRecord;
float pre1;
float pre2;void setup() {
size(500, 500);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, 3);
}void draw() {
//background(0);
serialRecord.read();
int value1 = serialRecord.values[1];
int value2 = serialRecord.values[2];
float x = map(value1, 0, 1024, 0, width);
float y = map(value2, 0, 1024, 0, height);
if (value1 != pre1) {
line(x, y, pre1,pre2);
}
pre1 = x;
pre2 = y;
stroke(255);
//line(x, y, );÷
}
Task 2
This task orders us to turn datas from the Processing to the Arduino and I worked with Chaoyue. By using two motors as the rackets and they beat the ball in the Processing picture just like a competition. While coding, I defined when x=0 the Processing will send “0”, and when x=width the Processing will send “1”. Then the left or right motor will move if they receive “0” or “1”. In the process, I found that motors will move no matter they receive “0” or “1”. To deal with this problem, I int a new sign “tell” and put it before the “if” so that in one work the data will be read just by one time. Then when x=0, the left motor move and when x=width, the right motor move.
Here is video:
Here are codes:
Arduino:
int tell = -1;
#include “SerialRecord.h”
#include <Servo.h>Servo left; // create servo object to control a servo
Servo right; // create servo object to control a servo// Change this number to the number of values you want to receive
SerialRecord reader(1);void setup() {
Serial.begin(9600);
left.attach(9);
right.attach(8);}
void loop() {
if (reader.read()) {
tell = reader[0];
}if (tell == 0) {
right.write(120);
delay(300);
right.write(0);
tell = -1;
}if (tell == 1) {
left.write(120);
delay(300);
left.write(0);
tell = -1;
}
}
Processing:
\float i=0;
int j = 1;
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, 1);
}void draw() {
background(0);
fill(255);
ellipse(i,height/2,60,60);
i=i+20*j;
if(i<=0){
serialRecord.values[0] = 1;
serialRecord.send();
j = 1;
}if(i>=width){
serialRecord.values[0] = 0;
serialRecord.send();
j = -1;
}
}