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);
}
}
};
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
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;
}
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
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.
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