Week 11: Exercises

Exercise 1:
Arduino
void setup() {
  Serial.begin(9600);
  Serial.println("0");
}

void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      Serial.println(sensor);
    }
  }
}
Processing
import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;

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

void draw(){
  background(255);
  ellipse(xPos,yPos,30,30);
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    int value= int(s);
    xPos=(int)map(value,0,1023,0, width);
    yPos=height/2;
  }
  myPort.write("\n");
}
Exercise 2:
Arduino
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(3, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    int right=Serial.parseInt();
    analogWrite(3,right);
  }
}
Processing
import processing.serial.*;
Serial myPort;
int brightness;

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

void draw(){
  background(255);
  brightness=int(map(mouseX,0,width,0,255));
  myPort.write(brightness+"\n");
}
Exercise 3:
Arduino
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(3, OUTPUT);
}
void loop() {
  while (Serial.available()) {
    int right = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(3, right);
      int sensor = analogRead(A0);
      Serial.println(sensor);
    }
  }
}
Processing
import processing.serial.*;
Serial myPort; 
int light;
int pos;
int value=0;
float previous;

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

void setup() {
  size(640, 360);
  noFill();
  printArray(Serial.list());
  String portname=Serial.list()[1]; 
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  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);
  previous=position.y;
  if (!keyPressed){
    wind.x=map(value,0,1023,-1, 1); // Add the wind
    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;
   }
  // check when it touches the bottom
  if (position.y >= height-mass/2) {
      light= int(true); // turn led on
  }
  else {
    light= int(false); // turn led off
  }
  // compare previous position to the current one (check if it stopped bouncing)
  if (previous==position.y){light= int(false);}
}

void serialEvent(Serial myPort){
  String s = myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    value = parseInt(s);
    //wind.x = map(value,0,1023,-1, 1);
  }
  myPort.write(light+"\n");
}
  
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);
  }
}

 

View post on imgur.com

Leave a Reply