Processing Handshake 3 Exercises

First Exercise

Arduino Code:

void setup() {
  Serial.begin(9600);
  Serial.println("0"); //CRITICAL sends a start of communication between arduino and processing
}

void loop() {
  while (Serial.available()) {
   if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
   }
 }
}

Processing Code:

import processing.serial.*;
Serial myPort;
int xPos=0;


void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[5];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
}

// there can only one serial connection
void serialEvent(Serial myPort){ //callback function; if there is anything in the serial, this function gets called
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int value = int(s);
    if (value>=0){
      xPos=(int)map(value,0,1023,0, width);
    }
  }
  myPort.write("\n");
}

Comments: I feel like this was a pretty simple exercise and the one part where I was stuck was to figure out how to get the processing to communicate back with the arduino but it didnt take much time as all I had t o include was the myPort.write(“\n”); line

 

 

Second Exercise 

 

Arduino Code:

int brightness = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0"); //CRITICAL sends a start of communication between arduino and processing
  pinMode(3, OUTPUT);
}

void loop() {
  while (Serial.available()) {
      brightness = Serial.parseInt();
   if (Serial.read() == '\n') {
      analogWrite(3, brightness);
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor);
   }
 }



}

Processing Code:

import processing.serial.*;
Serial myPort;
int xPos = 0;
int brightness = 0;


void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[5];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
  brightness = (int)map(mouseX, 0, width, 0, 255);
}
// there can only one serial connection
void serialEvent(Serial myPort){ //callback function; if there is anything in the serial, this function gets called
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int value = int(s);
    if (value>=0){
      xPos=(int)map(value,0,1023,0, width);
    }
  }
  myPort.write(brightness + "\n");
}

Comments: Following up from the first exercise, this was much easier because I retained the code of the first one which made the communication much easier.

 

Third Exercise

Arduino Code:

int brightness = 0;
int previousValue = 0;
int moving = 0;
int onOff = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(3, OUTPUT);
  
//  pinMode(5, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    onOff = Serial.parseInt();
    if (Serial.read() == '\n') {


      int sensor = analogRead(A0);
      delay(1);
      
      
      Serial.print(sensor);
      Serial.print(",");
      Serial.println(previousValue);

      previousValue = sensor;

      
      if (onOff == 1){
        analogWrite(3, 255);
      }
      else{
        analogWrite(3, 0);
      }
    }
  }
}

Processing Code:

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
int Mover = 0;

import processing.serial.*;
Serial myPort;
int wind_speed;

int xPos=0;
int yPos=0;
boolean onOff=false;
//boolean onOff2=false;

void setup() {
  size(640,360);
  printArray(Serial.list());
  String portname=Serial.list()[5];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  noFill();
  position = new PVector(width/2, 0);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0,0);
  hDampening=map(mass,15,80,.98,.96);
}

void draw() {
  background(255);
  if (!keyPressed){
    wind.x=wind_speed;
    velocity.x*=hDampening;
  }
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x,position.y,mass,mass);
  if (position.y > height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
    }
    
  
  
  println(velocity.y);
}
  
void applyForce(PVector force){
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed(){
  if (keyCode==LEFT){
    wind.x=-1;
  }
  if (keyCode==RIGHT){
    wind.x=1;
  }
  if (key==' '){
    mass= random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    //println(s);
    int values[] = int(split(s,','));
    
    
    if (values.length == 2){
      if (values[0] > values[1]){
        wind_speed = 20;
      }
      else if (values[0] < values[1]){
        wind_speed = -20;
      }
      else if (values[1] == values[1]){
        wind_speed = 0;
      }
    }
    
  }
    if (round(position.y + mass) >= height && round(velocity.y) != 0){
    onOff = true;
    }
    else{
      onOff = false;
    }
  
    myPort.write(int(onOff) + "\n");
  
}

Comments: This was by far the toughest of the lot and actually took Maaz and me a long time to figure this out but ultimately we decided to do it by using one sensor to send two values from arduino to processing, working on the mechanics of the previous Button state as explained by professor Aaron. For the LED, we just performed a check for the position and velocity of the ball to determine whether to turn on the LED or not. It is also important to note that there can be discrepancies based on the individual components on the board as both me and Maaz would often get differing results although our wiring and codes were similar.

 

Video: https://youtu.be/7IgCwE5suAU

Leave a Reply