Can someone help me how to set certain time to active/work to a sensor?

#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('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

delay(1000);
}

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Please edit your post, select all code and click the <CODE/> button to apply code tags. This will make the code easier to read, easier to copy and the forum software will display it correctly.


Your question in the title is not quite clear to me. Can you please provide a description (in a new reply).

2 Likes

I found this thread searching for "minutes since" and @alto777.

Maybe that discussion is relevant.

a7

2 Likes

Please, redo your post by placing your code between tags.
To do this click < code > in the toolbar.

Your code does not compile.

2 Likes

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?

2 Likes

I suggest you get the Arduino Cookbook and skim it from cover to cover stopping on all sections related to time and alarm clock functions and read them. You will find almost everything you want.

1 Like

No, OP did not. The reason why you don't see them is that OP did not use code tags.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.