We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
Currently I make a project for learn how to discuss between sensor and processing. So Processing read correctly the port of arduino ( I can see the value with processing)
My problem it's a cannot use this data for make something ( example I want to hide or show like influenced the opacity of an picture). For influenced the opacity I use a white rect but maybe is not a good idea.
Because ( maybe) I've string informations and for make a "if" I use a float . I search differents things but i'm lost with the differents method to are not very clear. I search to transform "cm" in float var1 for use my if. After I thinking to make a map for transform the data.
// the code with proximity sensor and "cm" like string value.
import cc.arduino.*;
import org.firmata.*;
import processing.serial.*;
Serial myPort;
String cm;
float var1;
PImage fonds;
void setup() {
size(displayWidth, displayHeight); //full screen
println(Serial.list());
myPort = new Serial(this, Serial.list()[6], 9600); // go to read the COM of arduino
myPort.bufferUntil('\n');
fonds = loadImage("testu.jpg");
image(fonds, 0, 0,displayWidth,displayHeight);
}
void draw() {
while (myPort.available() > 0) { //Continue usb serial
String cm = myPort.readStringUntil('\n'); // Read port analog
if(cm!=null) {
cm = trim(cm);
float var1 = float(cm); // The problem is here how to transform this value
var1 = map (var1, 0, 700, 0, 255);
println(cm); // affichage
println(var1);
}
}
if (var1 > 100){ // currently he don't read this part
noStroke();
fill(255,0);
rect(0, 0,displayWidth,displayHeight);
smooth();
}
else {
noStroke();
fill(255,10);
rect(0, 0,displayWidth,displayHeight);
smooth();
}
}
Sorry for my english if i make some mistake. Thank's Kinds regards
Answers
go back edit your post (click on the gear and then on edit) please
then format your code correctly - empty line before and after, highlight / select the entire code and hit ctrl-o
first use your
println()
to see what values cm / var1 you get from arduino - are they like 0.4, 0.6 etc. or 10,40,60 ?next make a second sketch without arduino to test your
fill()
.this complete sketch doesn't work:
you need to change the
fill()
lineyou can use a
println ("here 12");
within the if-clause to see whether he is getting there at all. When he gets there, and yourfill()
is wrong, you'll still see nothingCurrently I have my background picture and just after send play the rect begin progressively full white, I change fill(255,95) without arduino its the same.
The cm value is between 6 and 80 cm so more for value like 10,40,60
I have this message NotaNumber
this part is outside the while-statement
hence you are testing only the last var1 not all of them
Hint
in processing (not in the forum) hit ctrl-t. It gives you automatic better indents
Hep its better but I cannot control the fill of the rect again. The picture go slowly full white but don't come back clear.
I have the same message NaN. Its strange I try to change my map and the if but he make the same speed for add some white.
var1 = map (var1, 0, 30, 0, 255); if (var1 > 28) { noStroke(); fill(255, 95); rect(0, 0, displayWidth, displayHeight); smooth();
Thank's for your patience
message NaN.
in which line?
use ctrl-t
then post your entire code please.
did you put if inside while?
So now normally the "if" is in the while and for NaN its when he println(var1); So the problem its var1 for NaN.
you wrote:
what does println cm give you in this case?
say in line 26 :
if (cm!=null && !cm.equals("") ) {
You cannot convert a whole sentence to a number. But if the first part is always the same, you could just remove the first 15 characters from the string and parse the rest.
But i would actually try to change the arduino-code, and send only the values without the "Distance en cm :"
Indeed my mistake its to combine sentence and data
So now its functionnal I just need to change some parameters for the map and the background but its ok I go to continue.
Thanks guys !
Well done!