Recitation 10: Object Oriented Programming workshop – By Anica Yao

Reflection

 I wanted to enhance my skills then apply it to my final project, so I chose this OOP workshop. Firstly, we created a class called “Emoji” in another tab.  In this class, we defined the values,  Emoji ( later I learned that “Emoji” it’s the abbreviation of “Emoji Emoji” adjusted by the developer. ), display(), and move(). After that, in the first tab, we uploaded every new emoji in the setup() function and used them in the draw() function. To improve the codes, we added the mouse press interaction and put all the emojis in an array, which has a different format from the array I learned before. 
After class, I realized this works well when we need to draw things in a same pattern. So maybe we don’t need to do the same things to pictures in our final project. But still, I did some improvements to the codes. 

Processing Codes

ArrayList<Shape> shapeList;

void setup() {
  size(600, 600);
  background(255);
  shapeList = new ArrayList<Shape>();

  //draw the new shape
  for (int i=0; i<50; i++) {
    shapeList.add(new Shape(random(width), random(height), color(0, 0, random(255),200)));
  }
}

void draw() {
  //background(255);  
  for(int i=0; i<shapeList.size();i++){
    Shape temp = shapeList.get(i);
    temp.display(); //same as emojiList[i]
    temp.move(); 
  }
}

void mousePressed(){
  float x =map(mouseX, 0, width, width/4, 3*width/4);
  float y =map(mouseX, 0, height, height/4, 3*height/4);
  shapeList.add(new Shape(x, y, color(random(255), random(255), random(255))));
}
#TabName: Shape
class Shape {
  //only define them w/o value. The default values are all 0 here
  float x, y;
  float size;
  color clr;
  float spdX;

  Shape(float startingX, float startingY, color startingColor) { //without the para, it will be the same;
    //Not x,y, or it will not refer to the one defined at the beginning
    x = startingX;
    y = startingY;
    size = random(50, 200);
    clr = startingColor;
    spdX = random(-10, 10);
  }

  void display() {
    noStroke();
    fill(clr);
    square(x,y,size);
  }

  void move() {
    x += spdX;
    if (x<0 || x>width) {
      spdX = -spdX;
    } 
  }
}

Final Creation

Basically, there are all blue squares moving horizontally. When they touch the edges they will bounce back. When you press your mouse, a new square with random color will pop up and also begin to move.

Last Recitation Interlab Kris

The last recitation reviews the use of map function, and I went to Prof. Moon’s workshop to review Serial Communication. Prof. Moon didn’t leave us a homework, instead, he asks us to review what he taught in class and finish the implementation.

We reviewed the use of serial communication code in class:

Arduino to processing is what we will use in final, so we focus more on it in this blog:


void setup() {
Serial.begin(9600);
}

void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);
int sensor3 = analogRead(A2);

// keep this format
Serial.print(sensor1);
Serial.print(","); // put comma between sensor values
Serial.print(sensor2);
Serial.print(",");
Serial.print(sensor3);
Serial.println(); // add linefeed after sending the last sensor value

// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}


import processing.serial.*;

String myString = null;
Serial myPort;

int NUM_OF_VALUES = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/

void setup() {
size(500, 500);
background(0);
setupSerial();
}

void draw() {
updateSerial();
printArray(sensorValues);

// use the values like this!
// sensorValues[0]

// add your code

//
}

void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[ PORT_INDEX ], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----"
// and replace PORT_INDEX above with the index number of the port.

myPort.clear();
// Throw out the first reading,
// in case we started reading in the middle of a string from the sender.
myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII
myString = null;

sensorValues = new int[NUM_OF_VALUES];
}

void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), ",");
if (serialInArray.length == NUM_OF_VALUES) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}

We understand several important use of it:

1. the change of port numbers
2. the use of comma and println / print
3. different between write and print that is: output bytes / string
4. number of sensor values

map function is to map a value based on a certain range: map(value, r00, r01, r10, r11)
which is handy and the theory behind is also very simple:

it is a linear transformation f(x) = kx + b , where k, b can be calculated by the 4 r’s

Recitation 10: Serial Communication by Isabel Brack

Overview:

In this recitation we went to a workshop on mapping and then using serial communication to connect Arduino with Processing. The serial communication workshop leader had no major requirements for our recitation other performing serial communication, while using a mapping function. So following the instructor’s lead I mapped a potentiometer to the Y coordinate of a moving ellipse and the X coordinate was mouse X. Also, I used a button serial communication to change the color of the ellipse. I then used this serial communication in my final project code like the instructor suggested using buttons to switch the image faces.

This recitation mainly follows exactly what our instructor was doing. First we connected the circuit with the potentiometer and the button. Next we looked at the serial communication code for A to P and altered it to use one digital sensor and one analog. Finally we altered the processing code to map the potentiometer and to use a bullion to control the color of the ellipse which was most helpful as we want to use a button to control Processing in our final project.

Code for moving ellipse:

// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing

/*
 * Based on the readStringUntil() example by Tom Igoe
 * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
 */

import processing.serial.*;

String myString = null;
Serial myPort;


int NUM_OF_VALUES = 2;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;      /** this array stores values from Arduino **/


void setup() {
  size(500, 500);
  background(0);
  setupSerial();
}


void draw() {
  updateSerial();
  printArray(sensorValues);
   background(0);
  float posx= map (sensorValues[0],0,1023,0,255);
   ellipse(posx,mouseY,50,50);
   if (sensorValues[1]==1){
     fill(random(255));
   }

  // use the values like this!
  // sensorValues[0] 

  // add your code

  //
}



void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 1 ], 9600);
  // WARNING!
  // You will definitely get an error here.
  // Change the PORT_INDEX to 0 and try running it again.
  // And then, check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index number of the port.

  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
  myString = null;

  sensorValues = new int[NUM_OF_VALUES];
}



void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = '\n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

Code for Final:

This is a work in progress code for the final project.

Arduino:

// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing

void setup() {
Serial.begin(9600);
pinMode(9,INPUT);
}

void loop() {
int sensor1 = digitalRead(9);
int sensor2 = digitalRead(8);
int sensor3 = digitalRead(10);
int sensor4 = digitalRead(6);
int sensor5 = digitalRead(7);

// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
Serial.print(“,”);
Serial.print(sensor3);
Serial.print(“,”);
Serial.print(sensor4);
Serial.print(“,”);
Serial.print(sensor5);
Serial.println(); // add linefeed after sending the last sensor value

// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}

Processing:

// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing

/*
 * Based on the readStringUntil() example by Tom Igoe
 * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
 */

import processing.serial.*;

String myString = null;
Serial myPort;


int NUM_OF_VALUES = 5;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;      /** this array stores values from Arduino **/

int maxImages = 2; // Total # of images
int imageIndex = 0; // Initial image to be displayed

 
// Declaring three arrays of images.
PImage[] a = new PImage[maxImages]; 
PImage[] b = new PImage[maxImages]; 
PImage[] c = new PImage[maxImages]; 
void setup() {

  setupSerial();
   size(240,150);
 
  // Puts  images into eacu array
  // add all images to data folder
  for (int i = 0; i < a.length; i ++ ) {
    a[i] = loadImage( "eyes" + i + ".jpeg" ); 
  }
  for (int i = 0; i < b.length; i ++ ) {
    b[i] = loadImage( "Unknown-15.jpeg"); 
  }
  for (int i = 0; i < c.length; i ++ ) {
    c[i] = loadImage( "Unknown-14.jpeg" ); 
  }

}


void draw() {
  updateSerial();
  printArray(sensorValues);
image(a[imageIndex],0,0);
image(b[imageIndex],0,height/3*1);
image(c[imageIndex],0,height/3*2);


imageIndex = constrain (imageIndex, 0,0);
imageIndex = constrain (imageIndex, 0, height/3*1);
imageIndex = constrain (imageIndex, 0, height/3*2);  

  // use the values like this!
  // sensorValues[0] 

  // add your code
if (sensorValues[0] == 1 || sensorValues[1]== 1 || sensorValues[2] ==1|| sensorValues[3] ==1|| sensorValues[4] ==1){
 //imageIndex += 1;
  imageIndex = int(random(a.length));
  imageIndex = int(random(b.length));
  imageIndex = int(random(c.length));
  sensorValues[0] = 0;
 sensorValues[1]= 0;
 sensorValues[2] =0; 
 sensorValues[3] = 0;
 sensorValues[4] = 0;
}

 

  // use the values like this!
  // sensorValues[0] 

  // add your code

  //
}



void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 1 ], 9600);
  // WARNING!
  // You will definitely get an error here.
  // Change the PORT_INDEX to 0 and try running it again.
  // And then, check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index number of the port.

  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
  myString = null;

  sensorValues = new int[NUM_OF_VALUES];
}



void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = '\n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

Recitation10-clover

What I learned:

1.Map can do many things, not only changing the range of the sensor value from Arduino but also can change the range of the value in processing. It is really useful if I want all my images don’t go out the canvas.

2.I learn how to create many balls using  the add function.

ballList.add(new Ball(random(-10,10), random(-10,10),color(random (255))));

also the  Ball temp = ballList.get(i);     temp.display();     temp.move(); to make the ball spread out and move.

3. The class is a great thing. I can change on another file no need to worry about affecting other function in the original file.

The code for processing:

My interaction is when pressing the Key UP, the ball will appear and spread out.

Ball cloverBall;
Ball cindyBall;
float x;
ArrayList<Ball> ballList = new ArrayList<Ball>(); 

void setup(){
  size(1600,900);
  ballList = new ArrayList<Ball>(); 
  
  for(int i=0; i<100; i++){
  ballList.add(new Ball(random(-10,10), random(-10,10),color(random (255))));
  x=map(x,0,1023,0,width);
  }
  //cloverBall = new Ball(random(-10,10), random(-10,10),color(random (255)));
  //cindyBall = new Ball(5,0,color(0,0,200));
  
  
}
void draw(){
  background(255,255,150);
  if (key == CODED) {
    if (keyCode == UP) {
     for(int i=0; i<ballList.size(); i++){
    Ball temp = ballList.get(i);
    temp.display();
    temp.move();
    } 
    
  }
  }
}
class Ball{
  float x,y;
  color c;
  float spdX, spdY;
  float r;
  Ball(float newSpdX, float newSpdY, color newColor){
    r=50;
    spdX= newSpdX;
    spdY= newSpdY;
    c=newColor;
    
    x=width/2;
    y=height/2;
    
  }
  void move(){
    x= x + spdX;
    y= y + spdY;
    
  }
  void display(){
    fill(c);
    ellipse(x,y,r,r);
  }
  
}
The video recitation10

Recitation 10: Object Oriented Programming by Lifan Yu

Recitation 10: Object Oriented Programming by Lifan Yu (ly1164)

My object called “Plastic” contain all images of plastic bottles and their movements. This way, we can not only control the common ways of their movements (or their common parameters)  but also control their movements and parameters individually.

Source of the used image: Lifan Yu

Code:

ArrayList<Plastic> plasticList;
 PImage img2;
 
 void setup(){
   size(800,800);
 plasticList = new ArrayList<Plastic>();
  img2=loadImage("plastic.png");
 }
 
 void draw(){
   background(255);
   imageMode(CENTER);
    for(int k=0; k<plasticList.size(); k++){
    Plastic temp=plasticList.get(k);
    temp.display();
    temp.move();
   
  }
 }
 
 
   void keyPressed(){
  float x=map(mouseX,0,width,width/4,3*width/4);
  float y=map(mouseY,0,height,height/4,3*height/4);
  plasticList.add(new Plastic(x,y));
  
    
}

Create another tag called “Plastic”:

class Plastic {

  float x,y;
  float size;
  float speedX;
  float speedY;
  Plastic(float startingX, float startingY){
    x=startingX; 
    y=startingY;
    size=random(50,150);

    speedX=random(-10,10);
    speedY=random(1,15);
  }

    void display(){
      image(img2,x,y,size,size);
  }
  

  void move(){
    x+=speedX;
    y+=speedY;
    if(x<=0||x>=width){
      speedX=-speedX;
    }
    
    if(y<=0||y>=height){
      speedY=-speedY;
    }
    
  }
  
}