Control servo with physical button and infra red

Hello, i’m beginner at blynk… I have a question for my project.
I want to control a servo with physical buttons which also used as counter. It goes 180 degree when IR = Low & count = 0. And back into 0 degree after count = 2.
The program ran perfectly with nodemcu & arduino ide. But when I try to control through blynk it doesn’t work. IR always show low state.

Here is the code without blynk


Servo myservo;

int tombol1 = D4;
int tombol2 = D2;
int tombol3 = D1;
int irSensor = D6;
int led = D8;

int pb1;
int pb2;
int pb3;

int CountOk = 0;
int Repair = 0;
int Scrap = 0;
int count = 0;
int statepb1 = 0;
int statepb2 = 0;
int statepb3 = 0;

int sw = 0;
int bacaIR = 0;
int CT = 5;

double target;
double aktual=1.0;
double efisiensi;

// millis fuction//
unsigned long prevMillis1 = 0;
unsigned long prevMillis3 = 0;
 long interval1 = 6000;
 long interval2 = 2000;
 long interval3 = 6000;
 
void setup() {
Serial.begin(9600);
  // put your setup code here, to run once:
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(tombol3, INPUT_PULLUP);
pinMode(irSensor, INPUT);
pinMode(led, OUTPUT);
myservo.attach(D5);
}

void loop() {
unsigned long currentMillis = millis();
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  // put your main code here, to run repeatedly:
if (digitalRead(irSensor) == LOW && count == 0 && (currentMillis - prevMillis1 >= interval1)){
prevMillis1 = currentMillis;
myservo.write(180); // close servo
}
else if (count == 2 && (currentMillis - prevMillis1 >= interval2)){
prevMillis1 = currentMillis;
count = 0;
myservo.write(0);// open
}

  int pb1 = digitalRead(tombol1);
  int pb2 = digitalRead(tombol2);
  int pb3 = digitalRead(tombol3);  
  int irState = digitalRead(irSensor);
  
  if (pb1 != statepb1) {   // if the state has changed, increment the counter
  if(pb1 == LOW){ 
      CountOk++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }
  statepb1 = pb1;   // save the current state as the last state, for next time through the loop

  if (pb2 != statepb2) {   // if the state has changed, increment the counter
  if(pb2 == LOW){ 
      Repair++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }
  statepb2 = pb2;   // save the current state as the last state, for next time through the loop

  if (pb3 != statepb3) {   // if the state has changed, increment the counter
  if(pb3 == LOW){ 
      Scrap++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }  
  statepb3 = pb3;
}```

@trias please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Once you’ve done that, we’ll explain what is wrong with your code.

Pete.

thankyou @PeteKnight

And here is my code with blynk

#include <Servo.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
//#include <SimpleTimer.h>
//SimpleTimer timer;
BlynkTimer timer;

char auth[] = "auth";
char ssid[] = "ssid";     // Your WiFi credentials.
char pass[] = "pass";  // Set password to "" for open networks.

Servo myservo;

int tombol1 = D4;
int tombol2 = D2;
int tombol3 = D1;
int irSensor = D6;

int pb1;
int pb2;
int pb3;

double CountOk = 0;
double Repair = 0;
double Scrap = 0;
int count = 0;
int statepb1 = 0;
int statepb2 = 0;
int statepb3 = 0;

int sw = 0;
int CT = 5;

double target;
double aktual=1.0;
double efisiensi;

// millis fuction//
unsigned long prevMillis1 = 0;
unsigned long prevMillis3 = 0;
 long interval1 = 6000;
 long interval2 = 2000;
 long interval3 = 6000;

void servoopenclose(){
unsigned long currentMillis = millis();
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  // put your main code here, to run repeatedly:
if (digitalRead(irSensor) == LOW && count == 0 && (currentMillis - prevMillis1 >= interval1)){
prevMillis1 = currentMillis;
myservo.write(180); // close servo
}
else if (count == 2 && (currentMillis - prevMillis1 >= interval2)){
prevMillis1 = currentMillis;
count = 0;
myservo.write(0);// open
}
}


void counter(){
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  int pb1 = digitalRead(tombol1);
  int pb2 = digitalRead(tombol2);
  int pb3 = digitalRead(tombol3);
  int irState = digitalRead(irSensor);
  
  if (pb1 != statepb1) {   // if the state has changed, increment the counter
  if(pb1 == LOW){ 
      CountOk++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }
  statepb1 = pb1;   // save the current state as the last state, for next time through the loop

  if (pb2 != statepb2) {   // if the state has changed, increment the counter
  if(pb2 == LOW){ 
      Repair++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }
  statepb2 = pb2;   // save the current state as the last state, for next time through the loop

  if (pb3 != statepb3) {   // if the state has changed, increment the counter
  if(pb3 == LOW){ 
      Scrap++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }  
  statepb3 = pb3;

  Blynk.virtualWrite(1, CountOk);      
  Blynk.virtualWrite(2, Repair); 
  Blynk.virtualWrite(3, Scrap);
  Blynk.virtualWrite(4, target);      
  Blynk.virtualWrite(5, aktual); 
  Blynk.virtualWrite(6, efisiensi);
}
 
void setup() {
Serial.begin(9600);
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(tombol3, INPUT_PULLUP);
pinMode(irSensor, INPUT);
myservo.attach(D5);
SPI.begin();      
Blynk.begin(auth, ssid, pass); 
timer.setInterval(500L, counter);
timer.setInterval(500L, servoopenclose);
}

void loop() {
Blynk.run();
timer.run();
}```

Without seeing how you’ve tried to implement Blynk within this code, it’s difficult to say exactly what is causing the symptoms you describe. However, what is clear is that the code you have shared has a structure that is incompatible with Blynk. This document describes the basic changes you need to make to the code to get it working with a Blynk:

The main tasks are to move all of your existing code out of the void loop and move it into a function that is called with a timer. This will allow you to remove the delay command entirely.

Pete.

@PeteKnight sorry for posting the blynk code late…
my code

@PeteKnight i tried to use simple timer. But still not sure is my code was right? Sure it didn’t work because my servo doesn’t show any movement :smile:

unsigned long currentMillis = millis();
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  // put your main code here, to run repeatedly:
if (digitalRead(irSensor) == LOW && count == 0 && (currentMillis - prevMillis1 >= interval1)){
prevMillis1 = currentMillis;
timer.setTimeout(6000, servoclose);
}
else if (count == 2 && (currentMillis - prevMillis1 >= interval2)){
prevMillis1 = currentMillis;
count = 0;
timer.setTimeout(2000, servoopen);
}
}

void servoclose(){
  myservo.write(180); // close servo
  delay (50);
}
void servoopen(){
  myservo.write(0); // open servo
  delay (50);
}

You shouldn’t be using SimpleTimer, BlynkTimer is better.

The correct procedure should be:

  • Include the Blynk libraries, so that you have access to BlynkTimer, but don’t do anything else with Blynk (no Blynk.begin, run, virtualWrites etc).

  • Restructure your code so that it is Blynk friendly and get it working correctly,

  • Add-in the Blynk related code and re-test

Until you get to Step 3, this is just a C++ coding issue, not Blynk related.

Pete.

@PeteKnight Ok, i will use BlynkTimer…
I want to both control the servo and stay monitoring the output of the counter with my android. The counter seems to be ok on my phone. But my servo still not move…
If you don’t mind, this is the flow chart and design of my project

how can i move my servo along with running the counter?

So what does your restructured code look like after doing this:

Pete.

@PeteKnight I have restructured my program.
It works perfectly without blynk. The count status should be 0 after reaching value 2. But when I uncomment the blynk code, the count value continues to rise so that my servo doesn’t work anymore. Can you give me a clue?

#include <Servo.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

/*char auth[] = "auth";
char ssid[] = "ssid";     // Your WiFi credentials.
char pass[] = "pass";  // Set password to "" for open networks.
*/

Servo myservo;

int tombol1 = D4;
int tombol2 = D2;
int tombol3 = D1;
int irSensor = D6;
int led = D8;

int pb1;
int pb2;
int pb3;

double CountOk = 0;
double Repair = 0;
double Scrap = 0;
int count = 0;
int statepb1 = 0;
int statepb2 = 0;
int statepb3 = 0;

int irState;
int servoStatus;
int p = 0;

int sw = 0;
int CT = 5;

double target;
double aktual=1.0;
double efisiensi;


void setup() {
Serial.begin(9600);
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(tombol3, INPUT_PULLUP);
pinMode(irSensor, INPUT);
pinMode(led, OUTPUT);
myservo.attach(D5);
/*SPI.begin();      
Blynk.begin(auth, ssid, pass); */
timer.setInterval(500L, counter);
timer.setInterval(500L, readlocalservo);
}

void readlocalservo(){
  irState = digitalRead(irSensor);
  if (irState == LOW && p == 0){
    if(count == 0 || count == 1){
      servotutup();
    }
    if(count == 2){
      servobuka_p1();
    }
    }
  
  if (irState == HIGH){
    p == 0;
    servobuka_p0();
  }
}

void aplyservo(){
  if (servoStatus == 1){
    myservo.write(180); // close servo
  }
  else {
    myservo.write(0); // close buka
  }
}

void servotutup(){
  servoStatus = 1;
  aplyservo();
}

void servobuka_p1(){
  p = 1;
  servoStatus = 0;
  aplyservo();
}

void servobuka_p0(){
  count = 0;
  p = 0;
  servoStatus = 0;
  aplyservo();
}


void counter(){
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  int pb1 = digitalRead(tombol1);
  int pb2 = digitalRead(tombol2);
  int pb3 = digitalRead(tombol3);
  int irState = digitalRead(irSensor);
  
  
  if (pb1 != statepb1) {   // if the state has changed, increment the counter
  if(pb1 == LOW){ 
      CountOk++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.print("p = ");
      Serial.println(p);
      Serial.println( );
  }delay(50);
  }
  statepb1 = pb1;   // save the current state as the last state, for next time through the loop

  if (pb2 != statepb2) {   // if the state has changed, increment the counter
  if(pb2 == LOW){ 
      Repair++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }
  statepb2 = pb2;   // save the current state as the last state, for next time through the loop

  if (pb3 != statepb3) {   // if the state has changed, increment the counter
  if(pb3 == LOW){ 
      Scrap++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.println( );
  }delay(50);
  }  
  statepb3 = pb3;

  /*Blynk.virtualWrite(1, CountOk);      
  Blynk.virtualWrite(2, Repair); 
  Blynk.virtualWrite(3, Scrap);
  Blynk.virtualWrite(4, target);      
  Blynk.virtualWrite(5, aktual); 
  Blynk.virtualWrite(6, efisiensi);*/
}
 

void loop() {
//Blynk.run();
timer.run();
}

I’d add some serial print debug info to monitor variable values, especially the value of irState in the readlocalservo function.
Compare the readings you get in the idle state and activated state with Blynk enabled and disabled.

Pete.

Yess, why irState always Low while no object detected?
How can i enable and disable the Blynk app?
Do i have to add virtual button as a switch?

Maybe define it as INPUT_PULLUP like the rest of your pinMode declarations?

Pete.

@PeteKnight thank you for your assistance :bowing_woman:
Now i can run my servo and blynk :grinning:
Can i have one more question? When i press the button, it send slow responses to the serial monitor and blynk. Sometimes it don’t give any response at all. What should i do then?

#include <Servo.h>
//#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;

char auth[] = "IUaTE2u9pPCaJW8Q1RqnZmj5XJQIU6cX";
char ssid[] = "M200";     // Your WiFi credentials.
char pass[] = "11223344";  // Set password to "" for open networks.


Servo myservo;

int tombol1 = D4;
int tombol2 = D2;
int tombol3 = D1;
int irSensor = D6;
int led = D8;

int pb1;
int pb2;
int pb3;

double CountOk = 0;
double Repair = 0;
double Scrap = 0;
int count = 0;
int statepb1 = 0;
int statepb2 = 0;
int statepb3 = 0;

int irState = 0;
int ir;
int servoStatus;
int p = 0;
int adabarang = 0;

int sw = 0;
int CT = 10;

double target;
double aktual=1.0;
double efisiensi;

void setup() {
Serial.begin(9600);
pinMode(tombol1, INPUT_PULLUP);
pinMode(tombol2, INPUT_PULLUP);
pinMode(tombol3, INPUT_PULLUP);
pinMode(irSensor, INPUT_PULLUP);
myservo.attach(D5);
//SPI.begin();      
Blynk.begin(auth, ssid, pass); 
timer.setInterval(1000L, bacair);
timer.setInterval(1000L, counter);
timer.setInterval(1000L, readlocalservo);
}

void bacair(){
  int ir = digitalRead(irSensor);
  if (ir==1 && irState==0){
    irState = 1;
    adabarang = 0;
  }
  else if (ir==0){
    irState = 0;
    adabarang = 1;
  }
}

void readlocalservo(){
  irState = digitalRead(irSensor);
  if (adabarang==1 && p == 0){
    if(count == 0 || count == 1){
      servotutup();
    }
    if(count == 2){
      servobuka_p1();
    }
    }
    

  if (count == 2 && p == 1){
    servobuka_irS1();
  }
  
  if (adabarang==0){
    irState = 1;
    servobuka_p0();
  }
      Serial.print("p ");
      Serial.println(p);
      Serial.print("ir = ");
      Serial.println(ir);
      Serial.print("irState = ");
      Serial.println(irState);
      Serial.print("adabarang = ");
      Serial.println(adabarang);
      Serial.println( );
}

void aplyservo(){
  if (servoStatus == 1){
    myservo.write(180); // close servo
  }
  else {
    myservo.write(0); // close buka
  }
}

void servotutup(){
  servoStatus = 1;  
  timer.setTimer(5000L, aplyservo,1);
  //aplyservo();
}

void servobuka_p1(){
  p = 1;
  servoStatus = 0;
  aplyservo();
}

void servobuka_p0(){
  count = 0;
  p = 0;
  servoStatus = 0;
  adabarang = 0;
  aplyservo();
}

void servobuka_irS1(){
  irState = 1;
  //servoStatus = 0;
  //aplyservo();
  timer.setTimer(3000L, servobuka_p0,1);
}


void counter(){
unsigned long allSeconds=millis()/1000;
target = allSeconds/CT;
aktual = CountOk + Repair;
efisiensi = aktual/target*100;

  int pb1 = digitalRead(tombol1);
  int pb2 = digitalRead(tombol2);
  int pb3 = digitalRead(tombol3);
  
  if (pb1 != statepb1) {   // if the state has changed, increment the counter
  if(pb1 == LOW){ 
      CountOk++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("ir = ");
      Serial.println(ir);
      Serial.print("p = ");
      Serial.println(p);
      Serial.println( );
  }delay(50);
  }
  statepb1 = pb1;   // save the current state as the last state, for next time through the loop

  if (pb2 != statepb2) {   // if the state has changed, increment the counter
  if(pb2 == LOW){ 
      Repair++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("ir = ");
      Serial.println(ir);
      Serial.println( );
  }delay(50);
  }
  statepb2 = pb2;   // save the current state as the last state, for next time through the loop

  if (pb3 != statepb3) {   // if the state has changed, increment the counter
  if(pb3 == LOW){ 
      Scrap++;
      Serial.print("OK Part = "); 
      Serial.print(CountOk);  //optional, displays on the PC monitor the value of COUNT
      Serial.print("\t Repair Part = "); 
      Serial.print(Repair);
      Serial.print("\t NG SCRAP Part = "); 
      Serial.print(Scrap);
      count++;      
      Serial.print("\t count= ");
      Serial.println(count);
      Serial.print("Target= ");
      Serial.print(target);
      Serial.print("\t Aktual= ");
      Serial.print(aktual);
      Serial.print("\t \t Efisiensi=");
      Serial.print(efisiensi);
      Serial.print("%");
      Serial.println( );
      Serial.print("ir = ");
      Serial.println(ir);
      Serial.println( );
  }delay(50);
  }  
  statepb3 = pb3;
      
  Blynk.virtualWrite(1, CountOk);      
  Blynk.virtualWrite(2, Repair); 
  Blynk.virtualWrite(3, Scrap);
  Blynk.virtualWrite(4, target);      
  Blynk.virtualWrite(5, aktual); 
  Blynk.virtualWrite(6, efisiensi);
}
 

void loop() {
Blynk.run();
timer.run();
}

You still have some delays in there, so I’d start by getting rid of those.
You also have three timers that are all being called at EXACTLY the same time, and some of the functions that call have quite a lot of code in them. Try staggering them slightly.
If you search for “staggered timers” you’ll see quite a few discussions and suggestions about ways to achieve this, the best of which is probably a short delay between each one in the void setup.

Pete.