Can someone help me with my code?

Hello. Could you please show serial output?

You can move the IF statement for switching relays to the BLYNK_WRITE part. This way it will probably mess up things. It’s recommended to only have Blynk.run() in the loop() part and a timer, if needed :slight_smile:

Thanks for your comments. I added a counter to the BLYNK_WRITE function and kept the serial print statments for relay on/relay off, and added then removed a print statement in the void loop because the void loop was registering every time. The issue appears to be in the Blynk Write function. I can click the virtual button 4 times on 1 second intervals and watch the counter value increase, while the relay on/relay off print statements toggle properly on the serial port, and I watch the LED go on and off on the relay. However every time at about the 5th interval, it stops responding, and then ten or twenty seconds later it starts responding again. It seems to correlate to the Your Arduino Uno was Disconnected on the dashboard.

BLYNK_WRITE(V1){
    value=param.asInt();
    Serial.print("Blynk_Write Count:  ");
    countBlynkWrite=countBlynkWrite+1;
    Serial.println(countBlynkWrite);

The serial port entries are too many so I’ll cut them down here:

[5004] Connecting to cloud.blynk.cc:8442
relay off
relay off
[5332] Ready (ping: 13ms).
Blynk_Write Count: 1
relay off
relay off
relay off
Blynk_Write Count: 2
relay on
relay on
relay on
Blynk_Write Count: 3
relay off
relay off
relay off
Blynk_Write Count: 4
relay on
relay on
relay on
…now wait a long time with unresponsive virtual button
[20875] Ready (ping: 17ms).
relay on
relay on
Blynk_Write Count: 5
relay on
relay on
relay on
Blynk_Write Count: 6
relay off
relay off
relay off

Lichtsignaal, thanks for your advice. I tried relocating the IF statement to BLYNK_WRITE but it didn’t seem to change anything. A timer could be a good idea, too.

Late to the party I know :stuck_out_tongue:
I see the below part is not required for the sketch to work

 BLYNK_LOG("Got a value: %i", param.asInt());
  if (button == 1)
{
  BLYNK_LOG("HIGH");
}else{
  BLYNK_LOG("LOW");
 }

Also, can we move the below code to BLYNK_WRITE{} instead of keeping it in loop()? I was under the impression that Blynk.run() should be able to handle all this and keep the loop() clean.

  if (button == digitalRead(0))
  {
  digitalWrite(2, HIGH);
  Serial.println("lamp on");
}else{
  digitalWrite(2, LOW);
  Serial.println("lamp off");
}

Thanks mvkaxon,
I did comment out the BLYNK_LOG function and Lichgsignaal had also suggested to move the IF statement but that didn’t seem to resolve the issue, either.

Thanks for all of the advice but I’m still having the same issues. The relay is responsive from the virtual button for a few seconds, then it’s not, then it is again. Sometimes the non-responsive times are up to 30 seconds. For remote use it complicates things because you can’t be sure if it registered or not.