We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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
https://forum.Processing.org/two/discussion/14534/myport-available-always-0#Item_1
I edited the code adding in the If this lines:
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.
Kf
but don't forget
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:
`
`
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:
This is your x-axis:
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:For more, see the
map()
reference: