[Week 11] Serial communication exercises

I want to finish exercises in serial communication early so that I can focus on the final project.

Exercise 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.

Arduino

const int poten = A0;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0");
}
 
void loop() {
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      int sensor = analogRead(poten);
      delay(1);
      Serial.println(sensor);
    }
  }
}

Processing

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

void setup() {
  size(960, 720);
  yPos=height/2;
  printArray(Serial.list());
  String portname=Serial.list()[4]; //[4] "/dev/cu.usbmodem1101"
  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 value = int(s);
    xPos=(int)map(value, 0, 1023, 0, width);
  }
  myPort.write("\n");
}
Exercise 2.

Make something that controls the LED brightness from processing

Arduino

const int led1 = 3;
float brightness;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(led1, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    brightness = Serial.parseFloat();
    if (Serial.read() == '\n') {
      analogWrite(led1, brightness);
      Serial.print("\n");
    }
  }
}

Processing

import processing.serial.*;
Serial myPort;
float brightness;
 
void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[4]; //[4] "/dev/cu.usbmodem1101"
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
 
void draw(){
  brightness = map(height-mouseY, 0, height, 0, 255);
  background(brightness);
  noStroke();
  fill(50, 150, 50);
  ellipse(width/2,mouseY,30,30);
}
 
void serialEvent(Serial myPort){
  myPort.write(int(brightness)+"\n");
}
Exercise 3.

Take the gravity wind example 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

Demo

Arduino

const int poten = A0;
const int led1 = 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(poten);
      delay(1);
      Serial.println(sensor);
      if (onOff == 1) {
        analogWrite(led1, 255);
      } else {
        analogWrite(led1, 0);
      }
    }
  }
}

Processing

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

import processing.serial.*;
Serial myPort;
int poten;

void setup() {
  size(640, 360);
  noFill();
  position = new PVector(0, 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()[4]; //[4] "/dev/cu.usbmodem1101"
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw() {
  background(255);
  if (!keyPressed) {
    wind.x=poten;
    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;
  }
}

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);
  }
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null) {
    int value = int(s);
    poten = (int)map(value, 0, 1023, 0, width*.01);
  }
  if (round(velocity.y) < 0) {
    myPort.write(1 + "\n");
  } else {
    myPort.write(0 + "\n");
  }
}

UPDATE APRIL 11. I made changes to the Processing file so that turning the potentiometer up and down can move the ball right and left.

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

import processing.serial.*;
Serial myPort;
int poten;
int prevPoten = 0;
boolean movingRight = true;
int maxPoten;

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()[4]; //[4] "/dev/cu.usbmodem1101"
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  
  maxPoten = (int)map(1023, 0, 1023, 0, width*.01);
}

void draw() {
  background(255);
  if (!keyPressed) {
    if (prevPoten < poten) {
      movingRight = true;
    } else if (prevPoten > poten) {
      movingRight = false;
    }
    if (!movingRight) {
      wind.x = -(maxPoten-poten);
    } else {
      wind.x = poten;
    }
    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;
  }
  prevPoten = poten;
}

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);
  }
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null) {
    int value = int(s);
    poten = (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