Serial Output

In preparation for the media controller project, I had to learn how to transmit serial data from Arduino to another program. For this lab, I wired up an FSR on the Arduino board, as I have several times before. The Arduino code for this exercise was very simple:

[code lang="c"]void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int analogVal = analogRead(A0)/4;
  Serial.write(analogVal);
}[/code]

When I opened up the serial monitor in Arduino, squeezing the FSR resulted in a long string of junk data.

This is because the FSR sends data in binary, but the serial monitor expects transmissions in ASCII, so it displays the corresponding ASCII value for the byte it receives.

I also viewed this data in CoolTerm, which displayed the information more comprehensibly in hex.

Finally, I programmed Processing to display the data graphically.

This is the Processing code that makes it possible. It draws a vertical line that corresponds to the amount of force on the FSR.

[code lang="java"]
import processing.serial.*;
Serial myPort; 

int xPos;

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

void draw()
{}

void serialEvent(Serial myPort)
{
  int inByte = myPort.read();
  println(inByte);
  float yPos = height - inByte;
  stroke(#07095D);
  line(xPos, height, xPos, height - inByte);
  
  if (xPos >= width) {
    xPos = 0;
    background(#0582FC);  //clear screen - reset background
  }
  else {
    xPos++;
  }
}[/code]