Light and motion sencor

Hi,

I am novice with arduino and I need your help. I am using light and motion sencor, my plan is when is movement and not enaught light, then switch on lights. My problem is light are lighting but only afew sec then stop lighting and if is movement then again switch on, after about 1 sec. Where could be problem? Thanks Zdeněk Novotný

code:

#define LDR 0
int luz = 0;
int valor_sensor = 0;

int led = 3;
int sensor = 2;
int state = LOW;
int val = 0;

void setup() {
Serial.begin(9600);
pinMode(LDR,INPUT);

pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}

void loop() {
valor_sensor = analogRead(LDR);
luz = (5.0 * valor_sensor * 100.0)/1024.0;
Serial.print(luz);
Serial.println(" Luz");
val = digitalRead(sensor);

if ( (luz <= 300) && (val == HIGH) ) {
digitalWrite(led, HIGH);
delay(300);}

if ( (luz <= 300) && (state == LOW) ) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
if (luz > 300) // NESVÍTÍ
{
digitalWrite(led, LOW);
}
else {
digitalWrite(led, LOW); // turn LED OFF

if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

code is mix of 2 codes at motion sencor and light sencor so please dont be scared, its my first own project. :slight_smile: