# Graphing analog sensor data from Arduino in Processing

**URL:** <https://discourse.processing.org/t/graphing-analog-sensor-data-from-arduino-in-processing/32078>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 3, 2021, 6:12pm UTC](https://discourse.processing.org/t/graphing-analog-sensor-data-from-arduino-in-processing/32078 "2021-09-03T18:12:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nyeas001](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nyeas001](https://discourse.processing.org/u/nyeas001)\
**Post date:** [September 3, 2021, 6:12pm UTC](https://discourse.processing.org/t/graphing-analog-sensor-data-from-arduino-in-processing/32078/1 "2021-09-03T18:12:28Z")

</div>

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:

```auto
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}’?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 3, 2021, 8:11pm UTC](https://discourse.processing.org/t/graphing-analog-sensor-data-from-arduino-in-processing/32078/2 "2021-09-03T20:11:21Z")

</div>

Hello,

```auto
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:

```auto
framerate(30);

```

Look here for answers:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts…

Reference is a good place to start.

`:)`

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [September 4, 2021, 9:08am UTC](https://discourse.processing.org/t/graphing-analog-sensor-data-from-arduino-in-processing/32078/3 "2021-09-04T09:08:35Z")

</div>

```auto
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.
