Random Spikes in Real Time Data Graphing

edited May 2016 in Arduino

Hi! So I am using this program to plot data from serial coming from an accelerometer connected to an Arduino. As you can see there are random spikes appearing in the plot. I know that it is not because of the data because I am looking at the data that is being transmitted and none of the points match with those in the image. Any suggestion as to what I can do to get rid of the random spikes? Thank you!

`import processing.serial.*; PrintWriter writer;

Serial myPort; // The serial port int xPos = 0; // horizontal position of the graph float inByte = 0; int lastxPos = 1; int lastHeight = 0;

void setup () { // set the window size: size(800,600);

// 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, "COM3", 115200);

// don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n');

//writer = createWriter("new_data.txt"); // set inital background: background(0); } void draw () { // draw the line: stroke(255,255,255); strokeWeight(1); //delay(5); line(lastxPos, lastHeight, xPos, height - inByte); lastxPos = xPos; lastHeight = int(height - inByte); if (xPos >= width) { xPos = 0; lastxPos = 0; background(0); } else { // increment the horizontal position: xPos++; }

}

void serialEvent (Serial myPort) { // get the ASCII string: if(myPort.available() > 0){ 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 = constrain(inByte, 0, 32768);
println(inByte/16384.0);
inByte = map(inByte, 0, 32768, 0, height);

}

} }`

RandomLines

Answers

  • G_DG_D
    Answer ✓

    Hard to say without being able to see the data which is being input. Clearly you have a lot of possible variability in terms of values - e.g. anywhere from 0 to 32768 (or maybe more - because your use of constrain suggests that you might have input values greater than 32768).

    Possibly line(lastxPos, lastHeight, xPos, height - inByte); is your culprit. In particular, height - inByte. If you end up with a value of inByte that is close to zero (or somehow getting a null value for inByte), then you are going to end up drawing a line that is very close to the height of the screen. Which maybe might explain the spikes

  • Highlight code, press Ctrl-o to format

Sign In or Register to comment.