/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: **Servo Control**
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-11-15 06:12:59
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* The system shall utilize the EasyButton library to */
/* manage button presses on pin D2, controlling a */
/* servo motor on pin D3 for locking mechanisms, */
/* while interfacing with the DS3231 RTC for */
/* timekeeping. */
/****** END SYSTEM REQUIREMENTS *****/
/* START CODE */
/****** DEFINITION OF LIBRARIES *****/
#include
#include //https://github.com/arduino-libraries/Servo
#include //https://github.com/NorthernWidget/DS3231
#include // Added EasyButton library
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Button_PushButton_PIN_D2 = 2;
/***** DEFINITION OF PWM OUTPUT PINS *****/
const uint8_t Lock_Servomotor_PWMSignal_PIN_D3 = 3;
const int lockPin = 9; // Added for servo lock control
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
uint8_t Lock_Servomotor_PWMSignal_PIN_D3_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float Lock_Servomotor_PWMSignal_PIN_D3_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
RTC_DS3231 rtc; // RTC instance
Servo myServo; // Servo instance
EasyButton button(Button_PushButton_PIN_D2); // EasyButton instance
// Button state variables
bool isLocked = false;
bool timerStarted = false;
DateTime lockTime;
unsigned long lastCheckTime = 0;
const unsigned long checkInterval = 1000;
void setup(void)
{
Serial.begin(9600); // Serial communication
pinMode(Lock_Servomotor_PWMSignal_PIN_D3, OUTPUT); // Servo pin setup
if (!rtc.begin()) {
Serial.println("RTC not found!");
while (1);
}
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
button.begin(); // Initialize EasyButton
// Recover lock status from RTC
uint8_t lockStatus = rtc.readnvram(0);
if (lockStatus == 1) {
isLocked = true;
lockTime = rtc.now();
timerStarted = true;
Serial.println("Recovered locked state from RTC.");
} else {
unlock();
}
}
void loop(void)
{
button.read(); // Read button state
if (button.wasPressed() && !timerStarted) {
Serial.println("Button pressed, starting timer and locking.");
lock();
timerStarted = true;
lockTime = rtc.now();
rtc.writenvram(0, 1);
}
// Check time only every checkInterval milliseconds
if (millis() - lastCheckTime >= checkInterval) {
lastCheckTime = millis();
DateTime now = rtc.now();
if (timerStarted) {
if (now >= lockTime + TimeSpan(30)) {
Serial.println("30 seconds passed, unlocking.");
unlock();
timerStarted = false;
rtc.writenvram(0, 0);
}
}
}
}
void lock() {
if (!isLocked) {
myServo.attach(lockPin);
myServo.write(90);
delay(500);
myServo.detach();
isLocked = true;
Serial.println("Locked.");
}
}
void unlock() {
if (isLocked) {
myServo.attach(lockPin);
myServo.write(0);
delay(500);
myServo.detach();
isLocked = false;
Serial.println("Unlocked.");
}
}
/* END CODE */