Controlling two servos with sliders using an ESP32-CAM board

This is my first post here so tell me if I forgot to state anything.
I’m trying to control two separate servos with two separate sliders connected to an ESP32-CAM board. I can’t get the sliders to do anything. The servos are connected to the GPIO 2 and GPIO 4 pins on the board. The sliders in the app are set to V2 and V4 pins. What am I doing wrong?
Blynk library version: 1.0.0-beta.3
Servo library: ServoESP32


#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";

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

Servo servo;

BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  servo.attach(2);
  servo.attach(4);
}

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

Uninstall this and install 0.6.1 instead.

You don’t have any BLYNK_WRITE(vPin) callbacks for these pins in your sketch.

When you do a servo.write(), how does that sketch know which servo it’s supposed to be controlling?
Shouldn’t these servo objects have different names?

Pete.

Thank you, I’ve managed to get some of it working, but now both sliders are controlling the same servo.

New code:


#define BLYNK_PRINT Serial

#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";

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

    
        Servo servo1; 
        int ServoPin1 = 2;
        int currentServoPos1 = 0;

        Servo servo2; 
        int ServoPin2 = 4;
        int currentServoPos2 = 0;


        
        
        void setup()
        {
          // Debug console
          Serial.begin(115200);
          Blynk.begin(auth, ssid, pass);
          servo1.attach(ServoPin1);
          servo1.attach(ServoPin2);
          
          
        }
        
        BLYNK_WRITE(V2)
        {
           

              int pinValue1 = param.asInt(); // assigning incoming value from pin V2 to a variable
              int mappedServo1 = map(pinValue1, 0, 1023, 1, 180);
              currentServoPos1 = servo1.read();
                  Serial.print(" current servo Position is: ");
                  Serial.println(currentServoPos1);
                  Serial.print(" current slider Position is: ");
                  Serial.println(mappedServo1);
            

             if ( mappedServo1 < currentServoPos1)
                  {
                      delay(100);
                      servo1.write(mappedServo1 -1);
                
                  }
                  else if (mappedServo1 > currentServoPos1)
                  {
                     delay(100);
                     servo1.write(mappedServo1 +1);
                    
                 
                  }
                    
                          }

        BLYNK_WRITE(V4)
        {
           

              int pinValue2 = param.asInt(); // assigning incoming value from pin V4 to a variable
              int mappedServo2 = map(pinValue2, 0, 1023, 1, 180);
              currentServoPos1 = servo2.read();
                  Serial.print(" current servo Position is: ");
                  Serial.println(currentServoPos2);
                  Serial.print(" current slider Position is: ");
                  Serial.println(mappedServo2);
            

             if ( mappedServo2 < currentServoPos2)
                  {
                      delay(100);
                      servo1.write(mappedServo2 -1);
                
                  }
                  else if (mappedServo2 > currentServoPos2)
                  {
                     delay(100);
                     servo1.write(mappedServo2 +1);
                    
                 
                  }
                    
                          }
                        

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

You’re doing a servo1.write here, but it should be a servo2.write.

Pete.

It’s past midnight here and I’m not thinking well… fixed it. Thanks Pete!

1 Like

can I have your code who works ?

@lesfouduDIY this might help you :