Recitation 10–Workshops–Ketong Chen

We have a workshop today and I chose to attend the serial communication because our final project needs Arduino to talk with processing to control the LED and the words in the processing.

We learn to use the map function and I use it to change the value of potentiometer from 0 to 1023 to 0 to 500 to keep the ellipse within the screen. And when the button is pressed, the size of the ellipse change.

Here is the video:

Here is the code:

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

void loop() {
int sensor1 = analogRead(A0);
int sensor2 = digitalRead(9);
//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 = 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);

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

  // add your code
background(0);
 
  float posX = map(sensorValues[0],0,1023,0,500);
  int size;
  if (sensorValues[1]== 0){
    size = 50;
  }else{
    size = 100;
  }
   ellipse(posX, 300,size,size);
  
  
}



void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 3 ], 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]);
        }
      }
    }
  }
}

Recitation 10 : Workshops – Lillie Yao

Serial Communications Workshop:

For this weeks recitation, I attended the serial communications workshop because I felt like I still sort of struggled with communication between Arduino and Processing.

During this workshop, we went over the Serial Communications folder and discussed multiple and one values between Arduino and Processing. We connected a potentiometer to Arduino for the oneValue exercise and added a button for the multipleValues exercise. 

Lastly, we connected a servo motor for the last exercise. We had some obstacles during this exercise but overall, this workshop helped me a lot with telling Arduino and Processing what to do.

For using the map value, it was fairly simple with the potentiometer because there was a maximum and minimum value for the potentiometer and a maximum/minimum value for the mouse pressed function. 

Arduino Code:

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

void loop() {
int sensor1 = analogRead(A0);
int sensor2 = digitalRead(9);
// 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);
}

Processing Code:

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 **/
int previousSensorValues1;
int previousSensorValues2;
float posX;

void setup() {
size(600, 600);
background(255);
setupSerial();
}

void draw() {
//background(255);
updateSerial();
printArray(sensorValues);

float posX = map( sensorValues[0], 0, 1023, 0, 500);

ellipse(sensorValues[0],mouseY,50,50);

noFill();
line(previousSensorValues1,previousSensorValues2,sensorValues[0],sensorValues[1]);
previousSensorValues1 = sensorValues[0];
previousSensorValues2 = sensorValues[1];

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

// add your code

//
}

void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[2], 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]);
}
}
}
}
}

Young said that there wasn’t really an exercise for our workshop and to just write a few paragraphs on what we did during the workshop!

Recitation 10 by Christina Bowllan

For this week’s recitation class, we were tasked with creating a “knock-off” music video using images and videos, so I decided to create Rebecca Black’s “Friday” music video! First, I uploaded an image of a few girls riding around in a car. In order to do this, I used the variable “PImage” and put the photo code into void setup because I wanted the image to play one time. Next, I downloaded a video from the internet of a girl riding around in a car. In order to put this in processing, I imported the movie from my library and then put the movie processing code into void draw to to play the video. Then, since I wanted to play the image and then the video, I put in this code “if (millis()/1000 > 5)” which allowed me to telling processing that after 5 seconds, I wanted the image to end and start the video. Lastly, I talked to Marcela and she helped me insert music as well. I downloaded a simple sound code, and then downloaded the “Friday” song. I used boolean so that when the song plays, it will not be affected by the image and video changing as well; At first, when we didn’t have boolean, the song would repeat itself after 5 seconds because of the “millis” or time code.

import processing.video.*;
import processing.sound.*;
Movie myMovie;
SoundFile soundfile;
PImage photo;
boolean isPlaying=false;//this helps us so that the song won't restart with the millis starting over



void setup () {
  size (550, 380);
  background(255);
  photo =  loadImage ("download.jpg");
  //Rebecca Black - Friday.mp3
  image (photo, 100, 100);
  myMovie = new Movie (this, "friday.mp4");
  soundfile = new SoundFile(this, "Friday3.mp3");
}

void draw () {

  if (myMovie.available ()) {
    myMovie.read ();
  }

  if (millis()/1000 > 5) {
    //println(millis()/1000);
    background(255);
    myMovie.loop();
    myMovie.volume(0);
    image (myMovie, 0, 0);
    if (isPlaying ==  false) {
      playSong();//if it's not playing, then it'll play
    }
  }
}

void playSong() {
  isPlaying=true;  //this will make it play through one time 
  soundfile.play();
}

VIDEO:IMG_0299 2

Recitation 9 Media Controller by Barry Wang

Recitation 9 Media Controller

In this week’s recitation, I tried to make a brief test on the sensors that we would like to use in our final project. So I checked out a accelerometer and use it to control the speed that the video plays.  We hooked the accelerometer to the Arduino, and realize the communication between Arduino and Processing by serial communication.

The tilt of the accelerometer on x-axis represents the speed we would like to be. If I tilt to the left, the video plays reversely; if I stay in the middle, the video pauses; if I tilt to the right, the video plays forward. We managed to make it and it proved that this sensor is completely functional in our final project.

Here is a short test video:

Code on Arduino:

#include <LSM303D.h>
#include <Wire.h>
#include <SPI.h>

/* Global variables */
int accel[3]; // we’ll store the raw acceleration values here
int mag[3]; // raw magnetometer values stored here
float realAccel[3]; // calculated acceleration values here
float heading, titleHeading;
int v;

#define SPI_CS 10

void setup()
{
char rtn = 0;
Serial.begin(9600); // Serial is used for debugging
// Serial.println(“\r\npower on”);
rtn = Lsm303d.initI2C();
//rtn = Lsm303d.initSPI(SPI_CS);
if(rtn != 0) // Initialize the LSM303, using a SCALE full-scale range
{
// Serial.println(“\r\nLSM303D is not found”);
while(1);
}
else
{
// Serial.println(“\r\nLSM303D is found”);
}
}

void loop()
{
// Serial.println(“\r\n**************”);
//getLSM303_accel(accel); // get the acceleration values and store them in the accel array
Lsm303d.getAccel(accel);
while(!Lsm303d.isMagReady());// wait for the magnetometer readings to be ready
Lsm303d.getMag(mag); // get the magnetometer values, store them in mag

for (int i=0; i<3; i++)
{
realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE; // calculate real acceleration values, in units of g
}
heading = Lsm303d.getHeading(mag);
titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
v = int(realAccel[0]*10)+10;
// Serial.println(v);
Serial.write(v);

delay(10); // delay for serial readability
}

Code on Processing:

import processing.video.*;
import processing.serial.*;
Movie myMovie;
Serial Port;
float value;

void setup() {
  printArray(Serial.list());
  background(0);
  size(480, 480);
  myMovie = new Movie(this, "dancing.mp4");
  myMovie.loop();
  Port = new Serial(this, "COM11", 9600);
}
void movieEvent(Movie movie) {
  myMovie.read();  
}
void draw() {    
  while ( Port.available() > 0) {
    value = Port.read();
    println(value);
  }
  image(myMovie, 0, 0);   
  float newSpeed = map(value, 0, 20, -1, 1);
  myMovie.speed(newSpeed);
} 

Reflection:

In the Computer Vision reading, the part that I am most engaged with is the Myron Krueger’s point. He states that the “entire human body ought to have a role in our interactions with computers”. This is the exact point that me and my partner try to realize in our final project. In Krueger’s Videoplace project, the interaction is carried out through motion capture. Though we can out reach that point so far, what we can try is to fit the sensors into wearings like gloves, glasses and so on to create a similar interaction process. By doing so, the traditional interaction way of using mouse and keyboard is greately improved. And that is definitely a way that we need to pursue in the upcoming future.

Recitation 9: Media Controller-Azena

RESITATION:

Sorry for post so late. I realized my post was missing at Sunday, while those days I could not spare time to make it up. This is my second time recitation 9, hope you can forgive my late.

Arduino:

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

}

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

// 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);
}

Processing:

import processing.serial.*;

String myString = null;
Serial myPort;


int NUM_OF_VALUES = 3;   
int[] sensorValues;     
PImage img;
float j=0;
float i=0;
float x=0;
float y=0;
float speedX=10;
float speedY=10;


void setup() {
  size(1000, 1000);
  background(0);
  setupSerial();
  img = loadImage("pumpkin.png");
}


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

 
  imageMode(CENTER);
float j=map(sensorValues[0],0,1023,0,500);
  float i=map(sensorValues[1],0,1023,0,10);
  
    
 
  background(0);
 
  
image(img, x, y, j*1.2, j*1.2);
filter(BLUR, i);

if (sensorValues[2]==0 ){
  x=300;
  y=300;
}else{
x=x+1.5*speedX;
y=y+0.8*speedY;
if(x>width||x<0){
  speedX=-speedX;
}
if(y>height||y<0){
  speedY=-speedY;
}
}

  //
}



void setupSerial() {
  printArray(Serial.list());
   myPort = new Serial(this, Serial.list()[ 3], 9600);
  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); 
    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]);
        }
      }
    }
  }
}

I’d like to use two the potentiometers and a Botton to control the movements of image. At the first, I checked the code of the button online and have no idea how to move the digital Read(); to the processing, after asking help from assistant, I found out using sensorValue(); can transform all into processing;