Exercises – Jade

Exercise 1

Arduino:

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(A0, INPUT);
}
 
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;

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,height/2,30,30);
}

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==1){
      xPos=(int)map(values[0],0,1023,width, 0);
    }
  }
  myPort.write("\n");
}

 

 

Exercise 2

Arduino:

int bright = 0;

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

void loop() {
  while (Serial.available()) {
      bright = Serial.parseInt();
      if (Serial.read() == '\n') {
        analogWrite(5, bright);
        Serial.println(0);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;

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(mouseX,mouseY,30,30); // mark
}

void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  myPort.write((int)(map(mouseY, height,0, 0, 255))+"\n");
}

 

Exercise 3

Arduino:

int bounce = 0;

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

void loop() {
  while (Serial.available()) {
    bounce = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(5, bounce);
      int sensor = analogRead(A0);
      Serial.println(sensor);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;

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

boolean flag = false;

void setup() {
  size(640,360);
  printArray(Serial.list());
  String portname=Serial.list()[1];
  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);
  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;
    } 
  if (round(velocity.y)<0) {
    flag = true;
  } else {
    flag = false;
  }
}
  
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 (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==1) {
      wind.x=(float)map(values[0], 0, 1023, 5, -5);
    }
  }
  myPort.write(int(flag)+"\n");

}

Leave a Reply