I want to use two buttons to control one light (Blynk and RF remote) sync issue

Note: This is my first post here, and I'm sure there are plenty of bad practices in the code. The code is "functioning," but not exactly how I want it to. Any tips with general improvements to the code are appreciated as well.

I'm working on a project where I have two buttons, one implemented using the Blynk platform and the other using an RF remote control, both controlling the state of a light (which is then used to control the servo to turn the light switch ON/OFF physically). The state of the light is tracked using a shared Boolean variable named LightState. When LightState is false, the light is turned off, and when it's true, the light is turned on.

Hardware used:

Hardware Connections:

  • SG90 Servo connected to GPIO 14
  • RXB6 433 MHz receiver module connected to GPIO 4

The Problem:
The issue I'm encountering involves the synchronization between these two buttons. I want both buttons to be in perfect sync with each other. In other words, if one button turns the light on, the other button should recognize this change and turn the light off when it's pressed, and vice versa.

Behaviour Description:

  1. When I press the Blynk button first to turn the light on, it works as expected. The RF remote button in code recognizes that the light is on (as boolean LightState is already set properly by the Blynk button) and can turn it off without (sync) issues.
  2. However, when I use the RC remote first to turn the light on, the two buttons get out of sync. The Blynk button sometimes seems to be unaware of the current state of the light and attempts to turn it on again, even though it's already on (and vice versa).

Expected issue:
I believe that the issue of sync might be caused by the flow of code, as the Blynk function comes earlier in the code and the RF remote code is in a void loop. So, the Blynk function sets the "LightState" properly, which is used by the RF remote code, in a void loop and works as expected. However, an issue arises when we do it the other way around, where the "LightState" is set in the later part of the code in the void loop by the RF remote code and then used by the Blynk function in the earlier part of the code.

Code explanation (from what I understand):

For the Blynk IoT Button:

When the button from the Blynk IoT App is pressed, the received value (integer 1 or 0) is stored in the "LightState" boolean variable.

Next, I check if the value received is 1 or 0 and use the switch...case accordingly. If the value is 0, then I want the servo position to be set to 20 degrees. If the value is 1, then I want the servo position to be set to 160 degrees.

For the RF remote button:

First I check if the button is pressed.

Since the RF remote sends just a single value "7749524", I use another boolean variable, "buttonPressed," to keep track of the button state and to toggle the "LightState" accordingly.

Next, if the button was pressed, like with the Blynk IoT button, if the "LightState" = 0, then I want the servo position to be set to 20 degrees. If "LightState" = 1, then I want the servo position to be set to 160 degrees.

Complete Code:

#define BLYNK_TEMPLATE_ID ""    // Define Blynk template ID
#define BLYNK_TEMPLATE_NAME ""  // Define Blynk template name
#define BLYNK_AUTH_TOKEN ""     // Define Blynk authentication token
#define BLYNK_PRINT Serial      // Use the Serial interface for Blynk

char ssid[] = "";  // Define Wi-Fi SSID
char pass[] = "";  // Define Wi-Fi password

#include <WiFi.h>              // Include the WiFi library
#include <WiFiClient.h>        // Include the WiFiClient library
#include <BlynkSimpleEsp32.h>  // Include the Blynk library for ESP32
#include <ESP32Servo.h>        // Include the ESP32Servo library for servo control
#include <RCSwitch.h>          // Include the RCSwitch library for remote control

RCSwitch mySwitch = RCSwitch();  // Create an instance of the RCSwitch class for remote control

int Servo_Position;               // Variable to hold the servo position
int Default_Servo_Position = 90;  // Default servo position

bool LightState = false;     // Track the state of the light
bool buttonPressed = false;  // Track if the button was pressed

int delay_time = 200;  // Delay time in milliseconds

Servo servo;  // Create a servo object

BLYNK_WRITE(V5) {  // Blynk virtual pin handler

  LightState = (param.asInt());         // Read the value from Blynk virtual pin V5
  servo.write(Default_Servo_Position);  // Set the servo to the default position

  switch (LightState) {                     // Check the state of the light
    case false:                             // If light is off
      Servo_Position = 20;                  // Set servo position to 20 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
    case true:                              // If light is on
      Servo_Position = 160;                 // Set servo position to 160 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
  }
  Serial.print("Light state: ");            // Print the light state
  Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
  Serial.print(" / ");
  Serial.print("Servo Position: ");
  Serial.println(Servo_Position);  // Print the Servo_Position
}

void setup() {
  Serial.begin(115200);                       // Start serial communication
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);  // Initialize Blynk with the authentication token and Wi-Fi credentials
  mySwitch.enableReceive(4);                  // Enable receiving signals on GPIO pin 4
  servo.attach(14);                           // Attach the servo to GPIO pin 14
}

void loop() {

  Blynk.run();  // Run Blynk

  // Check if a signal has been received
  if (mySwitch.available()) {
    // Read the received signal
    unsigned long rc_code = mySwitch.getReceivedValue();

    Serial.print("Received ");                  // Print "Received"
    Serial.print(mySwitch.getReceivedValue());  // Print the received value
    Serial.print(" / ");

    // Check if the signal is valid and corresponds to the desired button
    if (rc_code != 0 && rc_code == 7749524) {  // If a valid signal is received
      LightState = !LightState;                // Toggle the light state
      buttonPressed = true;                    // Set buttonPressed to true
    }

    delay(100);  // Delay to avoid processing multiple times for a single button press

    mySwitch.resetAvailable();  // Reset the received value for the next signal
  }

  // Check if the button was pressed
  if (buttonPressed) {
    // Toggle the servo position based on the updated state
    switch (LightState) {
      case false:             // If light is off
        Servo_Position = 20;  // Set servo position to 20 degrees
        break;
      case true:               // If light is on
        Servo_Position = 160;  // Set servo position to 160 degrees
        break;
    }

    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Servo_Position);          // Move the servo to the specified position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position

    Serial.print("Light state: ");            // Print the light state
    Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
    Serial.print(" / ");

    Serial.print("Servo Position: ");
    Serial.println(Servo_Position);  // Print the Servo_Position

    buttonPressed = false;  // Reset the button state
  }
}



Nonsense! I have used such devices for a remote trigger for a bird trap. The device can be programmed for various responses, but comes programmed for "push on" and "push off". The remote does not can cannot receive anything.

The more words, the more knowledge is missing.

Sure but where? In a simulator, on a board, in the pocket...?

That's what You think tells facts. Schematics tell it all. What members don't know about or don't think about very often contains the issues.

Where is the system description? Button in Blynk, buttons any ware....

Sorry, no intention to keep on reading Your post.

Keep it simple are words often used. Try it.

I think there has been a misunderstanding.

The RF remote just sends the same signal/code each time it's pressed. Code is used to toggle the light on/off each time the RF remote button is pressed. The code also checks the boolean variable "LightState" (whether the light is on/off) and that is then used to sync the two buttons (Blynk button and the RF remote button). So, when I press the Blynk button first to turn the light on, it works as expected. The RF remote button in the code recognizes that the light is on (as the boolean variable "LightState" is already set properly by the Blynk button) and can turn it off without (sync) issues.

I believe that the issue of sync might be caused by the flow of code, as the Blynk function comes earlier in the code and the RF remote code is in a void loop. So, the Blynk function sets the "LightState" properly, which is used by the RF remote code, in a void loop and works as expected. However, an issue arises when we do it the other way around, where the "LightState" is set in the later part of the code in the void loop by the RF remote code and then used by the Blynk function in the earlier part of the code.

Blynk function:

BLYNK_WRITE(V5) {  // Blynk virtual pin handler

  LightState = (param.asInt());         // Read the value from Blynk virtual pin V5
  servo.write(Default_Servo_Position);  // Set the servo to the default position

  switch (LightState) {                     // Check the state of the light
    case false:                             // If light is off
      Servo_Position = 20;                  // Set servo position to 20 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
    case true:                              // If light is on
      Servo_Position = 160;                 // Set servo position to 160 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
  }
  Serial.print("Light state: ");            // Print the light state
  Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
  Serial.print(" / ");
  Serial.print("Servo Position: ");
  Serial.println(Servo_Position);  // Print the Servo_Position
}

RF remote code:

  // Check if a signal has been received
  if (mySwitch.available()) {
    // Read the received signal
    unsigned long rc_code = mySwitch.getReceivedValue();

    Serial.print("Received ");                  // Print "Received"
    Serial.print(mySwitch.getReceivedValue());  // Print the received value
    Serial.print(" / ");

    // Check if the signal is valid and corresponds to the desired button
    if (rc_code != 0 && rc_code == 7749524) {  // If a valid signal is received
      LightState = !LightState;                // Toggle the light state
      buttonPressed = true;                    // Set buttonPressed to true
    }

    delay(100);  // Delay to avoid processing multiple times for a single button press

    mySwitch.resetAvailable();  // Reset the received value for the next signal
  }

  // Check if the button was pressed
  if (buttonPressed) {
    // Toggle the servo position based on the updated state
    switch (LightState) {
      case false:             // If light is off
        Servo_Position = 20;  // Set servo position to 20 degrees
        break;
      case true:               // If light is on
        Servo_Position = 160;  // Set servo position to 160 degrees
        break;
    }

    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Servo_Position);          // Move the servo to the specified position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position

    Serial.print("Light state: ");            // Print the light state
    Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
    Serial.print(" / ");

    Serial.print("Servo Position: ");
    Serial.println(Servo_Position);  // Print the Servo_Position

    buttonPressed = false;  // Reset the button state
  }

I have updated the post to make it more clear.

Perhaps it is a logic problem that is expressed in the "flow" of the code.

In case it is a logic problem, any ideas on how to go about troubleshooting and fixing that?

The very first thing to do is to consider exactly what you want to do with the two buttons, especially the part where each button will have to know the status of the other button. Be concerned with knowing when a button is pressed, not while it is pressed. That seems to be the problem that you currently have.

So, when you discover that a button has been pressed, what do you want to do will depend on the status of the other button.

I apologise if the post is confusing; as I mentioned, this is my first such post. I'll try my best to improve it with feedback. I have already updated the post a few times now.

As told in the next line:

So, as mentioned above, one button is in the Blynk IoT App and the other is the RF remote control button (which I linked in the hardware section of the post).

I have included pictures in the post now for context (at first I wasn't even sure how to do that).


I didn't have a schematic for this particular set-up as I was just testing around, and there were just two main connections that were also mentioned in the post. I just created the schematic, and I have included it in the post now.

I'm not sure what you mean by system description here. I tried to provide all the details of the project in the post above; let me know if there's something specific you believe is missing, and I can provide the info in reply and update the post accordingly.

Thanks for all the feedback! I hope you understand that again, this is my first post; it takes time to learn from the feedback and update the post.

I'm not exactly sure what to update in the code.

I can explain how I'm going about it for now:

For the Blynk IoT Button:

When the button from the Blynk IoT App is pressed, the received value (integer 1 or 0) is stored in the "LightState" boolean variable.

Next, I check if the value received is 1 or 0 and use the switch...case accordingly. If the value is 0, then I want the servo position to be set to 20 degrees. If the value is 1, then I want the servo position to be set to 160 degrees.

For the RF remote button:

First I check if the button is pressed.

Since the RF remote sends just a single value "7749524", I use another boolean variable, "buttonPressed," to keep track of the button state and to toggle the "LightState" accordingly.

Next, if the button was pressed, like with the Blynk IoT button, if the "LightState" = 0, then I want the servo position to be set to 20 degrees. If "LightState" = 1, then I want the servo position to be set to 160 degrees.

Don't do that. Add new information in the oncoming replies. Else the replies already given loses their points.

Sorry. The post more and more looks like a mess. I'm out.

I have added information in the replies. I updated the post so that if someone new reads it, it'll be easier for them. I thought it'd be helpful, but from your reply, I guess that's not how it works. I'm still learning!

Anyway, no worries. It's all good - have a nice day.

FORGET all your code. Work on a logic table based on your TWO buttons. Each button has an off state and an on state. What do you want to happen, in words, for each of the four possible situations? Once you have cleared that up, you can write code to make it happen.

Blynk IoT ... library?

FIXED

I knew that the solution would be quite simple, and I had an idea of what was missing, but I wasn't exactly sure how to implement it. I knew that two buttons behave differently, i.e., the Blynk IoT App button outputs two values (1 or 0), and the RF Remote button outputs a single value; hence, a boolean variable is used to toggle using the single value received.

So, I fixed the code by changing the BLYNK_WRITE(V5) function to toggle the "LightState" variable. Now I can turn the light on or off with either button, and they do not contradict each other. For example, if the Blynk IoT App button turns the light on, then if the RF Remote button is pressed, it will turn the light off, and vice versa.

Thanks, @Paul_KD7HB for your time and for trying to help. I appreciate it!

Original:

Fix:

BLYNK_WRITE(V5) {

  int Blynk_V5 = (param.asInt());  // Read the value from Blynk virtual pin V5
  LightState = !LightState;        // Toggle the LightState

  servo.write(Default_Servo_Position);

  switch (LightState) {                     // Check the state of the light

Serial output:
Blynk App Button: Light state: ON / Servo Position: 160
RF Remote Button: Received 7749524 / Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
Blynk App Button: Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
Blynk App Button: Light state: OFF / Servo Position: 20
Blynk App Button: Light state: ON / Servo Position: 160
RF Remote Button: Received 7749524 / Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
RF Remote Button: Received 7749524 / Light state: OFF / Servo Position: 20
Blynk App Button: Light state: ON / Servo Position: 160

Complete Code:

#define BLYNK_TEMPLATE_ID ""    // Define Blynk template ID
#define BLYNK_TEMPLATE_NAME ""  // Define Blynk template name
#define BLYNK_AUTH_TOKEN ""     // Define Blynk authentication token
#define BLYNK_PRINT Serial      // Use the Serial interface for Blynk

char ssid[] = "";  // Define Wi-Fi SSID
char pass[] = "";  // Define Wi-Fi password

#include <WiFi.h>              // Include the WiFi library
#include <WiFiClient.h>        // Include the WiFiClient library
#include <BlynkSimpleEsp32.h>  // Include the Blynk library for ESP32
#include <ESP32Servo.h>        // Include the ESP32Servo library for servo control
#include <RCSwitch.h>          // Include the RCSwitch library for remote control

RCSwitch mySwitch = RCSwitch();  // Create an instance of the RCSwitch class for remote control

int Servo_Position;               // Variable to hold the servo position
int Default_Servo_Position = 90;  // Default servo position

bool LightState = false;     // Track the state of the light
bool buttonPressed = false;  // Track if the button was pressed

int delay_time = 200;  // Delay time in milliseconds

Servo servo;  // Create a servo object

BLYNK_WRITE(V5) {

  int Blynk_V5 = (param.asInt());  // Read the value from Blynk virtual pin V5
  LightState = !LightState;        // Toggle the LightState

  servo.write(Default_Servo_Position);

  switch (LightState) {                     // Check the state of the light
    case false:                             // If light is off
      Servo_Position = 20;                  // Set servo position to 20 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
    case true:                              // If light is on
      Servo_Position = 160;                 // Set servo position to 160 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
  }
  Serial.print("Blynk App Button: ");       // Print the button type
  Serial.print("Light state: ");            // Print the light state
  Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
  Serial.print(" / ");
  Serial.print("Servo Position: ");
  Serial.println(Servo_Position);  // Print the Servo_Position
}

void setup() {
  Serial.begin(115200);                       // Start serial communication
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);  // Initialize Blynk with the authentication token and Wi-Fi credentials
  mySwitch.enableReceive(4);                  // Enable receiving signals on GPIO pin 4
  servo.attach(14);                           // Attach the servo to GPIO pin 14
}

void loop() {

  Blynk.run();  // Run Blynk

  // Check if a signal has been received
  if (mySwitch.available()) {
    // Read the received signal
    unsigned long rc_code = mySwitch.getReceivedValue();

    Serial.print("RF Remote Button: ");         // Print the button type
    Serial.print("Received ");                  // Print "Received"
    Serial.print(mySwitch.getReceivedValue());  // Print the received value
    Serial.print(" / ");

    // Check if the signal is valid and corresponds to the desired button
    if (rc_code != 0 && rc_code == 7749524) {  // If a valid signal is received
      LightState = !LightState;                // Toggle the light state
      buttonPressed = true;                    // Set buttonPressed to true
    }

    delay(100);  // Delay to avoid processing multiple times for a single button press

    mySwitch.resetAvailable();  // Reset the received value for the next signal
  }

  // Check if the button was pressed
  if (buttonPressed) {
    // Toggle the servo position based on the updated state
    switch (LightState) {
      case false:             // If light is off
        Servo_Position = 20;  // Set servo position to 20 degrees
        break;
      case true:               // If light is on
        Servo_Position = 160;  // Set servo position to 160 degrees
        break;
    }

    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Servo_Position);          // Move the servo to the specified position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position

    Serial.print("Light state: ");            // Print the light state
    Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
    Serial.print(" / ");

    Serial.print("Servo Position: ");
    Serial.println(Servo_Position);  // Print the Servo_Position

    buttonPressed = false;  // Reset the button state
  }
}

Here:

Using the same concept, I added a third button (touch button using the ESP32 touchRead function). All three buttons work well now.

Same method to toggle LightState:

  if (touchRead(1) > touch_threshold) {  // If the touch sensor value is greater than the threshold
    LightState = !LightState;            // Toggle the light state
    delay(delay_time / 2);               // Delay for a certain time
    buttonPressed = true;                // Set buttonPressed to true
    Serial.print("Touch Button: ");      // Print the button type
    Serial.print("Touch pin_1 value: ");
    Serial.print(touchRead(1));  // Print value of T1
    Serial.print(" / ");
  }

Serial output:
Blynk App Button: Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
Touch Button: Touch pin_1 value: 38556 / Light state: OFF / Servo Position: 20
Blynk App Button: Light state: ON / Servo Position: 160
Blynk App Button: Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
Touch Button: Touch pin_1 value: 32512 / Light state: OFF / Servo Position: 20
Touch Button: Touch pin_1 value: 38530 / Light state: ON / Servo Position: 160
RF Remote Button: Received 7749524 / Light state: OFF / Servo Position: 20
RF Remote Button: Received 7749524 / Light state: ON / Servo Position: 160
Blynk App Button: Light state: OFF / Servo Position: 20

Complete Code:

#define BLYNK_TEMPLATE_ID ""    // Define Blynk template ID
#define BLYNK_TEMPLATE_NAME ""  // Define Blynk template name
#define BLYNK_AUTH_TOKEN ""     // Define Blynk authentication token
#define BLYNK_PRINT Serial      // Use the Serial interface for Blynk

char ssid[] = "";  // Define Wi-Fi SSID
char pass[] = "";  // Define Wi-Fi password

#include <WiFi.h>              // Include the WiFi library
#include <WiFiClient.h>        // Include the WiFiClient library
#include <BlynkSimpleEsp32.h>  // Include the Blynk library for ESP32
#include <ESP32Servo.h>        // Include the ESP32Servo library for servo control
#include <RCSwitch.h>          // Include the RCSwitch library for remote control

RCSwitch mySwitch = RCSwitch();  // Create an instance of the RCSwitch class for remote control

int Servo_Position;               // Variable to hold the servo position
int Default_Servo_Position = 90;  // Default servo position

int delay_time = 200;  // Delay time in milliseconds

const int touch_threshold = 32000;  // Set the touch threshold

bool LightState = false;     // Track the state of the light
bool buttonPressed = false;  // Track if the button was pressed

Servo servo;  // Create a servo object

BLYNK_WRITE(V5) {  // Blynk virtual pin handler

  int Blynk_V5 = (param.asInt());  // Read the value from Blynk virtual pin V5
  LightState = !LightState;        // Toggle the LightState

  servo.write(Default_Servo_Position);

  switch (LightState) {                     // Check the state of the light
    case false:                             // If light is off
      Servo_Position = 20;                  // Set servo position to 20 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
    case true:                              // If light is on
      Servo_Position = 160;                 // Set servo position to 160 degrees
      servo.write(Servo_Position);          // Move the servo to the specified position
      delay(delay_time);                    // Delay for a certain time
      servo.write(Default_Servo_Position);  // Move the servo back to the default position
      break;
  }
  Serial.print("Blynk App Button: ");       // Print the button type
  Serial.print("Light state: ");            // Print the light state
  Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
  Serial.print(" / ");
  Serial.print("Servo Position: ");
  Serial.println(Servo_Position);  // Print the Servo_Position
}

void setup() {
  Serial.begin(115200);                       // Start serial communication
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);  // Initialize Blynk with the authentication token and Wi-Fi credentials
  mySwitch.enableReceive(4);                  // Enable receiving signals on GPIO pin 4
  servo.attach(14);                           // Attach the servo to GPIO pin 14
}

void loop() {

  Blynk.run();  // Run Blynk

  // Check if a signal has been received
  if (mySwitch.available()) {
    // Read the received signal
    unsigned long rc_code = mySwitch.getReceivedValue();

    if (touchRead(1) < touch_threshold) {  // If the touch sensor value is less than the threshold
      Serial.print("RF Remote Button: ");  // Print the button type
    }

    Serial.print("Received ");                  // Print "Received"
    Serial.print(mySwitch.getReceivedValue());  // Print the received value
    Serial.print(" / ");

    // Check if the signal is valid and corresponds to the desired button
    if (rc_code != 0 && rc_code == 7749524) {  // If a valid signal is received
      LightState = !LightState;                // Toggle the light state
      buttonPressed = true;                    // Set buttonPressed to true
    }

    delay(100);  // Delay to avoid processing multiple times for a single button press

    mySwitch.resetAvailable();  // Reset the received value for the next signal
  }

  if (touchRead(1) > touch_threshold) {  // If the touch sensor value is greater than the threshold
    LightState = !LightState;            // Toggle the light state
    delay(delay_time / 2);               // Delay for a certain time
    buttonPressed = true;                // Set buttonPressed to true
    Serial.print("Touch Button: ");      // Print the button type
    Serial.print("Touch pin_1 value: ");
    Serial.print(touchRead(1));  // Print value of T1
    Serial.print(" / ");
  }

  // Check if the button was pressed
  if (buttonPressed) {
    // Toggle the servo position based on the updated state
    switch (LightState) {
      case false:             // If light is off
        Servo_Position = 20;  // Set servo position to 20 degrees
        break;
      case true:               // If light is on
        Servo_Position = 160;  // Set servo position to 160 degrees
        break;
    }

    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Servo_Position);          // Move the servo to the specified position
    delay(delay_time);                    // Delay for a certain time
    servo.write(Default_Servo_Position);  // Move the servo back to the default position

    Serial.print("Light state: ");            // Print the light state
    Serial.print(LightState ? "ON" : "OFF");  // Print "ON" if LightState is true, otherwise "OFF"
    Serial.print(" / ");

    Serial.print("Servo Position: ");
    Serial.println(Servo_Position);  // Print the Servo_Position

    buttonPressed = false;  // Reset the button state
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.