Processing Graphs/Arduino

Folks - Can someone point me to example code (Arduino & Processing sketches) that I can give to students in a class to graph numbers generated by a TMP 36 temperature sensor? I've tried several examples on the web that fell apart - old examples I used a year ago no longer work with new software revisions. Just want to use INTs. In a terrible bind for time this week. Please no blasts just give me a hand or skip it - thanks!

Post an old example that no longer works and maybe someone can tell you what to do to make it work with the current IDE.

Pete

A0159072:
old examples I used a year ago no longer work with new software revisions.

Why not just use the earlier software versions that work?
You can easily have several versions of the Arduino IDE on a PC. (I don't know Processing).

...R

Robin,
Because the school where I teach auto-installs newest versions, and it's tacky to make students in
a "modern" class go backwards. Thanks;

Here is an Arduino sketch that outputs temperature just fine:

// Measures temperature and displays on Output Screen
const int temperaturePin = 0;

void setup()
{
  Serial.begin(9600);
}
  
void loop()
{
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;

  /*
 Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  degrees C: ");
  Serial.print(degreesC);
  Serial.print("  degrees F: ");
  Serial.println(degreesF);
  //Serial.println((degreesF*40)-2700);
*/
  // These statements will print lines of data like this:
  // "voltage: 0.73 deg C: 22.75 deg F: 72.96"
   Serial.println(degreesF);
  delay(2); // repeat once per second (change as you wish!)
}


float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);
  // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 5.0 value that is the true voltage
  // being read at that pin.
}

And here is a Processing sketch that I used previously to graph the temp:

import processing.serial.*;

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

void setup () {
  // set the window size:
  size(1000, 300);

  // List all the available serial ports
  //println(Serial.list());

  myPort = new Serial(this, Serial.list()[5], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}

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

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:
    float inByte = float(inString);
    //println("inByte=",inByte);

    //inByte = map(inByte, 0, 1023, 0, height);
    inByte = map(inByte, 0, 80, 0, height);
    //println("inByte = ",inByte);
    // draw the line:
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inByte);
    //println("xPos =", xPos,"h=",height, "inByte=",width);

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

I am getting a NaN runtime error on the map statement, and I have gone
round and round for hours to no avail, trying lots of different things. I just want a simple graph of
temperature changing over time coming in from the Arduino.
Thanks for any and all help. Last year I swear this worked fine and I do not remember any difficulties.

[moderator: added code tags and reformatted some code]

Also this example generates NaN with the map function call:

Looks like something has just not been updated??

The delay was much to short I think

// Measures temperature and displays on Output Screen
const int temperaturePin = 0;


void setup()
  {
  Serial.begin(115200);
  }
 
 
void loop()
{
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0;
  degreesF = degreesC * (9.0/5.0) + 32.0;

  /*
 Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  degrees C: ");
  Serial.print(degreesC);
  Serial.print("  degrees F: ");
  Serial.println(degreesF);
  //Serial.println((degreesF*40)-2700);
*/
  // These statements will print lines of data like this:
  // "voltage: 0.73 deg C: 22.75 deg F: 72.96"
   Serial.println(degreesF);
  delay(200);
}


float getVoltage(int pin)
{
   
  return (analogRead(pin) * 0.004882814);
 
  // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 5.0 value that is the true voltage
  // being read at that pin.
}

Drawing has to be done in the draw() context

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
boolean newValue = false;
float inValue;

void setup () {
  // set the window size:
  size(1000, 300);       

  // List all the available serial ports
  println(Serial.list());

  myPort = new Serial(this, Serial.list()[0], 115200);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}


void draw ()
{
  if (newValue) {
    // draw the line:
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inValue);
    //println("xPos =", xPos,"h=",height, "inValue=",width);

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

void serialEvent (Serial myPort)
{
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);
    inValue = float(inString);
    //println("inValue=",inValue);
    inValue = map(inValue, 0, 1023, 0, height);
    //inValue = map(inValue, 0, 80, 0, height);
    //println("inValue = ",inValue);
    newValue = true;
  }
}

A0159072:
Robin,
Because the school where I teach auto-installs newest versions, and it's tacky to make students in
a "modern" class go backwards. Thanks;

It might be a great opportunity to demonstrate to your students the difference between "movement" and "progress".

...R

  • Format your code properly(Tools > Auto Format) - This can help you identify issues in your code and makes it much easier for others to read it as well.
  • Use code tags in your forum posts. Help us to help you.