Graph 2.0

http://www.objectdna.com/workshops/wp-content/uploads/2011/10/Screen-shot-2011-10-30-at-02.43.06.png 980w" sizes="(max-width: 300px) 100vw, 300px" />

The example below shows how to simply print values over the serial port from the Arduino and have Processing interpret them as many different graphs. As long as you follow some simple printing restrictions, the code can be easily adapted to suit many printing needs.

Basic Setup

1. Download and install Processing.
2. Upload the Arduino code below to your Arduino (change pins as needed).
3. Copy the Processing code below to a new sketch in Processing.
4. Connect Arduino to computer; run Processing sketch.

Data Protocol

The following is a basic description of the data format (line ended by CR or CR+LF) used to communicate from Arduino to Processing:

To graph one value, you would use Serial.println("DataLabel:1023"); on the Arduino.

The resulting message sent to Processing:

DataLabel:1023

To graph multiple values, you would use a series of Serial.print("DataLabel1:1023"); statements with Serial.print(" | "); statements between to separate the values. After printing the last value, you should use a single, empty Serial.println(); to signal the end of the block of sample values.

The resulting message sent to Processing:

DataLabel1:1023 | DataLabel2:1023 | DataLabel3:1023

Sample Code

This is the Arduino code:

int sensorPinArray[] = { 14, 15, 16, 17, 18, 19 };

int sensorValue = 0;
char separator[] = " | ";

void setup() {
  Serial.begin(57600);
  for (int i=0; i<sizeof(sensorPinArray)/sizeof(int); i++) {
    pinMode(sensorPinArray[i], INPUT);  
    digitalWrite(sensorPinArray[i], HIGH);
  }
}

void loop() {
  for (int i=0; i<sizeof(sensorPinArray)/sizeof(int); i++) {
    sensorValue = analogRead(sensorPinArray[i]);
    if (i>0) {
      Serial.print(separator);
    }
    Serial.print("Analog");
    Serial.print(i);
    Serial.print(":");
    Serial.print(sensorValue);
  }
  Serial.println();
}

This is the Processing code:

import processing.serial.*;

String delimiterBetweenMultipleValues = "|";
String delimiterBetweenLabelAndValue = ":";

int graphStartX = 100;
int textHeight = 15;
int textPadding = 2;
int circleDiameter = 3;
color backgroundColor = color(0, 0, 0, 255);
color graphHeadColor = color(255, 255, 255, 255);
color textColor = color(255, 255, 255, 255);

Serial myPort;        // The serial port
int opacity = 100;

String[] valuePairs;
boolean drawPoint = false;
boolean drawCircle = true;
boolean drawLine = true;
int xPos = graphStartX;         // horizontal position of the graph

void setup () {
  // set the window size. (Can be any size you want.  Width should be greater than 100, and height is
  // best when it's at least 32*number of streams)
  size(900, 192);        
  println(Serial.list());
  // Open Serial port at 57600.
  myPort = new Serial(this, Serial.list()[0], 57600);
  myPort.clear();
  myPort.bufferUntil('\n');
  background(0);
}

color gimmeTheRightColorForSensorNumber (int sensorNumber) {
  color col;
  int modulo8 = (sensorNumber % 8);
  modulo8++;
  if (modulo8 == 0) {
    modulo8 = 6;
  }
  col = color( ((modulo8 >> 2) & 0x01)*255, ((modulo8 >> 1) & 0x01)*255, (modulo8 & 0x01)*255, opacity);
  return col;
}

void draw () {
  // everything happens in the serialEvent()
}

void keyPressed() {
  // If you press 'l',
  if (key == 'l') {
    drawLine = !drawLine;
  }
  if (key == 'p') {
    drawPoint = !drawPoint;
  }
  if (key == 'c') {
    drawCircle = !drawCircle;
  }
  if (!drawLine && !drawPoint && !drawCircle) {
    drawPoint = true;
  }
}

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

  if (inString != null) {
    // trim off any whitespace.
    inString = trim(inString);
    // Show the incoming data string.
    println(inString);
    // split the incoming string into pairs of label+value and store in an array.
    valuePairs = split(inString, delimiterBetweenMultipleValues);

    // Go through all the label and value pairs and plot the current data points.
    for (int i=0; i= width) {
      xPos = graphStartX-1;
    }
    else {
      // increment the horizontal position:
      xPos++;
    }
  }
}

void exit() {
  myPort.stop();
}

Leave a Reply

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

 

Spam protection by WP Captcha-Free