Week 4_ Serial Communication

This week’s assignment was to construct an example of communicating between the Arduino and Processing.

There are 4 controllers: up, down, left and right and you move a ball with 4 directions. Try not to hit bouncing balls. If it does, the ball becomes bigger and harder to move around.

PROCESS

This is one of the tutorials that I tried which helped us to build a code. The tutorials helped me to understand the Serial communication between Arduino and Processing.

The hardest part of this project was coding within processing language.

From the tutorials that we learned, we decided to make a simple game with a physical controller. The code for processing is based on java script, which is similar with what we learned from P5.js. We spent most of our time on figuring out what is the connection of input and output.

void setup(){
Serial.begin(9600);
}
void loop() {
int sensor=digitalRead(A0);
// Serial.write(sensor/4);

int sensor2=digitalRead(A1);
int sensor3=digitalRead(A2);
int sensor4=digitalRead(A3);

//delay(1000);
Serial.print(sensor);
Serial.print(",");
Serial.print(sensor2);
Serial.print(",");
Serial.print(sensor3);
Serial.print(",");
Serial.println(sensor4);
//delay(1000);
}
code for arduino part (input)
import processing.serial.*;
Serial myPort;
float xPos;
float yPos;
float right=0;
float left=0;
float up=0;
float down=0;
float y;
float xMax=1023;
float xMin=0;
int numEllipses=10;
float diam=20;
float[] bxpos = new float[numEllipses];
float[] bypos = new float[numEllipses];
float[] xMov = new float[numEllipses];
float[] yMov = new float[numEllipses];
float yMax=700;
float yMin=0;
float newdiam=20;
void setup() {
size(1023, 700);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.clear();
myPort.bufferUntil(‘\n’);
initPos();
}
void draw() {
background(0);
fill(255, 0, 0);
if (right==1 && xPos <(xMax-20)) {
xPos=xPos + 10;
} else if (left==1 && xPos > (xMin+10)) {
xPos=xPos — 10;
}
if (up==1 && yPos >(yMin+20)) {
yPos=yPos — 10;
} else if (down==1 && yPos < (yMax-10)) {
yPos=yPos + 10;
}
ellipse(xPos, yPos, newdiam, newdiam);
updatePos();
drawEllipses();
}
void mousePressed() {
initPos();
}
void initPos() {
for (int i = 0; i < numEllipses; i++) {
// select random value for all xpos and ypos
bxpos[i]=random(1023); // random 0 to width
bypos[i] =random(700);// random 0 to height
ellipse(bxpos[i], bypos[i], diam, diam);
// select random value for all xmov and ymov
xMov[i] = random(-1, 1);// random -1 to 1
yMov[i] = random(-1, 1);// random -1 to 1
}
}
void updatePos() {
for (int i = 0; i < numEllipses; i++) {
// add movement to position, this creates animation
bxpos[i] += xMov[i];// xpos = xpos + xmov;
bypos[i] += yMov[i];// ypos = ypos + ymov;
if (bxpos[i] < xMin) {
xMov[i] = -xMov[i];
}
if (bxpos[i] > xMax) {
xMov[i] = -xMov[i];
}
if (bypos[i] < yMin) {
yMov[i] = -yMov[i];
}
if (bypos[i] > yMax) {
yMov[i] = -yMov[i];
}
if ((bxpos[i]-diam) < xPos+(newdiam-20) && xPos-(newdiam-20)<(bxpos[i]+diam) && (bypos[i]-diam)< yPos+(newdiam-20) && yPos-(newdiam-20) < (bypos[i]+diam)) {
//ellipse(xPos,yPos,newdiam+10,newdiam+10);
newdiam=newdiam+1;
}
}
}
void drawEllipses() {
for (int i = 0; i < numEllipses; i++) {
fill(255);
ellipse( bxpos[i], bypos[i], diam, diam);
}
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil(‘\n’);
println(inString);
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the
// resulting substrings into an integer array:
float[] position = float(split(inString, “,”));
// if the array has at least three elements, you know
// you got the whole thing. Put the numbers in the
// map them to the range 0–255:
//xPos = map(position[0], 0, 1023, 0, 255);
if (position.length >=4) {
right = position[0];
left = position[1];
up = position [2];
down = position [3];
}
}
}
code for processing (output)
Like what you read? Give Ashley S. Jang a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.