Lab: Serial Out

The lab link (source of the code).

The circuit:

Serial Out from Xinran Wang on Vimeo.

Code in Arduino:

void setup() {
    Serial.begin(9600);
}
void loop() {
    int analogValue = analogRead(A0) /4;      // read the pot value
    Serial.write(analogValue);        // print the value in the serial monitor as a binary value
}

Code in Processing:

import processing.serial.*;

Serial myPort;

float xPos = 0;  

void setup() {
  size(800, 600);
  println(Serial.list());
  String portName = Serial.list()[4];
  myPort = new Serial(this, portName, 9600);
  background(255);
}

void draw() {
}

void serialEvent (Serial myPort) {
  // get the byte:
  int inByte = myPort.read(); 
  // print it:
  println(inByte);

  float yPos = height - inByte;
  stroke(150);
  line(xPos, height, xPos, height - inByte);

  if (xPos >= width) {
    xPos = 0;
    background(255);
  } 
  else {
    xPos++;
  }
}