In your code, you lost a few characters on this line... (the two underscores "_" on each side of the keyword... those two words are called "macros")
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
To activate a section of your code, use a condition statement, like this...
if ((now.hour() == amHour && now.minute() == amMinute) && now.second() < 10) {
myServoFunction(); // call this function
}
Your code, formatted... it compiles and runs. The output is the time of day, every three seconds.
sketch.ino
// https://forum.arduino.cc/t/can-someone-help-me-how-to-set-certain-time-to-active-work-to-a-sensor/1249548/5
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int ldr_pin = 7;
const int led_pin = 13;
const int trigPin = 9; // Trig pin of ultrasonic sensor
const int echoPin = 10; // Echo pin of ultrasonic sensor
const int distance_threshold = 10; // Distance threshold for ultrasonic sensor
void setup() {
pinMode(ldr_pin, INPUT);
pinMode(led_pin, OUTPUT);
pinMode(trigPin, OUTPUT); // Set trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Set echoPin as an INPUT
Serial.begin(9600);
// Initialize RTC
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
// Read LDR sensor
int ldrValue = digitalRead(ldr_pin);
// Turn LED ON if LDR detects light, otherwise turn it OFF
if (ldrValue == HIGH) {
digitalWrite(led_pin, HIGH);
} else {
digitalWrite(led_pin, LOW);
}
// Ultrasonic sensor functionality
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
// Calculate distance
int distance_cm = duration * 0.034 / 2;
// Control LED based on ultrasonic sensor
if (distance_cm <= distance_threshold) {
digitalWrite(led_pin, HIGH); // Turn on LED if an object is detected within threshold distance
} else {
digitalWrite(led_pin, LOW); // Turn off LED
}
// Print date and time
Serial.print(now.year(), DEC);
Serial.print('/');
if (now.month() < 10) Serial.print("0"); // zero padding for single digit
Serial.print(now.month(), DEC);
Serial.print('/');
if (now.day() < 10) Serial.print("0"); // zero padding for single digit
Serial.print(now.day(), DEC);
Serial.print(' ');
if (now.hour() < 10) Serial.print("0"); // zero padding for single digit
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute() < 10) Serial.print("0"); // zero padding for single digit
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second() < 10) Serial.print("0"); // zero padding for single digit
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}
diagram.json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-ds1307", "id": "rtc1", "top": 71.4, "left": 172.9, "attrs": {} },
{
"type": "wokwi-photoresistor-sensor",
"id": "ldr1",
"top": -73.8,
"left": 161.2,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-led",
"id": "led1",
"top": -61.2,
"left": -34.6,
"attrs": { "color": "red" }
},
{
"type": "wokwi-resistor",
"id": "r1",
"top": 33.6,
"left": -38.95,
"rotate": 90,
"attrs": { "value": "1000" }
},
{
"type": "wokwi-hc-sr04",
"id": "ultrasonic1",
"top": -142.5,
"left": -32.9,
"attrs": { "distance": "12" }
}
],
"connections": [
[ "rtc1:GND", "nano:GND.1", "black", [ "h0" ] ],
[ "nano:5V", "rtc1:5V", "red", [ "v0" ] ],
[ "rtc1:SDA", "nano:A4", "green", [ "h0" ] ],
[ "nano:A5", "rtc1:SCL", "green", [ "v0" ] ],
[ "nano:5V", "ldr1:VCC", "red", [ "v0" ] ],
[ "nano:GND.1", "ldr1:GND", "black", [ "v0" ] ],
[ "nano:7", "ldr1:DO", "green", [ "v0" ] ],
[ "nano:13", "r1:2", "green", [ "v14.4", "h-29.3" ] ],
[ "r1:1", "led1:A", "green", [ "h0" ] ],
[ "nano:GND.1", "led1:C", "black", [ "v-72", "h-163.7" ] ],
[ "ultrasonic1:TRIG", "nano:9", "green", [ "v0" ] ],
[ "nano:10", "ultrasonic1:ECHO", "green", [ "v-24", "h18.7" ] ],
[ "nano:5V", "ultrasonic1:VCC", "red", [ "v-91.2", "h-86.9" ] ]
],
"dependencies": {}
}
Your HC-SR04 triggers at 10cm and your LDR triggers a digital output at some value, but only lights the LED for a few milliseconds, even in total darkness.
@clyde_gigs - What sensor do you want to activate and what time/day/date/month/year do you want it active?