Firmata and Processing

I'm running standard firmata with this processing code:

/* Grapher
 * 
 * Graphs changing values, refreshing after graph reaches edge. (like an occiloscope)
 * 
 */

// DEFINTIONS

/* Define speed (X-axis). 1 is the fastest, and is equal to a speed of one value per millisecond.
 * i.e. if SPEED = 2 and width is 1000px then width/speed = 500ms per value
 */
int SPEED = 3 ;

// Define Y-axis value.
int Yaxis() {
  return arduino.analogRead(3); // Change "mouseY" to Y axis value
}

// End defs

// Program starts here
import cc.arduino.*;
import processing.serial.*;
Arduino arduino;

int lastX, lastY;
int newX, newY;
int high, low;

void setup() {
  size(screen.width - 100, screen.height);
  background(0);
  lastY = height/2;
  lastX = 0;
  low = height/8;
  high = height - low;
  arduino = new Arduino(this, Arduino.list()[0], 57600);
}

void draw() {
  stroke(255);
  newX =  millis()/SPEED % width;
  if(newX >= width - 20) {
    stroke(0);
    background(0);
    newX = 0;
  }
  newY = int(map(Yaxis(), 0, 1023, 0, height));
  line(lastX, lastY, newX, newY);
  stroke(255);
  lastX = newX;
  lastY = newY;
  println(newY);
}

If you didn't already figure it out, it's supposed to be a grapher. I have it graphing analog inputs. Notice how on the last line I am printing my "newY" variable. This always works, but the problem is on the graph. (You can try it if you want.)

Problem 1: values are inverted. A value of about 150 (this is what I get from my electret mic w/ ambient noise) is near the top, and 700 is near the bottom, i.e. it is inverted. Why is this? Is it something in my code, or firmata?

Problem 2: cutoff. You can see that I am mapping the values to fit my laptop's screen. When I hook up a pot to test the graph, I get the above mentioned problem plus another one. Even though I am mapping the low to 0, some values go off the bottom* of the graph Any ideas?

  • because of Problem 1.

Thanks!
baum

Problem 1: values are inverted. A value of about 150 (this is what I get from my electret mic w/ ambient noise) is near the top, and 700 is near the bottom, i.e. it is inverted. Why is this? Is it something in my code, or firmata?

Neither one. The issue is where the coordinate system origin is - upper, left corner with x to the right and y down.

So the top left corner is the origin, as opposed to the bottom right in conventional graphing?

So the top left corner is the origin, as opposed to the bottom right in conventional graphing?

Yes.

Why? Having the origin where it is implies that all Y axis values are negative!

Having the origin where it is implies that all Y axis values are negative!

No, it doesn't. 0 is at the top of the screen, and larger numbers represent values farther from the top.

You don't have to like it, or even understand it. You simply need to live with it.

Or, write your own drawing functions that use the lower, left corner of the dialog as the origin.

Fine. But why would they do that?

But why would they do that?

It's typical for dialog layout. The important stuff goes near the top (low Y values) and near the left (low X values). The underlying draw area is a dialog element, so it uses the same coordinate system.

OK. Thanks!