Serial Communication Examples

 Exercises 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

int left = 0;
int right = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}
void loop() {
  // while the serial is available
  while (Serial.available()) {
    // info that we parse and send to processor 
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      int sensor = analogRead(A0);
      delay(1);
      Serial.print(sensor);
    }
  }
}

– Processing

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;
boolean onOff=false;
boolean onOff2=false;
void setup(){
  size(960,720);
  // print list of ports
  printArray(Serial.list());
  // find the arduino port in the list and choose the right index/port
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
void draw(){
  background(255);
  // when mouse pressed, we change the onoff
  // if mouse is on right , turn on the light
  ellipse(xPos,height/2,30,30);
  
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=false;
    else
      onOff=false;
  }else{
    onOff=onOff2=true;
  }
  
}
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);
    }
  }
  
 
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
 
}

 Exercises 2

Make something that controls the LED brightness from processing
-Arduino
float brightness_led;
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(5, OUTPUT);
}
void loop() {
  while (Serial.available()) {
    
    brightness_led = Serial.parseFloat();
    
    if (Serial.read() == '\n') {
    
      analogWrite(5, brightness_led);
      Serial.println(brightness_led);
    }
  }
}

– Processing

import processing.serial.*;
Serial myPort;
int pos_x = 0;
int pos_y;
float brightness_led;
void setup() {
  size(960, 720);
  printArray(Serial.list());
  String portname=Serial.list()[5];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
void draw() {
  background(255);
  brightness_led = int(map(mouseX, 0, width, 0, 255));
  
  pos_y = height/2;
  ellipse(mouseX, pos_y, 30, 30);
}
void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  myPort.write(brightness_led+ "\n");
}

 Exercises 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

-Processing

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
int Mover = 0;
import processing.serial.*;
Serial myPort;
int speedWind;
int xPos=0;
int yPos=0;
boolean onOff=false;
void setup() {
  size(640,360);
  printArray(Serial.list());
  String portname=Serial.list()[5];
  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);
  if (!keyPressed){
    wind.x= speedWind;
    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;
    }
    
  
  
  println(velocity.y);
}
  
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 serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    
    int values[] = int(split(s,','));
    
    
    if (values.length == 2){
      if (values[0] > values[1]){
        speedWind = 10;
      }
      else if (values[0] < values[1]){
        speedWind = -10;
      }
      else if (values[1] == values[1]){
        speedWind = 0;
      }
    }
    
  }
    if (round(position.y + mass) > height && round(velocity.y)  != 0){
    onOff = true;
    }
    else{
      onOff = false;
    }
  
    myPort.write(int(onOff) + "\n");
  
}
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);
  }
}

– Arduino

int brightness = 0;
int previousValue = 0;
int moving = 0;
int onOff = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(2, OUTPUT);
  
}
 
void loop() {
  while (Serial.available()) {
    onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      delay(1);
      
      
      Serial.print(sensor);
      Serial.print(",");
      Serial.println(previousValue);
      previousValue = sensor;
      
      if (onOff == 1){
        analogWrite(2, 255);
      }
      else{
        analogWrite(2, 0);
      }
    }
  }
}

Leave a Reply