Recitation 8: Serial Communication

Task 1:

Everything went well. The only difficulty is that the order of the code was quite complicated and there are a lot of virables, so I needed to make sure every varible is right, all of those errors I met were caused by misspelling. The interaction was quite engaging, and it was inspiring to use those potential meters as knobs on the toy.

CODE:

boolean firstTime = true;
import processing.serial.*;
import osteele.processing.SerialRecord.*;
float prevX;
float prevY;
Serial serialPort;
SerialRecord serialRecord;
int value1P;
int value2P;
void setup() {
  size(500, 500);
  background(0);
  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);
}

void draw() {
  serialRecord.read();
  int value1 = serialRecord.values[0];
  int value2 = serialRecord.values[1];
  stroke(random(255),random(255),random(255));
  strokeWeight(5);
  float x = map(value1, 0, 1024, 0, width);
  float y = map(value2, 0, 1024, 0, height);
  if (firstTime){
    line(x,y,x,y);
    firstTime = false;
  }else{
  line(prevX,prevY,x,y);

  }
   prevX = x;
   prevY = y;  
}

Task 2:

My teammate is Angel Guo.

This task is much harder than task 1. There were a lot of errors when we were creating communication between Arduino and Processing. We learnt that the ability of Arduino to recieve values was very limited. When we kept sending values to Arduino, the error occurred and the communication between Arduino board and computer stopped. To solve the problem, we had to minimize the frequency of communication between Arduino and Processing that we only send values when the ball hit the border. 

This task deepens my understanding of sending and receiving values between Processing and Arduino. I find it interesting to make physical devices replying to digital images.

Code for Arduino:

//code for task 2 of recitation 8 by Rebecca and Angel
#include "SerialRecord.h"
#include <Servo.h>
// Change this number to the number of values you want to receive
SerialRecord reader(2);
Servo servo1;
Servo servo2;
void setup() {
  Serial.begin(9600);

  servo1.attach(9);
  servo2.attach(10);
     servo1.write(0);
        servo2.write(0);
}

void loop() {
  if (reader.read()) {

  if (reader[0] == 0) {
    servo1.write(-120);
    delay(200);
    servo1.write(0);
  } else {
    servo1.write(0);
  }
  if (reader[1] == 0) {
    servo2.write(120);
  delay(200);
    servo2.write(0);
  } else {
    servo2.write(0);
  }
}
}

Code for Processing:

 

//code for processing for rectation 8 By Rebecca and Angel
import processing.serial.*;
import osteele.processing.SerialRecord.*;
float xOffset = 0;
Serial serialPort;
SerialRecord serialRecord;
int direction = 1;
void setup() {
  fullScreen();
  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, 2);
        serialRecord.values[0] =1;
              serialRecord.values[1] =1;
    serialRecord.send();
}

void draw() {
  background(0);

  circle(0+xOffset, height/2, 300);

  // store some values in serialTransport.values, and send them to the Arduino
  xOffset = xOffset+10*direction;

    
    if (xOffset>=width-30) {
      direction=-direction;
      serialRecord.values[1] =0;
    } else      serialRecord.values[1]=1;
    if (xOffset<=0) {
      direction=-direction;
      serialRecord.values[0] =0;
    } else     serialRecord.values[0]=1;

  serialRecord.send();
}

 

Leave a Reply

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