I want to set the value of the duty cycle through the slider in processing, but when I set the slider value (0-255) there is a very long delay. How to overcome it ?
This is my arduino code:
#include <SoftwareSerial.h>
#include <TimerOne.h>
#include <PZEM004T.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
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);
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(); // 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.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.setCursor(10, 0);
lcd.print("I=");
lcd.setCursor(12, 0);
lcd.print(i);
float p = pzem.power(ip);
if (p < 0.0) p = 0.0;
Serial.print(p); Serial.print("W; ");
lcd.setCursor(0, 1);
lcd.print("P=");
lcd.setCursor(2, 1);
lcd.print(p);
float e = pzem.energy(ip);
Serial.print("PF="); Serial.print((p) / (v * i));
lcd.setCursor(9, 1);
lcd.print("PF=");
lcd.setCursor(12, 1);
lcd.print((p) / (v * i));
Serial.println();
delay(100);
}
This is my processing code:
import processing.serial.*;
import controlP5.*;
final int INDEKS_PORT_SERIAL = 0;
Serial portSerial;
ControlP5 cp5;
String dutycycle= "0";
Textlabel labelTegangan;
Textlabel labelArus;
Textlabel labelDaya;
Textlabel labelFaktorDaya;
void setup ()
{
size (800, 550);
portSerial = new Serial (this, Serial.list () [INDEKS_PORT_SERIAL], 9600);
portSerial.bufferUntil ('\n');
cp5 = new ControlP5 (this);
cp5.addSlider("ATUR DUTY CYCLE")
.setValue(0)
.setRange(0, 255)
.setPosition(200, 155)
.setSize(380, 55)
;
cp5.addTextlabel ("labelJudulTegangan")
.setText ("TEGANGAN")
.setPosition (150, 300)
.setColorValue (color(#08006C))
.setFont (createFont("Verdana", 25))
;
labelTegangan = cp5.addTextlabel ("labelTegangan")
.setText ("0")
.setPosition (150, 330)
.setColorValue (color(#FEFF12))
.setFont (createFont("Times", 25))
;
cp5.addTextlabel ("labelJudulArus")
.setText ("ARUS")
.setPosition (460, 300)
.setColorValue (color(#08006C))
.setFont (createFont("Verdana", 25))
;
labelArus = cp5.addTextlabel ("labelArus")
.setText ("0")
.setPosition (460, 330)
.setColorValue (color(#FEFF12))
.setFont (createFont("Times", 25))
;
cp5.addTextlabel ("labelJudulDaya")
.setText ("DAYA")
.setPosition (150, 400)
.setColorValue (color(#08006C))
.setFont (createFont("Verdana", 25))
;
labelDaya = cp5.addTextlabel ("labelDaya")
.setText ("0")
.setPosition (150, 430)
.setColorValue (color(#FEFF12))
.setFont (createFont("Times", 25))
;
cp5.addTextlabel ("labelJudulFaktorDaya")
.setText ("FAKTOR DAYA")
.setPosition (460, 400)
.setColorValue (color(#08006C))
.setFont (createFont("Verdana", 25))
;
labelFaktorDaya = cp5.addTextlabel ("labelFaktorDaya")
.setText ("0")
.setPosition (460, 430)
.setColorValue (color(#FEFF12))
.setFont (createFont("Times", 25))
;
if (portSerial.available() >0) {
delay(100);
dutycycle=portSerial.readString();
}
}
void draw ()
{
background(#00AEE0);
textSize(25);
fill(#FEFF12);
text("PENGENDALIAN INVERTER FULL BRIDGE SATU FASA", 90, 30);
textSize(25);
fill(#FEFF12);
text("SECARA WIRELESS BERBASIS ARDUINO", 175, 60);
}
void controlEvent(ControlEvent theEvent)
{
if (theEvent.isController()) {
int dutycycle = int(theEvent.getController().getValue());
portSerial.write(dutycycle);
}
}
void serialEvent (Serial portSerial)
{
String strMasukan = portSerial.readStringUntil ('\n');
String strTegangan = strMasukan.substring (0, 7);
String strArus = strMasukan.substring (9, 14);
String strDaya = strMasukan.substring (16, 22);
String strFaktorDaya = strMasukan.substring (27);
labelTegangan.setText (strTegangan);
labelArus.setText (strArus);
labelDaya.setText (strDaya);
labelFaktorDaya.setText (strFaktorDaya);
}
Thank you very much