# Week 11: Trio of Exercises

This week, we had to complete these three exercises:

1. make something that uses only one sensor  on arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing
2. make something that controls the LED brightness from processing
3. take the gravity wind example (https://github.com/aaronsherwood/introduction_interactive_media/blob/master/processingExamples/gravityExamples/gravityWind/gravityWind.pde) and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor

I was able to complete 2 of these in class, and the third one required some figuring out. The Arduino and Processing codes will be inserted for each in order here. I had to attach 2 videos at the end for exercise 3 because my phone memory was acting up and not letting me record long videos. This was the best I could manage.

## Exercise 1

This exercise still has the code to take inputs from both sensors, in case we wanted to change it up.

### Processing

```import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;
boolean onOff=false;
boolean onOff2=false;

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

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

}

void serialEvent(Serial myPort){
s=trim(s);
if (s!=null){
println(s);
int values[]=int(split(s,','));
if (values.length==2){
xPos=(int)map(values[0],0,1023,0, width);
yPos=(int)map(values[1],0,1023,0, height);
}
}
myPort.write(int(onOff)+","+int(onOff2)+"\n");
}```

### Arduino

```int left = 0;
int right = 0;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
while (Serial.available()) {
right = Serial.parseInt();
left = Serial.parseInt();
digitalWrite(2, right);
digitalWrite(5, left);
delay(1);
delay(1);
Serial.print(sensor);
Serial.print(',');
Serial.println(sensor2);
}
}
}```

## Exercise 2

### Processing

```import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;
boolean onOff=false;
boolean onOff2=false;

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

void draw(){
background(255);
ellipse(mouseX,mouseY,30,30);

}

void serialEvent(Serial myPort){
myPort.write((int)map(mouseY, height, 0, 0, 255)+"\n");
}```

## Arduino

```int brightness;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
while (Serial.available()) {
brightness = Serial.parseInt();

analogWrite(5, brightness);
Serial.println("0");

}
}
}```

## Processing

```PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
int sensor = 0;
int sensor_prev = 0;
float pos_prev = 0;

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;
boolean onOff=false;
boolean onOff2=false;
int bounce = 0;

void setup() {
size(640,360);
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);
printArray(Serial.list());
String portname=Serial.list()[2];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil('\n');
}

void draw() {
background(255);
bounce = 0;
if (sensor==0){
wind.x=0;
velocity.x*=hDampening;
}
applyForce(wind);
applyForce(gravity);
velocity.mult(drag);
acceleration.mult(0);
ellipse(position.x,position.y,mass,mass);
if((int)position.y >= height-mass/2 && (int)position.y != pos_prev){
bounce = 1;
}

if (position.y > height-mass/2) {
pos_prev = (int)position.y;
velocity.y *= -0.9;  // A little dampening when hitting the bottom
position.y = height-mass/2;
}
}

void applyForce(PVector force){
// Newton's 2nd law: F = M * A
// or A = F / M
PVector f = PVector.div(force, mass);
}

void keyPressed(){
if (key==' '){
mass=random(15,80);
position.y=-mass;
velocity.mult(0);
}
}

void serialEvent(Serial myPort){
s=trim(s);
if (s!=null){
int i = int(s);
sensor = (int)map(i, 0, 1023, 0, width);
if(sensor>sensor_prev){
wind.x=1;
}
else if(sensor<sensor_prev){
wind.x=-1;
}
sensor_prev = sensor;
}
myPort.write(bounce+"\n");

}```

## Arduino

```int onOff;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
while (Serial.available()) {
onOff = Serial.parseInt();