Help with code if possible

Hi everyone first time posting this has been racking my head all day yesterday for the life of me I can’t solve it so I’m asking for help,
I have 2 servos both analog / 1 Arduino nano
First off I have code that controls the bottom servo to move along with the 2 servo at the same time and my problem is I can’t seem to delay them so the 1st one works then the second after a 300ms delay no matter how I do it. Am I writing the code wrong or deleting/adding too much.. I’m not sure I’m at a loss..

The code I have here is what I’ve got up too without adding anything else like detailed above,

I want one servo to start first then 300ms delay other to start then when pressing again for signal it to do it in reverse (which last bit does work properly.

#include "ServoEasing.h"
ServoEasing servotop;
ServoEasing servobottom;
const int action_pin = 2;
int location = 35;
// Below numbers should be adjusted in case the servos does not close/open as desired
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(7);//missle
  servobottom.attach(8);//up and down
  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);
 delay(200);
 synchronizeAllServosStartAndWaitForAllServosToStop();
 servobottom.setEaseTo(bottom_closed);
 location = bottom_closed;
 delay(200);
 synchronizeAllServosStartAndWaitForAllServosToStop();
 servotop.setEaseTo(top_closed);
 }
 }
};

Most people use serial.Print() to track the logic and values to debug their programs. I do not see anything like that in your program. Why not?

Is this the sequence you want:

  1. Wait for button press
  2. If button press then open top and open bottom
  3. if top open and bottom open, wait for button press
  4. if top open and bottom open and button press, close top and close bottom

this the sequence id like

  1. Wait for button press
  2. If button press then open bottom delay then open top
  3. if top open and bottom open, wait for button press
  4. if top open and bottom open and button press, close top delay then close bottom.

(What I have is)

  1. Wait for button press
  2. If button press open top open bottom
  3. if top open and bottom open, wait for button press
  4. if top open and bottom open and button press, close top delay then close bottom.”

Post a link to the documentation of the library ServoEasing

Hi @StefanL38,

the lib and documentation can be found here:

I managed to run the TO's sketch on Wokwi which might help to understand the logic:

The code does not compile on Wokwi with the usual

#include "ServoEasing.h"

but with

#include "ServoEasing.hpp"

It's not an error it's a feature ... :wink:

The code uses this function

setEaseToForAllServosSynchronizeAndWaitForAllServosToStop()

setEaseToForAllServos Synchronize AndWaitForAllServosToStop()

Well if you use a function that synchronises the movement of all servos
you can't expect the servos to move sequentially
the next thing is

void loop() {
  int proximity = digitalRead(action_pin);
  
  if (proximity == LOW) {
    if (location > bottom_open) {
      servotop.setEaseTo(top_open); // there is NO delay below this command
      servobottom.setEaseTo(bottom_open); // which means execute the next line of code IMMIDIATELY
      synchronizeAllServosStartAndWaitForAllServosToStop();

I want to ask if you have thought at least a few minutes about what each line of code does?

post your attempt what you have to modify in the code to have a delayed() movement between servotop and servobottom

best regards Stefan

Hi Stefan

The :- “setEaseToForAllServosSynchronizeAndWaitForAllServosToStop”

I had taken out at the beginning and replaced with a condition.

IF (bottom_open) then servotop(top_open)

But it doesn’t work off hand that’s what I can remember as I’m just out at a 4th birthday party (oh the fun)

Many thanks.

Hi @rich_d_t,

it is obviously a nice task for a state machine ... See here:

/*
  Forum: https://forum.arduino.cc/t/help-with-code-if-possible/1201603
  Wokwi: https://wokwi.com/projects/384469984687635457

  At least for Wokwi we have to use

  #include "ServoEasing.hpp"



*/

#include "ServoEasing.hpp"
ServoEasing servotop;
ServoEasing servobottom;
const int action_pin = 2;
int location = 35;
// Below numbers should be adjusted in case the servos does not close/open as desired
int bottom_closed = 107;
int top_closed = 167;
int bottom_open = 20;
int top_open = 20;

/*
  WAITFOROPEN   Wait for button press
  OPENBOTTOM:   If button press then open bottom
  OPENTOP       Delay then open top
  WAITFORCLOSE  Wait for button press
  CLOSETOP      Close top
  CLOSEBOTTOM   Delay then close bottom
                Goto WAITFOROPEN
*/


enum stateTypes {WAITFOROPEN, OPENBOTTOM, OPENTOP, WAITFORCLOSE, CLOSEBOTH, CLOSETOP, CLOSEBOTTOM};
stateTypes state = WAITFOROPEN;


void setup()
{
  pinMode(action_pin, INPUT_PULLUP);
  servotop.attach(7);//missle
  servobottom.attach(8);//up and down
  setSpeedForAllServos(190);
  servotop.setEasingType(EASE_CUBIC_IN_OUT);
  servobottom.setEasingType(EASE_CUBIC_IN_OUT);
  synchronizeAllServosStartAndWaitForAllServosToStop();
}
void loop()
{
  stateMachine();
}

void stateMachine() {
  static unsigned long startTime = 0;
  switch (state) {
    case WAITFOROPEN:
      if (buttonPressed()) {
        state = OPENBOTTOM;
      }
      break;
    case OPENBOTTOM:
      servobottom.setEaseTo(bottom_open);
      synchronizeAllServosStartAndWaitForAllServosToStop();
      startTime = millis();
      state = OPENTOP;
      break;
    case OPENTOP:
      if (millis() - startTime > 300) {
        servotop.setEaseTo(top_open);
        synchronizeAllServosStartAndWaitForAllServosToStop();
        state = WAITFORCLOSE;
      }
      break;
    case WAITFORCLOSE:
      if (buttonPressed()) {
        state = CLOSETOP;
      }
      break;
    case CLOSETOP:
      servotop.setEaseTo(top_closed);
      synchronizeAllServosStartAndWaitForAllServosToStop();
      startTime = millis();
      state = CLOSEBOTTOM;
      break;
    case CLOSEBOTTOM:
      if (millis() - startTime > 300) {
        servobottom.setEaseTo(bottom_closed);
        synchronizeAllServosStartAndWaitForAllServosToStop();
        state = WAITFOROPEN;
      }
      break;
    default:
      state = WAITFOROPEN;
      break;
  }
}

boolean buttonPressed() {
  static unsigned long lastChange = 0;
  static byte buttonState = HIGH;
  static byte lastState   = HIGH;
  byte actState = digitalRead(action_pin);
  if (actState != lastState) {
    lastState = actState;
    lastChange = millis();
  }
  if (buttonState != lastState && millis() - lastChange > 20) {
    buttonState = lastState;
    return !buttonState;
  }
  return false;
}

On Wokwi: https://wokwi.com/projects/384469984687635457

It might not exactly do what you want, but the logic behind is

  WAITFOROPEN   Wait for button press
  OPENBOTTOM:   If button press then open bottom
  OPENTOP       Delay then open top
  WAITFORCLOSE  Wait for button press
  CLOSETOP      Close top
  CLOSEBOTTOM   Delay then close bottom
                Goto WAITFOROPEN
2 Likes

If you are back home post you real actual sketch.

You are almost kicking users in the ass by posting code that is no longer relevant

Sorry I posted the last code I had working before going out as the last few tries of me altering it got deleted as they didn’t work. First time problem solving how and why it wouldn’t work I thought if I post the code then ask what I wanted to happen then someone post how they would do it and then I’d understand why mine is/wasn’t working if that makes sense.

Kind regards

thats it .. as in that program works - as I’d want it
I’ll try it once I get home and have a look at the code and compare it with mine as I couldn’t understand why mine wasn’t working as I thought. Lol

many thanks
:pray:

Well written state machine example. This, as well as the ServoEasing library have been saved.
Thanks for sharing

1 Like

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