Recitation 8: Serial Communication

Task 1: draw line code


 

We had problems with the float values, but a learning assistant came and guided us to put “float pre1;” and “float pre2;” before the void setup. Although the code works, I find it hard to control where you are drawing lines.

/**
* Example sketch for the SerialRecord library for Processing.
 *
 * Receives two integers from the serial port, and uses them to control the x
 * and y position of a circle on the canvas.
 */

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

Serial serialPort;
SerialRecord serialRecord;
float pre1;
float pre2;

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

void draw() {
  //background(0);
  serialRecord.read();
  int value1 = serialRecord.values[1];
  int value2 = serialRecord.values[2];
  float x = map(value1, 0, 1024, 0, width);
  float y = map(value2, 0, 1024, 0, height);
  if (value1 != pre1) {
  line(x, y, pre1, pre2);
}
  pre1=x;
  pre2=y;
  stroke(255);
}

Task 2:

I worked with Xiao Zhou. We both worked to attach the wires to the right spot. After that, I worked with her on the codes. We had some trouble with making the ball move from one side of the screen to the other, so the LA helped to explain the left and right codes. 

This step is the ball being controlled by the potentiometer.

Audrino:

int tell = -1;

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

Servo left;  // create servo object to control a servo
Servo right;  // create servo object to control a servo

// Change this number to the number of values you want to receive
SerialRecord reader(1);

void setup() {
  Serial.begin(9600);
  left.attach(9);
  right.attach(8);
  
}

void loop() {
  if (reader.read()) {
    tell = reader[0];
  }

  if (tell == 0) {
    right.write(120);
    delay(300);
    right.write(0);  
    tell = -1;
  } 

  if (tell == 1) {
    left.write(120);
    delay(300);
    left.write(0);
    tell = -1;
  }
}

Processing:

\float i=0;
int j = 1;
import processing.serial.*;
import osteele.processing.SerialRecord.*;

Serial serialPort;
SerialRecord serialRecord;

void setup() {
  fullScreen();

  String serialPortName = SerialUtils.findArduinoPort();
  serialPort = new Serial(this, serialPortName, 9600);
  serialRecord = new SerialRecord(this, serialPort, 1);
}

void draw() {
  background(0);
  fill(255);
  ellipse(i,height/2,60,60);
  i=i+20*j;
  if(i<=0){
    serialRecord.values[0] = 1;
    serialRecord.send();
    j = 1;
  }

  if(i>=width){
    serialRecord.values[0] = 0;
    serialRecord.send();
    j = -1;
  }
}

 

Leave a Reply

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