Exercises: Serial Comms

Exercise 1: I essentially modified the starter code such that the y position remained fixed in the middle of the screen.

Arduino:

int pot = A0;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(pot, 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;
int yPos=height/2;
 
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){
    println(s);
    int val=int(s);
      xPos=(int)map(val,0,1023,0, width);
  }
  myPort.write("\n");
}

 

Exercise 2: I used the mouse speed from Processing to determine the brightness of the LED in Arduino.

Arduino code:

int ledPin = 5;
int bright;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    bright = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(ledPin, bright);
      Serial.print("\n");
    }
  }
}

Processing Code:

import processing.serial.*;
Serial myPort;
float mouseSpeed;
float smoothSpeed;
 
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, 25, 25);
  mouseSpeed =  5 * dist(mouseX, mouseY, pmouseX, pmouseY);
  smoothSpeed = lerp(smoothSpeed, mouseSpeed, 0.1);
  println(smoothSpeed);
}
 
void serialEvent(Serial myPort){
  float val = map(smoothSpeed, 0, 400, 0, 255);
  myPort.write((int)val + "\n");
}

 

Exercise 3: The photoresistor affected the wind direction and each time the ball bounced on Processing, I wrote to the serial to communicate to Arudino that the LED should light up. I originally used a photoresistor, but it felt a bit clunky. I still find the response in the wind vector kind of slow but find the movement more intuitive than the potentiometer.

Arduino Code:

int phot = A0;
int led = 2;
int bounce = 0;
int pastVal;
int val;
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(led, OUTPUT);
  pinMode(phot, INPUT);
  val = analogRead(phot);
  pastVal = val;

}

void loop() {

  while (Serial.available()) {
    bounce = Serial.parseInt();
    if (bounce == 1) {
      digitalWrite(led, HIGH);
    } else {
      digitalWrite(led, LOW);
    }
    if (Serial.read() == '\n') {
      

      val = analogRead(phot);
      if (pastVal != val) {
        if (val >= 34) {
          Serial.println(1);
        } else {
          Serial.println(-1);
        }
        pastVal = val;
      }
      else {
        Serial.println(0);
      }
      }
     
    }
  }

 

Processing Code:

PVector velocity;
PVector gravity;
import processing.serial.*;
Serial myPort;

PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
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);
  
  String portname=Serial.list()[2];
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

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;
      bounce = 0;
    }
}
  
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) {
    int windVal = int(s);
    println(windVal);
    if (wind.x >= -1 || wind.x <= 1) {
      wind.x = windVal;
    }
  }
  
  if (round(velocity.y) < 0) {
    bounce = 1;
  } else {
    bounce = 0;
  }
  myPort.write(bounce + "\n");
}

 

 

 

 

 

Leave a Reply