We are about to switch to a new forum software. Until then we have removed the registration on this forum.
im trying to make a graph (added to the bottom of this post) with data acquired by a serial feed But it won't draw right no matter how many variables I tweak.
I am not asking anyone to code for me, just to point out to a first timer(usually play with arduino) where my problem lies.
here is my code:`
import processing.serial.*;
Serial myPort; // The serial port
float inByte = 0; // defines starting value for the serial input
int xPos = 1; // horizontal position of the graph
int yPos = 300; // current vertical position of the graph
int lastxPos = xPos; // previous horizontal position of the graph
float lastyPos = (inByte); // last vertical position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list ());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 115200);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// draw the line:
stroke(0, 205, 0);
line(xPos, height-lastyPos*5, xPos, height - inByte*5);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
} else {
// increment the horizontal position:
xPos =xPos + 10;
//lastxPos = xPos;
}
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
println(inByte);
inByte = map(inByte, 0, 1023, 0, height);
}
}
`
Answers
that picture looks fine to me. you need to describe what the problem is, not just 'it won't draw right'.
where do you update lastyPos?
I don't really know what else I can give you.
Cheers J
After reading that again, I think it comes across.....Well a bit crappy. Tbh, I'm on a laaaarge amount of painkillers at the moment and it tends to mess with my comprehension and retaining of info too
but this sets lastyPos to the value of inByte AT THAT INSTANT IN TIME. if inbyte changes after this lastyPos won't reflect that.
but you didn't actually say that was an 'expected' picture and not an 'actual' picture...
try adding add line 48:
(i can't run your code, no serial port)
You don't set lastyPos
It's not enough to set it before setup, you need to set it in the right spot
Does println in line 62 good results? Did you try println in line 64?
Line 34: the multiply by 5 is surprising : wrong
Move 46 to line 63 maybe? Then you move your writing position only when there is an input
I have tried the lastyPos=inByte at line 48, and that gives an error of (paraphrased) can't assign a glitch to an integer
Q: Does println in line 62 good results? A: yes
Q: Line 34: the multiply by 5 is surprising A: It's only to blow the data up to where I can see it on the scale, as I'm manually triggering the pin on the arduino that reports to Processing via serial
Q: Move 46 to line 63 maybe? Then you move your writing position only when there is an input A : the graph is recorded over time, so I need the constant increments
Here's how it actually displays for me.
lastyPos Must be type float
Then follow koogs advice
can't assign a float to an integer, probably. so try:
what kind of values? the map in line 63 suggests 0 to 1023 (10 bits)
Anything you put in setup is called once.
If you put it in draw, it is called continuously. Think of an infinite loop(@koogs) being called 30 times per second.
To draw a line, it is easy. You need to points labeled x1,y1 and x2,y2. In your case, for any x1, x2=x1+10. For the second part of your data, you need to keep track of previous values. In other words, line 46 in your code should be like:
and line 34:
line(lastxPos, height-lastyPos*5, xPos, height - inByte*5);
Kf
I'm trying to log up to and maybe above 400,000rpm times up to 16 pulses per rpm 0 to 1023 is for.....oh damn the range that the arduino can see, its name eludes me. (ffs, buprenorphine, let me think) EDIT ADC is what i was thinking of
(int)inByte fixed that issue
Getting closer!
(beware calling println() too much (and 400,000 sounds like too much) - the console in processing is dodgy and can lock up you code.)
I think I'm only pulling maybe 150 pulses at best, by hand. end result will be more like 400,000. I'm trying to read Turbocharger rpm
Ding Ding Ding Ding We have a weiner! Thankyou very much Guys!! Much Gratitude :)
well done!
ok, so I got quite a lot more code now, and in the midst of trying to make this graph display 6 sensors worth of data, I've gotten stuck on the part where it strips the string back down into 6 parts(after the arduino put them together) I think that part actually may work, but I'm confused as to how to get the stripped string into my existing x/y display. if what I've messed up is obvious, please just point out where the problem is, not how to fix it.
the basic idea is just a 6 channel datalogger- I've not gotten to the saving parts, I'm still on the displaying live data side of things.
1st data feed is in Hz next 5 are just 0-5v
------------------Arduino code------------------
-------------------Processing Code---------------------
I can't seem to get this to view properly in here, in the edit window, it's fine.
edit post, highlight code, press ctrl-o
Much better, thanks koogs. Ill move this new info to another thread, as its a different issue