Categories
interaction lab recitations

Serial Communication

task #1 

So, I didn’t have much issues with completing the first task as it was mostly just using two codes and putting them together. (just mapping the numbers I get from the potentiometers with the width/height of the screen and then these were the coordinates of a position of a circle)

and then I had to just manipulate code so that i will have global variables that store previous position (x1 and y1) and create a line with the current coordinates (x0 and y0). both of them update every loop.

the Processing code is the following:

import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
float x2 = 0;
float y2 = 0;
void setup() {
  size(500, 500);
  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  // If the Arduino sketch sends a different number of values, modify the number
  // `2` on the next line to match the number of values that it sends.
  serialRecord = new SerialRecord(this, serialPort, 2);
  background(0);
}
void draw() {
  serialRecord.read();
  int value1 = serialRecord.values[0];
  int value2 = serialRecord.values[1];
  float x1 = map(value1, 0, 1023, 0, width);
  float y1 = map(value2, 0, 1023, 0, height);
  stroke(255);
  line(x2, y2, x1, y1);
  x2 = x1;
  y2 = y1;
}

for the Arduino it was “SendMultipleValues” example. 

task#2 

for the second task I had to play around with the code a bit. I decided not to send two variables from processing but to send only 1 that would say wether the ball reached the right side or the left side (otherwise it would not send anything). And then based on that the Arduino would activate the right servo. 

I had issues with it at first as apparently Processing doesn’t like “-1” as a number to send. and “0”. so i had to change those for “1” and “2” eventually (professor told me that feature of Processing). it also doesn’t like delay() function.

I didn’t do the FullScreen() thing, but I don’t think that it would be a problem as I would only need to change one thing. (add the fullscreen())

anyway, after I fixed that, everything was fine and the thing worked perfectly:

Arduino code:

#include "SerialRecord.h"
#include <Servo.h>

// Change this number to the number of values you want to receive
SerialRecord reader(1);
Servo myservo;
Servo myservo2;
int pos = 0;



void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  myservo2.attach(10);
}

void loop() {
  if (reader.read()) {
    if (reader[0] == 2) {
      myservo.write(180);
      delay(100);
      myservo.write(0);
    } else if (reader[0] == 1) {
      myservo2.write(180);
      delay(100);
      myservo2.write(0);
    }
  }
  //delay(20);
}

Processing code:

import processing.serial.*;
import osteele.processing.SerialRecord.*;
float acceleration = 5;
float ballsize = 30;
float y = height/2;
float x = width/2;
int where = 0;

Serial serialPort;
SerialRecord serialRecord;

void setup() {
  size(1000, 600);

  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);

  // In order to send a different number of values, modify the number `2` on the
  // next line to the number values to send. In this case, the corresponding
  // number in the Arduino sketch should be modified as well.
  serialRecord = new SerialRecord(this, serialPort, 1);
}

void draw() {
  background(0);
  circle(x, height/2, ballsize);
  if (x + ballsize/2 > width) {
    acceleration *= -1;
    where = 1;
  } else if (x - ballsize/2 < 0) {
    acceleration *= -1;
    where = 2;
  } else {
    where = 0;
  }

  x+= acceleration;
  if (where != 0) {
    serialRecord.values[0] = where;
    serialRecord.send();
    println(where);
  }
}


Leave a Reply

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