Slider Value Delay

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

We can’t run your program. Could you describe this delay? How do you see it? What is a long delay for you? You are referring to a delay every time you interact with a controlP5 controller, right? Please, also consider stripping your code to a minim example where you can still observe this problem.

Also I am not sure if accessing the portSerial in setup is proper. This is not related to your chief issue btw but more of a general comment.

Kf

Hello @kfrajer sorry I did not include the arduino code, but I have included it above.

For example, I applied it to an LED light, I controlled the LED using slider in processing.
When I shift the slider to turn on the LED, the command response is very long
But when I click the mouse and press the slider to a certain value (example 127) the LED can turn on immediately, so the point is that I can’t use a slider as usual.

Sorry if my english is not good, i use it google translate :smiley:

What do you mean by that?

I need to differentiate the two events to properly help you. When you say shift, are you referring to interacting with the slider via the keyboard?

Kf

@kfrajer
Yes, the “shift” I mean here is the command done by the keyboard / mouse in PC.

Whereas the “slider” that I mean is interface from processing in desktop.

Sounds like Processing is sending 60 values per second, which the serial port can’t digest fast enough. If that’s the case, is there any throttling library / function in Processing (like this one for js http://benalman.com/projects/jquery-throttle-debounce-plugin/ ) ?

The idea would be to reduce the rate at which it sends data while dragging the slider. One way would be to store the time stamp when you send data, and only send data if enough milliseconds have passed since last time. It might be also good to always send the value when stopping the dragging, otherwise the last value may not be sent due to discarding of too-frequent data.

A quick test: set frameRate(15) inside setup (Processing) and see if there’s still latency.

1 Like

In simple code, rate limiting may look like this:

int waitMillis = 1000 / 15; // second number = desired update rate per second
int lastSendMillis = 0;
void draw() {
  int now = millis();
  if(now > lastSendMillis + waitMillis) {
    lastSendMillis = now;
    println("SEND MSG TO ARDUINO HERE", now);
  }
}

it seems like I haven’t found a solution to this problem :cry:

What did you try and how did it fail?

I insert the sketch that you recommend in the comments, but the results remain the same.

What happened when you reduced the frameRate? Could you show your updated code?

You need to be more helpful if you want to solve your problem. You need to reduce your code to a minimum so you can show the section of code that is problematic. Unfortunately it is not clear what you want to do as the description of “shift” and “slider” are not understood based on your description.

Kf

@hamoid
when I reduce the framerate there is no change at all

@kfrajer
The sketch I made consists of two parts, the first for setting the “Slider” and the second for reading the current and voltage sensors. The value of the sensor is obtained if the “Slider” in Processing is dragged.

I can run sketches one by one there is no problem, but when I put the two sketches together “Sider” on the Processing cannot run the same as when I tried it without sketching the current and voltage sensors.

Can you indicate the different parts in your code above? What part are you referring to setting the slider and what part reads the voltage and current?

Notice this code is likely ineffective in setup:

if (portSerial.available() >0) {
    delay(100);
    dutycycle=portSerial.readString();
     }

When you read serial, you need to do ensure you verify the data you are processing. I dont know if you are getting any errors in the last part of your code. Check this:

    void serialEvent (Serial portSerial)
    {
     String strMasukan = portSerial.readStringUntil ('\n');


    if(strMasukan==null) return;  //DO nothing

   
    if(strMasukan.size() < 27)  return;   //DO nothing


    //If the code makes it this far, it is is very likely you have good data

     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);
      }

A better example here

Kf

@kfrajer
This is the arduino code for setting PWM Duty Cycles which will be arranged in “Processing” using the “Slider” with the title “ATUR DUTY CYCLE”.

#include <TimerOne.h>              // include TimerOne.h

int dutycycle = 0;               // Initailize duty cylce variable as integer data type
int incomingByte = 0; 

void setup()               // Setup function
{
 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
}

void loop()              // loop function starts
{ 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);
  }
    }               // loop function ends

dan yang ini adalah kode arduino untuk pembacaan sensor arus dan tegangan.

#include <SoftwareSerial.h> 
#include <PZEM004T.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
PZEM004T pzem(2,3);  // (RX,TX) connect to TX,RX of PZEM
IPAddress ip(192,168,1,1);


void setup() 
{
 Serial.begin(9600);
 pzem.setAddress(ip);
 lcd.begin(); // lcd rows and columns
 }


void loop()
{
 Serial.read();
  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);
 }

what does it mean to be ineffective ?

It is very likely that code will not execute. However this is not important at the moment. I have to admit I am confused with your post. You presented two arduino code blocks above. How do they related to your processing code?

There are two parts that needs to be clarified from a previous post:

What parts are you referring to in the above?

What sketches are you referring to?

Kf