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
}
