How to show multiple data from arduino

how to read the value from arduino and then i want to displaying that on processing window.
This is my arduino code :

#include <TimerOne.h>
#include <PZEM004T.h>
#include <LiquidCrystal.h>


int dutycycle = 0;   // Initailize duty cylce variable as integer data type
int incomingByte = 0;
PZEM004T pzem(2, 3);  // (RX,TX) connect to TX,RX of PZEM
IPAddress ip(192, 168, 1, 1);
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);


void setup()
{
Serial.begin(9600);
pinMode (9, OUTPUT);   // set pin 9 as an output pin for pwm
pinMode (10, OUTPUT);   // set pin 10 as an output pin for pwm
 
Timer1.initialize(20000);   // Initailize timer1 time period as 20 milli second (50 Hz frequency)
TCCR1A = (TCCR1A & 0x0F) | 0xB0 ;   // set pin 10 inverted of pin 9
pzem.setAddress(ip);
lcd.begin(16, 2);   // lcd rows and columns
}


void loop()
{ 
if (Serial.available() > 0)
  {
int incomingByte = Serial.read ();
dutycycle = map(incomingByte, 0, 255, 0, 1023);
Timer1.pwm(9, dutycycle, 20000);   // Timer1.pwm function takes argument as (pin no. , dutycycle , time period)
Timer1.pwm(10, 1023 - dutycycle, 20000);

float v = pzem.voltage(ip);
if (v < 0.0) v = 0.0;

Serial.print(v); Serial.print("V; ");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V= ");
lcd.setCursor(2, 0);
lcd.print(v);


float i = pzem.current(ip);
if (i < 0.0) i = 0.0;

Serial.print(i); Serial.print("A; ");
lcd.clear();
lcd.setCursor(9, 0);
lcd.print("A= ");
lcd.setCursor(11, 0);
lcd.print(i);


float p = pzem.power(ip);
if (p < 0.0) p = 0.0;

Serial.print(p); Serial.print("W; ");
lcd.clear();
lcd.setCursor(9, 1);
lcd.print("W= ");
lcd.setCursor(11, 1);
lcd.print(p);


float e = pzem.energy(ip);

Serial.print("PF= "); Serial.print((p) / (v * i));
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("PF=");
lcd.setCursor(3, 1);
lcd.print((p) / (v * i));

Serial.println();
delay(100);
  }
}

i want to send the value of “duty cycle”, “v”, “i”, “p” and “pf” and show it in processing window, this is my processing code :

import controlP5.*;
import processing.serial.*;

ControlP5 cP5;

Serial arduino;
String val= "0";

void setup() 
{
  size(800, 550);
  println(Serial.list());
  String portName = Serial.list()[0];
  arduino = new Serial(this, portName, 9600);

  cP5 = new ControlP5(this);
  cP5.addSlider("ATUR DUTY CYCLE")
  .setValue(0)
  .setRange(0, 255)
  .setPosition(210, 155)
  .setSize(350, 50)
  ;
  }

  void draw() 
  {
    background(#614DED);
    textSize(20);
    fill(#F5ED00);
    text("PENGENDALIAN INVERTER FULL BRIDGE SATU FASA", 140, 30);
    textSize(20);
    text("SECARA WIRELESS BERBASIS ARDUINO", 200, 60);
    text("TEGANGAN", 210, 300);
    text("ARUS", 460, 300);
    text("FAKTOR DAYA", 210, 400);
    text("DAYA", 460, 400);

   if (arduino.available() >0) {
   delay(100);
   val=arduino.readString();
   }  

    textSize(18);
    fill(#FAFF08);
    text(val, 210, 330 );

    textSize(18);
    fill(#FAFF08);
    text(val, 460, 330);

    textSize(18);
    fill(#FAFF08);
    text(val, 210, 430);

    textSize(18);
    fill(#FAFF08);
    text(val, 460, 430);
    }

   void controlEvent(ControlEvent theEvent) 
  {
   if (theEvent.isController()) {
   int val = int(theEvent.getController().getValue());
   arduino.write(val);
      }
   }

Thank you very much :slight_smile:

if you want to send multiple data from the arduino to the processing sketch, the easyest way to doit (at least for me) is to send everything in the same message, and if you can add a timestamp better, something like this:

v&&i&&p&&pf&&timestamp

where the “&&” are separators, so in the processing sketch you just need to split the message. the timestamp would be usefull if you want to make a graph of your data (the timestamp could be the amount of milliseconds from start of your arduino)

You need to remember most ppl are not able to reproduce your setup. You need to describe what your pde does. First question: Are you getting your data? As suggested by @erwrow, you can send the data in the same stream. Check this recent post to see the concept: ArrayIndexOutOfBoundsException: 1 and follow the discussion there.

Also relevant:

https://forum.processing.org/two/discussion/comment/88115/#Comment_88115
https://forum.processing.org/two/discussion/21447/two-sensors-trigger-same-part-of-code#latest

After you get this part ready, then you can check this post to have an idea for plotting data: https://forum.processing.org/two/discussion/comment/100623/#Comment_100623

Also check this as it displays one way to do it and it focus in the concept: https://processing.org/examples/sinewave.html

Last, you can indent your code in the PDE before you post it here. To indent, select your code in the PDE and press ctrl+t. It makes your code easier to read after you paste it and format it here.

Kf

1 Like

You need to remember most ppl are not able to reproduce your setup. You need to describe what your pde does. First question: Are you getting your data? As suggested by @erwrow, you can send the data in the same stream. Check this recent post to see the concept: como crear: 1 1 and follow the discussion there.

oh sure…
what if i want to display the value of the sensor when i set the duty cycle cycle ?

You are right, you do not need to send all the data. Or you could send all the data and just read the field that you want to know about. There are different strategies and it all falls under your design. What is given here is jut a suggestion.

I read your arduino code, so as I understand, you are already sending the data. Your data looks like this:

Serial.print(v); 
Serial.print("V; ");
Serial.print(i); 
Serial.print("A; ");
Serial.print(p); 
Serial.print("W; ");
Serial.print("PF= "); 
Serial.print((p) / (v * i));

Serial.println();
delay(100);

You are printing the label in the reverse order in most of them? It is not common to do that. Instead, you could do:

print("label:");
print(value);
print(",");

and then at the end of the loop() you add println(), exactly as you are doing before to signal the end of the stream for this loop cycle in your arduino side. In Processing, you read data until your encounter the new line character (aka. \n) and then you process the data by splitting it and assigning it to your Processing variables, exactly as it has been discussed in previous posts.

Kf

Ok thank you @kfrajer I have found a solution to my problem :slight_smile: