In class exercises

Exercise 1:

Arduino Code:

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();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Processing code:

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos;
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,yPos,30,30);
  
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=true;
    else
      onOff=true;
  }else{
    onOff=onOff2=false;
  }
}

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

Exercise 2:

Arduino code:

int left = 0;
int right = 0;

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

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

 

Processing code:

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

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

void draw() {
  background(255);
  brightness=int(map(mouseY, 0, height, 0, 255));
  ellipse(xPos, yPos, 30, 30);
  if (mousePressed) {
    if (mouseX<=width/2) {
      onOff2=brightness;
    } else {
      onOff=brightness;
    }
  } else {
    onOff=onOff2=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) {
      xPos=(int)map(values[0], 0, 1023, 0, width);
      //yPos=(int)map(values[1],0,1023,0, height);
      yPos=mouseY;
    }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
}

 

Exercise 3:

Arduino code:

const int wind = A0;
const int brightness = 3;
int onOff = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0");


}

void loop() {
  while (Serial.available()) {
    onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      int sensor = analogRead(wind);
      delay(1);
      Serial.println(sensor);
      if (onOff == 1) {
        analogWrite(brightness, 255);
      } else {
        analogWrite(brightness, 0);
      }
    }
  }
}

 

Processing code:

import processing.serial.*;
Serial myPort;

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


int pos;
int prevPos = 0;
boolean Right = true;
int maxPos;

void setup() {
  size(640, 360);
  noFill();
  printArray(Serial.list());
  String portname=Serial.list()[2]; 
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  


  velocity = new PVector(0, 0);
    position = new PVector(width/2, 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);
  maxPos = (int)map(1023, 0, 1023, 0, width*.01);
 
}

void draw() {
  background(255);
  if (!keyPressed) {
    if (prevPos < pos) {
     Right = true;
    } else if (prevPos > pos) {
Right = false;
    }
    if (!Right) {
      wind.x = -(maxPos-pos);
    } else {
      wind.x = pos;
    }
    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;
  }
  prevPos = pos;
}

void applyForce(PVector force) {
 
  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) {
    int value = int(s);
    pos = (int)map(value, 0, 1023, 0, width*.01);
  }
  if (round(velocity.y) < 0) {
    myPort.write(1 + "\n");
  } else {
    myPort.write(0 + "\n");
  }
}

Leave a Reply