Starting at 180 degrees servo motor

So i got a code where the servos move when I input a time. Theres a countdown that happens before they move and what im wanting help with is that, is there a way that when the countdown ends the servo main go into 180 degrees first then moves to its respective area. Like for example The countdown its it goes into 180 degrees then goes to 210 degrees clockwise. Btw all my servos are 360 so not really quite sure here the code

#include <Servo.h> // Include the Servo library to control servo motors
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LCD library for I2C communication

// Define servo objects
Servo mainServo; // Main rotating servo
Servo servo1; // First dispensing servo
Servo servo2; // Second dispensing servo

// Initialize LCD with I2C address 0x27 and 16x2 size
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define button pins
const int selectButton = 2; // Button to select time unit
const int increaseButton = 3; // Button to increase time value
const int startButton = 4; // Button to start countdown

// Variables to store user-selected countdown time
int selectedTime = 0; // 0 = hours, 1 = minutes, 2 = seconds
int countdownHours = 0; // Stores countdown hours
int countdownMinutes = 0; // Stores countdown minutes
int countdownSeconds = 0; // Stores countdown seconds

// Variables to store previous button states for debouncing
bool lastSelectState = HIGH;
bool lastIncreaseState = HIGH;
bool lastStartState = HIGH;

void setup() {
    Serial.begin(9600); // Start serial communication for debugging
    mainServo.attach(9); // Attach main servo to pin 9
    servo1.attach(10); // Attach first dispensing servo to pin 10
    servo2.attach(11); // Attach second dispensing servo to pin 11

    lcd.init(); // Initialize LCD
    lcd.backlight(); // Turn on LCD backlight

    pinMode(selectButton, INPUT_PULLUP); // Set select button as input with pull-up
    pinMode(increaseButton, INPUT_PULLUP); // Set increase button as input with pull-up
    pinMode(startButton, INPUT_PULLUP); // Set start button as input with pull-up
    
    displayTimeSelection(); // Display initial time selection screen
}

void loop() {
    if (buttonPressed(selectButton, lastSelectState)) { // Check if select button is pressed
        selectedTime = (selectedTime + 1) % 3; // Cycle between hours, minutes, and seconds
        displayTimeSelection(); // Update LCD display
    }

    if (buttonPressed(increaseButton, lastIncreaseState)) { // Check if increase button is pressed
        increaseTime(); // Increase selected time value
        displayTimeSelection(); // Update LCD display
    }

    if (buttonPressed(startButton, lastStartState)) { // Check if start button is pressed
        if (countdownHours > 0 || countdownMinutes > 0 || countdownSeconds > 0) { // Ensure a valid time is set
            startCountdown(); // Start countdown process
        } else {
            lcd.clear(); // Clear LCD
            lcd.setCursor(0, 0);
            lcd.print("Set time first!"); // Prompt user to set time
            delay(2000); // Wait for 2 seconds
            displayTimeSelection(); // Restore time selection display
        }
    }
}

void increaseTime() {
    if (selectedTime == 0 && countdownHours < 23) { // Increase hours if selected
        countdownHours++;
    } else if (selectedTime == 1 && countdownMinutes < 59) { // Increase minutes if selected
        countdownMinutes++;
    } else if (selectedTime == 2 && countdownSeconds < 59) { // Increase seconds if selected
        countdownSeconds++;
    }
}

void displayTimeSelection() {
    lcd.clear(); // Clear LCD screen
    lcd.setCursor(0, 0);
    lcd.print("Set Time:"); // Display title
    lcd.setCursor(0, 1);
    
    // Display selected time unit with an asterisk
    lcd.print(selectedTime == 0 ? "*H " : "H ");
    lcd.print(countdownHours);
    lcd.print(":");
    lcd.print(selectedTime == 1 ? "*M " : "M ");
    lcd.print(countdownMinutes);
    lcd.print(":");
    lcd.print(selectedTime == 2 ? "*S " : "S ");
    lcd.print(countdownSeconds);
}

void startCountdown() {
    lcd.clear(); // Clear LCD
    lcd.setCursor(0, 0);
    lcd.print("Counting down..."); // Display countdown message
    
    while (countdownHours > 0 || countdownMinutes > 0 || countdownSeconds > 0) { // Run countdown loop
        lcd.setCursor(0, 1);
        printDoubleDigit(countdownHours);
        lcd.print(":");
        printDoubleDigit(countdownMinutes);
        lcd.print(":");
        printDoubleDigit(countdownSeconds);
        delay(1000); // Wait for one second

        if (countdownSeconds > 0) {
            countdownSeconds--;
        } else if (countdownMinutes > 0) {
            countdownMinutes--;
            countdownSeconds = 59;
        } else if (countdownHours > 0) {
            countdownHours--;
            countdownMinutes = 59;
            countdownSeconds = 59;
        }
    }
    
    triggerServo(); // Activate servos when countdown reaches zero
}

void triggerServo() {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Dispensing...");

    mainServo.write(210); // Rotate main servo to 210 degrees clockwise
    delay(2000);
    mainServo.write(90);
    delay(2000);

    servo1.write(180); // Open first dispensing servo
    delay(1000);
    servo1.write(90); // Open first dispensing servo
    delay(5000);
    servo1.write(0);
    delay(1000);
    servo1.write(90); // Close first dispensing servo
    delay(1000);

    mainServo.write(180); // Move main servo back to 180 degrees
    delay(2000);
    mainServo.write(90);
    delay(500);

    mainServo.write(330); // Rotate main servo to 330 degrees counterclockwise
    delay(2000);
    mainServo.write(90);
    delay(6000);

    servo2.write(180); // Open second dispensing servo
    delay(1000);
    servo2.write(90); // Close second dispensing servo
    delay(5000);
    servo2.write(0);
    delay(1000);
    servo2.write(90);

    mainServo.write(180); // Reset main servo to initial position
    delay(2000);
    mainServo.write(90);
    delay(2000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Done!"); // Indicate completion
}

bool buttonPressed(int button, bool &lastState) {
    bool buttonGotPressed = false;
    bool currentState = digitalRead(button);

    if (currentState != lastState) { // Detect button state change
        if (currentState == LOW) { // Button press detected
            buttonGotPressed = true;
        }
        lastState = currentState;
        delay(50); // Debounce delay
    }

    return buttonGotPressed;
}

void printDoubleDigit(int number) {
    if (number < 10) {
        lcd.print("0"); // Print leading zero if needed
    }
    lcd.print(number); // Print number
}

Please try again your description of the desired motion sequence.

Perhaps a timing diagram, where you draw with time going along the x-axis and servo positions depicted on the y axis.

Also, you make you life hard with the 360, but at a glance it looks like you always carefully 90 stop anything you started.

So it should be sink,e logic to do what you want, or close enough. It's just your words are a bit confuse; I'm not sure at all what it is not doing that you need to add, or is doing that is not what you meant or want.

a7

neither am i actually im just kinda messing around with the servos cause im just experimenting.

So basically I got one 360 motor with a platform ontop with walls and one hole. Then theres a container cylinder with 2 holes but covered by 2 servo motors which are also 360 (Thats all the store had no 180). Now the 360 motor with the platform when the countdown ends on the lcd it would go first to a 180 position in the x axis. Then after maybe 1 second it would turn to the 210 degrees of a circle counterclockwise. Then it would stop and the first motor that closes one of the holes of the cylinder would open after maybe 5 seconds it would close then servo motor with platform would go to the 330 degrees of a circle clockwise then it would stop and same with the first one would happen then after everything it would go back to its 180 degrees or just stay in the position because when another countdown ends it would just go back to the 180 again. Sorry for my lack of knowledge in explaining im not native to english and not a really good explainer. Here is the angles of a circle im basing it on

Also not quite sure how i can do the coding for the 360 motor its kind of confusing for me tho @alto777 hoping for your help

That might be tricky because while 360deg servos exist, most 360 servos are actually "continuous rotation" ones.
They don't have position control.

Seems to make sense. But with continuous rotation servos and without adding anything, you can only achieve positions by running the motors for a certain time.

I thought you were clear on that from your other thread, where you moved with angles that weee not 90 and stopped movement by writing 90 to the servo.

But over time 3 seconds left and 3 seconds right will slowly drift. It is impossible to rely on time only over any significant number of moves.

You could add an encoder for each servo motor or limit switches that could signal the code that you'd moved to a known position to effectively reset, so now time will be OK enough.

The sequence of moves is something you already have done in the other thread(s) on this topic.

  set the motor running at some speed and direction (write a not-90 degree value to the servo
  delay just the right amount of time for the motor at that speed to attain the angle
  stop the motor by writing 90 degrees to the servo

for each new angle you want to move to.

Srsly, lose this project until you can get real servos. Doesn't

  myServo.write(42);

which would move to and stay at 42 degrees look like a bunch more fun?

Or add to the hardware as I suggest and have more of a different kind of fun. :expressionless:

a7

idem.