Recitation – 11/18 – Serial Communication

Task 1

Completing this task was not too difficult, as I simply had to connect two potentiometers to my Arduino and modify the SendMultipleVariables example. When it came to coding on Processing, however, I encountered some challenges, mainly when changing the moving circle to a continuous drawing of lines. In the end, I was able to complete my code successfully with the help of the provided example. Here is a video of this task in action!

Here is my Processing code!

import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;

float px;
float py;

void setup(){
  size(500, 500);
  ellipseMode(CENTER);
  
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 2);
  background(0);
}

void draw(){
  drawBall();
}

void keyPressed(){
  if(key == 'b' || key == 'B'){
    drawBack();
  }
}

void drawBack(){
  background(0);
}

void drawBall(){
  serialRecord.read();
  
  float value1 = serialRecord.values[0];
  float value2 = serialRecord.values[1];
  
  float x = map(value1, 0, 1024, 0, width);
  float y = map(value2, 0, 1024, 0, height);
  
  //fill(255);
  //circle(x, y, 100);
  
  stroke(225);
  line(x, y, px, py);
  
  px = x;
  py = y;
} 

Here is my Arduino code!

#include "SerialRecord.h"

// Change this number to send a different number of values
SerialRecord writer(2);

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);

  writer[0] = sensorValue1;
  writer[1] = sensorValue2;
  writer.send();

  // This delay slows down the loop, so that it runs less frequently. This can
  // make it easier to debug the sketch, because new values are printed at a
  // slower rate.
  delay(10);
} 

Task 2

My partner Kenny and I agreed that this task was significantly harder than the previous one, as the input for our Arduino was coming from Processing instead of the input for Processing coming from Arduino. The basic logic and the one for Processing were quite simple, but we were facing some issues when having our servos do a back-and-forth action. However, after consulting with a Lab Assistant and realizing we made a tiny mistake, we got our program to work! Here’s a short video demonstrating our finished product!

Here is my Processing code!

import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;

int x = 0;
int size = 200;
int b = 0;

void setup(){
  fullScreen();
  
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 2);
}

void draw(){
  drawBack();
  drawBall();
}

void drawBack(){
  background(0);
}

void drawBall(){
  noStroke();
  int R = floor(map(x, 0, width, 0, 255));
  int B = floor(map(x, 0, width, 255, 0));
  
  fill(R, 0, B);
  circle(x, height/2, size);
  
  if(x < width && b % 2 == 0) { x += 5; serialRecord.values[0] = 0; serialRecord.values[1] = 0; } if(x > 0 && b % 2 != 0)
  {
    x -= 5;
    serialRecord.values[0] = 0;
    serialRecord.values[1] = 0;
  }
  if(x == 0 || x == width)
  {
    b++;
    if(x == 0){
    serialRecord.values[0] = 1;
    }
    
    if(x == width){
    serialRecord.values[1] = 1;
    }
  }
  serialRecord.send();
} 

Here is my Arduino code!

#include "SerialRecord.h"
#include 

SerialRecord reader(2);

Servo myservo1;
Servo myservo2;

int pos1 = 0;
int pos2 = 0;
int b = 0;

void setup() {
  myservo1.attach(9);
  myservo2.attach(8);

  Serial.begin(9600);
}

void loop() {
  reader.read();

  if(reader[0] == 1) {
    myservo1.write(180);
    delay(50);
    myservo1.write(0);
  } 
  if(reader[1] == 1) {
    myservo2.write(180);
    delay(50);
    myservo2.write(0);
  }
} 

 

Leave a Reply

Your email address will not be published. Required fields are marked *