Incorrect data

I have already created a discussion for this but someone closed it. I created an Arduino sketch where i register the data of a temperature sensor.

This is the sketch:

`

int tmpin = A0;

void setup()
{
 pinMode(tmpin, INPUT);
 Serial.begin(9600);

}

void loop()
{
  //Some calculations to transform the voltage to temperature
  int value = analogRead(A0);
  float voltage = (value / 1024.0) * 5.0;
  float temp = (voltage - 0.5) * 100;


  Serial.println(temp);
  delay(1000);
}  

`

Then i created a Processing script where i represent the Arduino's data. Howewer in the console some values are incorrect, while others are correct.

This is the sketch:

`

import processing.serial.*;

//Serial port
Serial port;

//Value read from serial
float val;

//value to create x and y axes
int x = 150;
int y = 600;

//value to write intervals x and y axes
int x1;
int y1;

void setup()
{
  size(1000, 700);
  background(255);
  //name of the usb port
  String portname = Serial.list()[0];

  //object port
  port = new Serial(this, portname, 9600);

  //Increase in 2 the thickness
  strokeWeight(2);

  //x axis
  line(100, 650, 900, 650);

  //y axis
  line(100, 650, 100, 50);

  //intervals x axis
  for(int i = 0; i < 16;i++)
  {

    line(x, 640, x, 660);
    x = x + 50;

  }  

  //intervals y axis
  for(int i = 0;i < 12; i++)
  {

    line(90, y, 110, y);
    y = y - 50;

  }

  //values x axis
  text("1", 147, 675);
  for(int i = 0;i < 17;i++)
  {



  }  

}

void draw()
{

  //If port is available
  if(port.available() > 0)
  {
    String string = port.readString();
    val = float(string);
    println(val);
  }  

}  

`

Answers

  • We don't have your sensor. Could you post the values you're getting, with an indication of which ones look wrong?

  • In my Serial monitor i could see values such as 18.5 , 20.2 , 23,7 (degree), while in processing the values sometimes are correct a sometimes incorrect. Ex. 18.5 , 81.9 , Na , ecc.

  • Answer ✓

    In your arduino code, you are sending your data with println(). Then in Processing you should get your data using:

    String string = port.readStringUntil(ENTER);

    I recomend changing the name of your variable string to something different, for example, strVal. Lastly, to avoid null entries, use if(string!=null){...}.

    Kf

  • edited January 2017

    I edited the code adding in the If this lines:

    if (strVal != null) {
      println(val);
    }
    

    but after 5-6 seconds it raises an exception "Null Pointer Exception"

  • What line shows the error?

    Kf

  • val = float(strVal);

  • Ensure strVal in that last line is not null.

    if (strVal != null){
      val = float(strVal);
      println(val);
    }
    

    Kf

  • but don't forget

    sometimes incorrect. Ex. 18.5 , 81.9 , Na

    so you might need code to handle "Na" too, float() isn't going to work on "Na" (do you mean NaN?)

  • I solved the problem but there is another one. The datas arrived from arduino are in a range from -10.0 to 40/50.0 degree. How can i transorm they in x coordinates?

  • This is the new sketch:

    `

    import processing.serial.*;
    
    //Serial port
    Serial port;
    
    //variables read from serial
    float val;
    
    //variables to create x and y axes
    int x = 150;
    int y = 600;
    
    //variables to write line x and y axes
    int x1;
    int y1;
    
    //variables to write value x axis
    int xValue = 147;
    int number = 1;
    
    //variable to write value y axis
    int yValue = 605;
    int number2 = 1;
    
    //variable that rapresent the time
    int time = 150;
    
    
    //Mapped value of temperature
    
    float mapped;
    
    
    
    //------------------------------------------------------------------------------------------------------------------------------------------------------
    
    void setup()
    {
      size(1000, 700);
      background(255);
      //name of the usb port
      String portname = Serial.list()[0];
    
      //object port
      port = new Serial(this, portname, 9600);
    
      //Increase in 2 the thickness
      strokeWeight(2);
    
      //x axis
      line(100, 650, 900, 650);
    
      //y axis
      line(100, 650, 100, 50);
    
      //intervals x axis
      for(int i = 0; i < 16;i++)
      {
    
        line(x, 640, x, 660);
        x = x + 50;
    
      }  
    
      //intervals y axis
      for(int i = 0;i < 12; i++)
      {
    
        line(90, y, 110, y);
        y = y - 50;
    
      }
    
      //values x axis
      fill(0); 
      for(int i = 0;i < 16;i++)
      {
    
         text(str(number), xValue, 675);
         number = number + 1;
         xValue = xValue + 50;
    
      }  
    
      //values y axis
      text("1", 65, 605);
      for(int i = 0; i < 12; i++)
      {
    
         text(str(number2), 65, yValue);
         number2 = number2 + 1;
         yValue = yValue - 50;
    
      }  
    
    }
    
    //--------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    
    
    void draw()
    {
    
      //If port is available
      if(port.available() > 0)
      {
        String strVal = port.readStringUntil(ENTER);
    
    
        //to avoid null entries
        if(strVal != null)
        {
        val = float(strVal);  
        mapped = map(val, 50, -30, 650, 0);
        println(val);
        }
      }  
    
    
      //Writing of data in the diagram
      strokeWeight(4);
      point(time, mapped);
      time = time + 50;
    
    }  
    

    `

  • You could look up map() in the reference

    you can map values from one range to another range

  • if you read the code, you can see that i used map but don't work

  • @skizzo66 -- You said:

    The datas arrived from arduino are in a range from -10.0 to 40/50.0 degree. How can i transform they in x coordinates?

    This is your x-axis:

    //x axis
    line(100, 650, 900, 650);
    

    So, you've asked how to transform (-10.0 to 50.0) data into (100 to 900) x-values. Your code says map(val, 50, -30, 650, 0) -- but I'm not sure where these numbers are coming from, they don't match your data range and they don't match your x-axis range. That would be:

    map(val, -10.0, 50.0, 100, 900);
    

    For more, see the map() reference:

Sign In or Register to comment.