Line Graph instead of Bar graph

Hi all, I am helping my daughter with a science project where she has to measure vibration.

I followed a tutorial and I got the bar graph to show up in processing but I would like to graph it as a line graph instead. I found this page here that he modified the code from the same tutorial I used but the code as he posted does not work.

The people in the comments seem to have the same problems I am having with his code.

Here is my code that graphs a piezo to processing to a bar graph and it works well. I just think it would look nicer as a line graph.

Written to arduino uno

const int threshold = 25;
int sensorReading = 0;

void setup() {
  // initialize the serial communication:

void loop() {
sensorReading = analogRead(A0);
  if (sensorReading >= threshold) {

  else {


and here is the processing sketch

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
float inByte = 0;

void setup () {
  // set the window size:
  size(900, 800);

  // List all the available serial ports
  // if using Processing 2.1 or later, use Serial.printArray()

  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, "COM4", 9600);

  // don't generate a serialEvent() unless you get a newline character:

  // set inital background:
void draw () {
  // draw the line:
  stroke(127, 34, 255);
  line(xPos, height, xPos, height - inByte);

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
  } else {
    // increment the horizontal position:

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    inByte = float(inString);
    inByte = map(inByte, 0, 1023, 0, height);

I don't know Processing, but it sounds like your problem is a Processing problem rather than an Arduino problem. I assume there is a Processing Forum somewhere.


The people in the comments seem to have the same problems I am having with his code.

I'm not going to wade through a boat load of vague comments to guess what YOUR problem is. Define the problem HERE, since you posted your issue HERE.

I assume there is a Processing Forum somewhere.

There is, but enough of us use both that Arduino-related Processing questions are OK here.

To draw a line graph you need to draw a line from the previous point to the latest point and then make the latest the previous point.

line(xPos, height, xPos, height - inByte);


int latestHeight = height - inByte; line(xPos-1, previousHeight , xPos, latestHeight); previousHeight = latestHeight;

previousHeight must be a global int and you need to add some extra admin to get the first & last point of the graph correct.