Servo moves 2 times.

Good morning,

We have been in the 3D community for a few months with a problem with the servos.

The point is that with the following code for an Iron Man helmet consisting of:
-2 servos
-1switch
-1 button

We have the problem that when we press the servos they move but for some reason they return to the same position instead of maintaining the angle.

The code should do the following:
Press-> open angle.
Click again -> close angle.

We appreciate any help from now on.

#include "ServoEasing.h"
ServoEasing servoTop;
ServoEasing servoBottom;
const int action_pin = 2;
int location = 31;
// Below numbers should be adjusted in case the facemask does not close/open to desired angle
int bottom_closed = 107;
int top_closed = 167;
int bottom_open = 20;
int top_open = 20;
void setup()
{
 pinMode(action_pin, INPUT_PULLUP);
 servoTop.attach(9);
 servoBottom.attach(10);
 setSpeedForAllServos(190);
 servoTop.setEasingType(EASE_CUBIC_IN_OUT);
 servoBottom.setEasingType(EASE_CUBIC_IN_OUT);
 synchronizeAllServosStartAndWaitForAllServosToStop();
}
void loop()
{
 int proximity = digitalRead(action_pin);
 if (proximity == LOW)
 {
 if (location > bottom_open) {
 servoTop.setEaseTo(top_open);
 servoBottom.setEaseTo(bottom_open);
 synchronizeAllServosStartAndWaitForAllServosToStop();
 location = bottom_open;
 delay(600);
 } else {
 servoTop.setEaseTo(top_closed);
 servoBottom.setEaseTo(bottom_closed);
 synchronizeAllServosStartAndWaitForAllServosToStop();
 location = bottom_closed;
 delay(600);
 }
 }
}

Your button doesn't have a fixed position, while you pressing it - servos move to position, once you release the button - servos return to start.

1 Like

You have a 600ms delay between when you detect button pushes. If you hold the button for more than 600ms, it will look like you pushed it twice.

1 Like

@jim-p So what would you recommend I put? So I try in a moment, maybe that can be the solution. Thanks from now.

If you don't need to push the button more than once every 2 seconds, then change the delays to 2000 and make sure you don't hold the button for more than 2 seconds.

Otherwise, do a search for switch de-bouncing and button pushes.

1 Like

considier

# include "ServoEasing.h"

ServoEasing servoTop;
ServoEasing servoBottom;
const int action_pin = 2;
int location = 31;
// Below numbers should be adjusted in case the facemask does not close/open to desired angle
int bottom_closed = 107;
int top_closed    = 167;
int bottom_open   = 20;
int top_open      = 20;

bool open;
byte butLst;

// -----------------------------------------------------------------------------

void loop  ()
{
    byte but = digitalRead  (action_pin);
    if (butLst != but)  {
        butLst = but;
        delay (20);     // debounce

        if (LOW == but)  {
            open = ! open;
            if (open)  {
                servoTop.setEaseTo    (top_open);
                servoBottom.setEaseTo (bottom_open);
            }
            else  {
                servoTop.setEaseTo    (top_closed);
                servoBottom.setEaseTo (bottom_closed);
            }

            synchronizeAllServosStartAndWaitForAllServosToStop  ();
        }
    }
}

void setup  ()
{
    pinMode  (action_pin, INPUT_PULLUP);
    butLst = digitalRead  (action_pin);

    servoTop.attach     (9);
    servoBottom.attach  (10);
    setSpeedForAllServos  (190);
    servoTop.setEasingType  (EASE_CUBIC_IN_OUT);
    servoBottom.setEasingType  (EASE_CUBIC_IN_OUT);
    synchronizeAllServosStartAndWaitForAllServosToStop  ();
}
1 Like

@gcjr Thanks for this code, I will try, I'm new on arduino :sweat_smile:

@gcjr your code seems correctly but it looks like something it is not good with battery. Because if I connect the arduino to USB all seems working well. ¿Do you know what can be?

(wrong)Battery example: battery - YouTube (he goes to the 0 position)
(good)USB: usb - YouTube

can't reach DropBox

you should power servos from battery, not Arduino

I have added to youtube.

I mean, if I connect by a USB from Computer cable, it works. Only 1 movement. If I click again he goes to 0 position.

But if we connect to a batery module, it does 2 movement, the normal movement, and the second one to the original position after.

presumably a switch debounce issue.

you can increase the delay, try 100msec to rule it in/out.

without usb connections, you can't use the serial monitor for debugging. you could consider how to used LEDs to track the progress of code. 2 LEDs could indicate 4 states

In the end I will end the problem by removing the batteries and replacing it with a Xiaomi POWERBANK REDMI battery, 10000MAH. It works with that so I leave it like that.

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