mapping error - arduino, very basic

edited August 2015 in Arduino

Hey yall - firstly thanks for any help.

I am trying to run the very basic "graph" tutorial off of an arduino. (https://www.arduino.cc/en/Tutorial/Graph). I have a simple circuit with a potentiometer.

When I run this processing code, I get the following error:

map(NaN, 0, 1023, 0, 300) called, which returns NaN (not a number)

Weird. But if I type "println(inByte)" after my map line, the console shows me changing potentiometer values just fine. No error. Also, I should say the pop-out window which is supposed to graph my moving values is black, and never shows any sort of graph. Thanks again for all your help -

Tagged:

Answers

  • i assume that inByte comes as String

    so use trim on it

    so convert it by

    map(float(inByte), 0 .......

    and show your code

  • edited October 2015

    Thank you for the reply! This is my code:

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

    Again, if I write in a println(inByte); after the last line, the console does display mapped values. However no graph appears. But when I don't print the line, I get this error in the console (and still no graph):

    map(NaN, 0, 1023, 0, 300) called, which returns NaN (not a number)

    Meaning "inByte" -as its called in the map function - isn't working somehow.

    When I tried what I interpreted as your suggestion - tossing "float(inByte)" into my map function - i got the error message: "the function parseFloat(int) expects parameters like: "parseFloat(int)" ". ... Dang!

  • What type is inString at the start?

  • Hm, actually not sure. I think it's just listening to whatever is coming out of the serial port?

  • I dont know... debug it...

  • edited October 2015

    I had the same problem and find a solution here: [https://forum.arduino.cc/index.php?topic=351828.0]

    This code work for me:

    import processing.serial.*;
    
    Serial myPort;        // The serial port
    int xPos = 1;         // horizontal position of the graph
    
    float fValue;
    boolean newVal = false;
    
    void setup () {
      size(400, 300);
    
    
      println(Serial.list());
      myPort = new Serial(this, Serial.list()[0], 115200);
      myPort.bufferUntil('\n');
      background(0);
      stroke(127, 34, 255);
    }
    
    void draw () {
      if (newVal) {
        line(xPos, height, xPos, height - fValue);
        if (++xPos >= width) {
          xPos = 0;
          background(0);
        }
        newVal = false;
      }
    }
    
    void serialEvent (Serial myPort) {
      String inString = myPort.readStringUntil('\n');
      if (inString != null) {
        inString = trim(inString);
        fValue = float(inString);
        fValue = map(fValue, 0, 1023, 0, height);
        newVal = true;
      }
    }
    
  • I know this is a year old, but still. You will get the map(NaN) error if your baud rate is set incorrectly in processing.

    As in, your Arduino is running at 9600 Baud, and your processing code has: myPort = new Serial(this, Serial.list()[0], 115200);

Sign In or Register to comment.