We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello! I'm new to programming, so I'm having problems with understanding how serial works. My goal is to control a robot from Processing via serial to Arduino. Problem appears when I try to send FLOAT to arduino. This is the part of my code that I need help with. How do I send value X ? And I know that this could probably be shorter and better written, but that's not the point here.
thanks for help! :)
float speed;
float x = 200;
color fillE= 255;
void slider_Servo()
{
background(0);
fill(fillE);
x = constrain(x, 20, 380);
smooth();
ellipse(x,50,25,25);
if(key == CODED)
{
if(keyCode == RIGHT && keyPressed == true)
{
x=x+speed;
fillE= color(0,0,255);
println(x);
myPort.write(x);
}
else
{
x=x;
fillE=255;
println(x);
myPort.write(x);
}
if(keyCode == LEFT && keyPressed == true)
{
x = x-speed;
fillE=color(0,0,255);
println(x);
myPort.write(x);
}
else
{
x=x;
fillE=255;
println(x);
myPort.write(x);
}
if(keyCode == DOWN)
{
if(x > 200)
{
x = x-4;
println(x);
myPort.write(x);
}
if(x < 200)
{
x = x+4;
println(x);
myPort.write(x);
}
}
}
}
Answers
oh, and speed value is controled by another slider. just to make things clear.
You need to have a serial connection opened to your arduino and you need to send the value with with write http://arduino.cc/en/Serial/write. And i think that you can't send float to arduino. Here is a processing sketch for rgb led control it might help.
So how could I send value of slider to arduino? is there even a way?
You can always send the value from Processing as a string and then on the Arduino end ready the bytes and convert the string to a float using
float f = strtof (data);
another variant would be
myFloatVariable = (float)strtod(myStringReadFromSerial);