We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i am doing a project with a MPU 6050 where i use a arduino to read the values and get them. (http://playground.arduino.cc/Main/MPU-6050)
i have rewritten the code to get me the gyroscope values written like this:
x:42 y:3 z:-17 x:76 y:29 z:121 x:61 y:21 z:121 x:-34 y:3 z:20
and so on
now i wanted to get it and a graph so i can see a visual change on my computer but all my search on the Internet got me nowhere, so i asked 1 of my friends whom i a lot better at processing then me (i have only just started knowing arduino)
he then wrote me the following code
import processing.serial.*;
Serial myPort;
int val;
float x, y, z;
void setup()
{
size(512, 512);
background(0);
x = y = z = 0;
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
}
int c = 0;
void draw()
{
if ( myPort.available() > 0) {
String in = myPort.readStringUntil('\n');
try
{
if (in != null)
{
float x, y, z;
x = y = z = 0;
c++;
in.trim();
in = in.replaceAll("\n", "");
println("." + in + ".");
if (in.indexOf("x") >= 0)
{
x = Float.parseFloat(in.substring(2, in.length() - 1).trim());
} else if (in.indexOf("y") >= 0)
{
y = Float.parseFloat(in.substring(2, in.length() - 1).trim());
} else if (in.indexOf("z") >= 0)
{
z = Float.parseFloat(in.substring(2, in.length() - 1).trim());
}
if (c == 3)
{
c = 0;
PImage b = get();
background(0);
image(b, -1, 0);
stroke(255,255,255);
point(width-1, height/2);
stroke(255, 0, 0);
point(width-1, height/2 - x);
stroke(0, 255, 0);
point(width-1, height/2 - y);
stroke(0, 0, 255);
point(width-1, height/2 - z);
}
}
} catch (Exception e) {}
}
}
(i hope i got the syntax on the forum right, and the codes syntax is my friends)
i realize posting someone else code and just asking for help on why its not working is kinda weird, but i read it and tried to understand the code and i think i got the most of it, the biggest thing i don't get it how it get the number from the cl port but when i run the code thats seems fine so i don't question it.
what seems to be the problem is the x and y parsing. when i run the code i get the x and y values to 0 constantly however the z seems to work fine. (i can't for some reason upload the picture of how it works.)
i know now helping with this is hard since the values i get are made with my hardware, but its basically 3 random values between -250 and 250 i try and parse.
so the question i am getting at is how do i get my code to work/ why isn't it working?
Answers
@ line #28 you've got a classic overshadowing bug case!
That is, the fields x, y & z from line #5 are re-declared as local variables inside the
if (in != null)
block scoping!Line #29 seems a logical bug too! :-/
I think what you want to do is extract data from the Arduino and then use the values to display visuals on the screen. I wrote a tutorial on the same on my blog at http://theblubot.blogspot.in/2013/10/interfacing-arduino-from-processing.html. Hope it helps.
GoToLoop yes it definitely looks that way, it was also the first thing that can to mind ass i tried understanding the code. however consulting my friend he explained why there was no overshadowing bug, and i curse myself for not remembering why, and using "my friend said differently" to argue with. but a far ass i remember it something like the in.indexOf("x") (line 35, 38 and 41) part of the code that made it possible to only change the one of them and not resetting them.
(i have also run a version where i change line 46 to "if (c== 2 )" some how when i run the program i get 3 graphs showing then values of x, y, z and a line at 0 colored in a lighter version of the z line (no idea why) but i got my values so its not bug its a feature) but this code is just one bug bug that happens to sometimes work so id like t get it to work and understand why.
abhikpal2509 i read your guide its nice and simple thx but there are differences her you only send 1 number i send 3 lines with letters also and i can read the values on the bottom of my processing PDE (hope i am using that correctly) its parsing the numbers that seem to be a problem but nice guide it did help me under stand processing better.