Graphing analog sensor data from Arduino in Processing

Hi,
I am totally new in processing. I was following some of the you tube materials to familiarize myself with processing.I wanted to plot a graph of analog sensor using arduino. But I am getting error and I could not fix the problem. Could anyone please help me to understand the problem and its solution?

Code:

import processing.serial.*;

Serial port;            //define name of serial communication

int xPos=1;
float oldanalogData=0;

void setup(){
  size(1000,1000);     //defines the dimension of the display window width and height in units of pixels.//size(width, height)
 // width(int)width of the display window in units of pixels//height(int)height of the display window in units of pixels
 
  framerate(30);
  
  port=new Serial(this,"COM3",9600);
  
   background(0); 
 }

void draw(){                                 //draw function-here is the graphicl screen
  }

void serialEvent(Serial port){
 String inString = port.readStringUntil('\n');
}

if (inString != null) {
  //trim off any whitespace
  inString = trim(inString);
  print(inString);
  int currentanalogData = int(inString);
 
  //draw the analog data
  float analogData = map(currentanalogData,0,1023,0,height); //Re-maps a number from one range to another. map(value, start1, stop1, start2, stop2)
  stroke(0,255,0);  //Sets the color used to draw lines and borders around shapes
  line(xPos - 1, height - oldanalogData, xPos, height - analogData);
  oldanalogData = analogData;
  
 //at the edge of the screen, go back to beginning;
  if(xPos >= width){
    xPos=0;
    background(0);
   } else {
    xPos++;
     }
    }
   }

Error message I was getting:

Syntax Error - Incomplete statement or extra code near ‘extraneous input ‘if’ expecting {, ‘color’, ‘abstract’, ‘boolean’, ‘byte’, ‘char’, ‘class’, ‘double’, ‘enum’, ‘final’, ‘float’, ‘import’, ‘int’, ‘interface’, ‘long’, ‘native’, ‘private’, ‘protected’, ‘public’, ‘short’, ‘static’, ‘strictfp’, ‘synchronized’, ‘transient’, ‘var’, ‘void’, ‘volatile’, ‘{’, ‘;’, ‘<’, ‘@’, IDENTIFIER}’?

1 Like

Hello,

void serialEvent(Serial port){
 String inString = port.readStringUntil('\n');

// There should be code here from below !

}

//The code down here should go in function above

This is not correct:

framerate(30);

Look here for answers:

Reference is a good place to start.

:)

2 Likes
void serialEvent(Serial port){
 String inString = port.readStringUntil('\n');
}

if (inString != null) {

Hi @nyeas001, That } is the end of serialEvent. I think you want the rest of the code in serialEvent, so that } should be further down. Processing is seeing the "if " etc, as not in a function which is not allowed with other functions.

1 Like