# Plotting data from Arduino

**URL:** https://discourse.processing.org/t/plotting-data-from-arduino/42285
**Category:** Electronics (Arduino, etc.)
**Created:** [June 24, 2023, 2:45pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285 "2023-06-24T14:45:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cyp](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cyp](https://discourse.processing.org/u/cyp)
#### Post date: [June 24, 2023, 2:45pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/1 "2023-06-24T14:45:08Z")

</div>

Hi everyone.i have a little code i wrote from a school project. if someone could help me wih a code to processing to plot these variables. ex: volt=f(debit) curent=f(debit).

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 24, 2023, 3:21pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/2 "2023-06-24T15:21:15Z")

</div>

Here’s my best guess at what you are trying to do:

```auto
import processing.serial.*;

float x, y, y1;
float inByte = 0;

Serial myPort;

void setup() {
  size(800, 400);
  background(255);
  printArray(Serial.list());
  // Enter correct number for your system
  myPort = new Serial(this, Serial.list()[4], 9600);
}

void draw () {
  inByte = map(inByte, 0, 1023, 0, height); // map to screen height:
  y1 = height - inByte;
  line(x - 1, y, x, y1);
  y = y1;
  if (x >= width) {
    x = 0; // at edge of screen -> back to beginning
    background(255);
  } else {
    x++; // increment horizontal position:
  }
}

void serialEvent( Serial myPort) {
  //'\n' is the end delimiter indicating the end of a complete packet
  String inStr = myPort.readStringUntil('\n');
  //make sure our data isn't empty before continuing
  if (inStr != null) {
    //trim whitespace and formatting characters (like line feed, carriage return)
    inStr = trim(inStr);
    inByte = float(inStr);
    println(inByte);
  }
}

```

**Arduino code:**

```auto
byte outputValue = 0;
 
 void setup() {
 Serial.begin(9600);
 }
 
 void loop() {
 outputValue += 4;
 Serial.println(outputValue);
 //delay(10);
 }

```

---

<div class="post-metadata">

### Author: ![cyp](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cyp](https://discourse.processing.org/u/cyp)
#### Post date: [June 24, 2023, 8:47pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/3 "2023-06-24T20:47:45Z")

</div>

I can send to you the arduino code, in processing i didn’t wrote anuthing

În sâm., 24 iun. 2023 la 18:31 svan via Processing Foundation \<[notifications@processingfoundation1.discoursemail.com](mailto:notifications@processingfoundation1.discoursemail.com)\> a scris:

---

<div class="post-metadata">

### Author: ![cyp](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cyp](https://discourse.processing.org/u/cyp)
#### Post date: [June 24, 2023, 9:51pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/4 "2023-06-24T21:51:36Z")

</div>

#include \<Wire.h\>  
#include \<LiquidCrystal\_I2C.h\>  
#include \<Adafruit\_INA219.h\>  
#define enA 3  
#define in1 6  
volatile int flow\_frequency; // Measures flow sensor pulses  
// Calculated litres/hour  
float vol = 0.0,l\_minute;  
unsigned char flowsensor = 2; // Sensor Input  
unsigned long currentTime;  
unsigned long cloopTime;  
int X;  
int Y;  
int i;  
int j;  
float TIME = 0;  
float FREQUENCY = 0;  
float WATER = 0;  
float TOTAL = 0;  
float LS = 0;  
float voltage;// internal variable for voltage  
float voltage1;  
const int input = A2;  
int offset =20;// set the correction offset value  
LiquidCrystal\_I2C lcd(0x27, 20, 4);  
void flow () // Interrupt function  
{  
flow\_frequency++;  
}  
void setup()  
{

pinMode(enA, OUTPUT);  
pinMode(in1, OUTPUT);  
pinMode(flowsensor, INPUT);  
digitalWrite(flowsensor, HIGH);  
Serial.begin(9600);  
lcd.init();  
lcd.begin(20,4);  
lcd.backlight();  
attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // Setup Interrupt  
lcd.setCursor(0,0);  
lcd.print(“Water Flow Meter”);  
lcd.setCursor(0,1);  
lcd.print(“Circuit Digest”);  
currentTime = millis();  
cloopTime = currentTime;  
}  
void loop()  
{

int volt = analogRead(A0);// read the input  
double voltage = map(volt,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset

voltage /=100;// divide by 100 to get the decimal values

delay(500);  
int volt1 = analogRead(A3);// read the input  
double voltage1 = map(volt1,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset

voltage1 /=100;// divide by 100 to get the decimal values

delay(500);  
currentTime = millis();  
// Every second, calculate and print litres/hour  
if(currentTime \>= (cloopTime + 1000))  
{  
cloopTime = currentTime; // Updates cloopTime  
if(flow\_frequency != 0){  
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.  
l\_minute = (flow\_frequency / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour  
lcd.clear();  
lcd.setCursor(0,0);

```
 lcd.print("Debit: ");
  lcd.print(l_minute);
  Serial.print(l_minute);
  Serial.print(" ");
  lcd.print(" L/M");
  l_minute = l_minute/60;
  unsigned int x=0;

```

float AcsValue=0.0,Samples=0.0,AvgAcs=0.0,AcsValueF=0.0;

for (int x = 0; x \< 150; x++){ //Get 150 samples  
AcsValue = analogRead(A1); //Read current sensor values  
Samples = Samples + AcsValue; //Add samples together  
delay (3); // let ADC settle before next sample 3ms  
}  
AvgAcs=Samples/150.0;

//AcsValueF = (-3.76 + (AvgAcs \* (5.0 / 1024.0)) )/ 0.0000950;// pentru alimentare fara usb  
AcsValueF = (-2.53 + (AvgAcs \* (5.0 / 1024.0)) )/ 0.00090;//alimentare usb  
if(AcsValueF\<1)  
AcsValueF=0;  
lcd.setCursor(3,1);  
lcd.print(“Curent:”);  
lcd.print(AcsValueF);  
Serial.println(" Curent, Tensiune, Debit,");

```
  Serial.print(AcsValueF);
   Serial.print(" ");

```

lcd.print(“mA”);  
delay(50);

```
  flow_frequency = 0; // Reset Counter
}
  else {
  lcd.clear();
  lcd.setCursor(0,0);
  
  lcd.print("Debit: ");
  
  lcd.print( flow_frequency );
Serial.print(flow_frequency );
   Serial.print(" ");
        lcd.print(" L/M");

  
   
}

```

lcd.setCursor(0,3);  
lcd.print("Volt hidro: ");  
lcd.print(voltage);  
lcd.print("V ");

Serial.print(voltage);  
Serial.print(" ");  
[//Serial.println](https://Serial.println)("V ");  
lcd.setCursor(0, 2);  
lcd.print("Volt sarcina: ");  
lcd.print(voltage1);  
lcd.print("V ");

i=0;  
if(voltage1\>14)  
i=255;  
else i=0;  
analogWrite(in1, i);  
//digitalWrite(enA, HIGH);  
if(voltage1\<14 & voltage\<14){  
digitalWrite(enA, LOW);}  
else {  
digitalWrite(enA, HIGH);}  
}  
}

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 24, 2023, 10:09pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/5 "2023-06-24T22:09:46Z")

</div>

I would recommend temporarily not using the Arduino code that you posted. You will need to learn how to write Processing code if you don’t already know. It’s not that difficult. Most apps have at least a setup() and draw() functions. In your case your will also need a serialEvent() method to get Arduino output as shown above. Start with the examples below; once you have that working then you can try and apply those demonstrated principles to display the data from the Arduino code that you posted. It may take you a while to learn all of this, so be patient.

Basic Processing app looks like this:

```auto
void setup() {
  size(400,400);
  background(209);
}

void draw() {
}

```

You will need to find your serial port first. Use the following code to do that and then we will take it from there:

```auto
import processing.serial.*;

Serial myPort; // The serial port

void setup() {
  printArray(Serial.list());
  // Change the index number for your system
  myPort = new Serial(this, Serial.list()[3], 9600);
}

void draw() {
}

```

---

<div class="post-metadata">

### Author: ![cyp](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@cyp](https://discourse.processing.org/u/cyp)
#### Post date: [June 24, 2023, 10:22pm UTC](https://discourse.processing.org/t/plotting-data-from-arduino/42285/6 "2023-06-24T22:22:33Z")

</div>

Thanks a lot i will try on Monday.
