Hello everyone.
I looking for the solution about my small project.
So I decided to make motion and light sensor using PIR sensor and LDR.
I have the code which work perfectly. Here is it
#define LDR 0
#define PIR 2
#define LED 3
int pirState;
int ldrValue;
void setup() {
//Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(PIR, INPUT);
digitalWrite(LED, LOW);
}
void loop(){
ldrValue = analogRead(LDR);
//Serial.print("Analog reading = ");
//Serial.println(ldrValue);
if (ldrValue <= 512) { // dark
digitalWrite(LED, HIGH);
}
else { // ldrValue > 512
pirState = digitalRead(PIR);
if (pirState == HIGH) {
digitalWrite(LED, HIGH);
delay(5000);
digitalWrite(LED, LOW);
delay(1000);
}
else { // pirState == LOW
digitalWrite(LED, LOW);
}
}
delay(1000);
}
my question is, how is the code :
LED OFF when time is (example) 10pm - 3am but ON when there is a motion without using RTC module
Have you though about how is your system going to know the time if you don't have a RTC?
You need a means to set the current time when you start the Arduino. E.g. using the Serial port or a keypad. The current time can be stored in an unsigned long.
You can use makeTime (comes with the Time.h library) to create a time in seconds since 1970 (unix timestamp) from year, month, day, hours, minuets and seconds and store that in the above variable.
Next you can e.g. in loop() check if one second has passed and increment the value by one if it has. You can use breakTime (also in the same library) to do the reverse of above to get the date and time components.
You can check the broken down time against your limits and act based on that.
PS: looks like your serial port is not available as you're using pin 0 for the LDR. Why? The serial port is very useful for debugging, so it actually should be the last pins that you use for something else.
sterretje:
Have you though about how is your system going to know the time if you don't have a RTC?
You need a means to set the current time when you start the Arduino. E.g. using the Serial port or a keypad. The current time can be stored in an unsigned long.
You can use makeTime (comes with the Time.h library) to create a time in seconds since 1970 (unix timestamp) from year, month, day, hours, minuets and seconds and store that in the above variable.
Next you can e.g. in loop() check if one second has passed and increment the value by one if it has. You can use breakTime (also in the same library) to do the reverse of above to get the date and time components.
You can check the broken down time against your limits and act based on that.
PS: looks like your serial port is not available as you're using pin 0 for the LDR. Why? The serial port is very useful for debugging, so it actually should be the last pins that you use for something else.
thanks for your respond. 
Honestly, I know what you mean, but i cant implement it in to my code.
could you help me? i'm not perfect at programming.
here is my best, but still error 
#include <Time.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
#define LDR 0
#define PIR 2
#define LED 3
unsigned char hour, min, sec, day, month, year;
int last_detik = 0;
int jam_menit = 0;
int pirState;
int ldrValue;
void setup() {
//Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(PIR, INPUT);
digitalWrite(LED, LOW);
}
void processSyncMessage() {
// If Unix Timestamp is provided over serial, parse the timestamp
while(Serial.available() >= TIME_MSG_LEN ){ // Ensure it contains at least 10 characters
char c = Serial.read() ; // Capture the Serial Header
Serial.print(c); // Print it out
if( c == TIME_HEADER ) { // Make sure its the right header
time_t pctime = 0; // Clear the time
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync the Arduino clock with the Unix Timestamp
}
}
}
void loop(){
ldrValue = analogRead(LDR);
Serial.println(ldrValue);
if (ldrValue <= 100) { // dark
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(0);
}
else { // ldrValue > 512
if( c >= '0' && c <= '9'){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(0);
}
else {
pirState = digitalRead(PIR);
Serial.println(pirState);
if (pirState == HIGH) {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(0);
}
else { // pirState == LOW
digitalWrite(LED, LOW);
}
}
// The processing in the Arduino occurs faster
// than the response from the PIR, and adding this delay
// eliminated a flickering on the LED
delay(0);
}
Your code is missing a closing curly (at the end of loop()).
In loop you use variable 'c' that is not declared; where does it come from? If that is from serial input, you need a different approach. I suggest that your read Serial Input Basics - updated. Easiest is probably to read a message with the '\n' (newline) endmarker. With 'T' you set the time, other characters can be used for something else. Read a full message and next process based on first character.
The variables in below statement might conflict with variables / functions in the time library (it does with the time library that I use).
unsigned char hour, min, sec, day, month, year;
You can add an 's' at the end of those variables.
Which Arduino are you using? Why do you have LDR on pin 0? As said, that is usually for serial communication so you can not even use a PC to set the time.
sterretje:
Your code is missing a closing curly (at the end of loop()).
In loop you use variable 'c' that is not declared; where does it come from? If that is from serial input, you need a different approach. I suggest that your read Serial Input Basics - updated. Easiest is probably to read a message with the '\n' (newline) endmarker. With 'T' you set the time, other characters can be used for something else. Read a full message and next process based on first character.
The variables in below statement might conflict with variables / functions in the time library (it does with the time library that I use).
unsigned char hour, min, sec, day, month, year;
You can add an 's' at the end of those variables.
Which Arduino are you using? Why do you have LDR on pin 0? As said, that is usually for serial communication so you can not even use a PC to set the time.
Ok, thank a lot.
So if I declared 'c', what's the code looks like? Will it work perfectly?
Could you post here the right code?
I am using arduino Uno
Thanks for your response 
I don't know what the variable 'c' contains? Is it supposed to come from the serial port?
sterretje:
I don't know what the variable 'c' contains? Is it supposed to come from the serial port?
honestly, I just mix the new code above (which contain 'c') to my code in first post, the result is like that.
Sorry sir, if I just have code in first post, could you make it work with LED OFF in particular time?