#include <Wire.h> // Include the Wire library for I2C communication
#include <RTClib.h> // Include the RTClib library for the DS1307 RTC module
#include <Stepper.h> // Include the Stepper library
// Define the number of steps per revolution (change as needed)
const int stepsPerRevolution = 200; // Adjust based on your stepper motor
// Define stepper motors (stepper1 on pins 18, 19 and stepper2 on pins 16, 17)
Stepper stepper1(stepsPerRevolution, 18, 19);
Stepper stepper2(stepsPerRevolution, 16, 17);
// Define the pins for the LEDs
const int greenLED = 2;
const int blueLED = 4;
// Create RTC object
RTC_DS1307 rtc;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set up LED pins as OUTPUT
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
// Initialize I2C communication for RTC
Wire.begin(21, 22); // SDA pin 21, SCL pin 22
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Halt the program if RTC is not found
}
// Check if RTC is running, and if not, set the time
if (!rtc.isrunning()) {
Serial.println("RTC is not running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set time to compile time
}
// Set the speed of both stepper motors
stepper1.setSpeed(15); // Adjust the speed of stepper1 as needed
stepper2.setSpeed(15); // Adjust the speed of stepper2 as needed
}
void loop() {
// Get the current time from the RTC
DateTime now = rtc.now();
// Print the current time to the Serial Monitor (optional)
Serial.print("Current time: ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
// Check if the current time is between 22:00 and 23:00
if (now.hour() >= 00 && now.hour() < 24) {
// Turn on both LEDs
digitalWrite(greenLED, HIGH);
digitalWrite(blueLED, HIGH);
// Rotate stepper motors
stepper1.step(806); // Rotate stepper1 forward by one full revolution
Serial.println("STEPPER MOTOR 1 (PAN STEPPER MOTOR): MOVED 360 DEGREES CCW INDICATED BY -200 STEPS");
stepper2.step(116); // Rotate stepper2 forward by one full revolution
Serial.println("STEPPER MOTOR 2 (TILT STEPPER MOTOR): MOVED 50 DEGREES CCW INDICATED BY -28 STEPS");
delay(2000); // Wait for 2 seconds
// Optionally rotate the motors in reverse or add another action
stepper1.step(-800); // Rotate stepper1 backwarD
Serial.println("STEPPER MOTOR 1 (PAN STEPPER MOTOR): MOVED 360 DEGREES CW INDICATED BY 0 STEPS");
stepper2.step(-176); // Rotate stepper2 backward
Serial.println("STEPPER MOTOR 2 (TILT STEPPER MOTOR): MOVED 80 DEGREES CW INDICATED BY 16 STEPS");
delay(2000); // Wait for 2 seconds
stepper1.step(800); // Rotate stepper1 backward
Serial.println("STEPPER MOTOR 1 (PAN STEPPER MOTOR): MOVED 360 DEGREES CCW INDICATED BY -200 STEPS");
stepper2.step(64); // Rotate stepper2 backward
Serial.println("STEPPER MOTOR 2 (TILT STEPPER MOTOR): MOVED 30 DEGREES CCW INDICATED BY 0 STEPS");
delay(2000); // Wait for 2 seconds
stepper1.step(-800);
Serial.println("STEPPER MOTOR 1 (PAN STEPPER MOTOR): MOVED 360 DEGREES CW INDICATED BY 0 STEPS");
} else {
// If the time is outside 22:00 - 23:00, turn off the LEDs and stop motors
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
stepper1.step(0); // Rotate stepper1 backward
stepper2.step(0); // Rotate stepper2 backward
}
// Wait for a short period before checking the time again
delay(1000); // Delay 1 second before next time check
}
GSM MODULE
EMERGENCY SWITCH
PAN STEPPER MOTOR
RTC MODULE
TILT STEPPER MOTOR
LASER
DIODE 1
LASER
DIODE 2