Problems on program

Hello, so I had a setup where I had a LDR and an Infrared Sensor

What I intended to do is that:

A. When the environment is dark, the LEDS will turn on B. When the environment is light, the LEDS will turn off

Hello, so I had a setup where I had a LDR and an Infrared Sensor

What I intended to do is that:

A. When the environment is dark, the LEDS will turn on
B. When the environment is light, the LEDS will turn off
C. However, when something is detected by the Infrared Sensor, the lights will blink on and off

Then I created a block of code and it goes like this

===========================

#include <Servo.h>
Servo serv;
int MOTOR = A1;
int INFRAREDSENSOR = A2;
int LIGHTSENSOR = A3;
int INFRAREDval = 0;
int LIGHTval = 0;
int LED1234 = 13; //outside
int LED5678 = 9; //inside
int normal = 0;
int xxx = 0;
int detected = 0;
void setup()
{
Serial.begin(9600);
pinMode(LED1234,OUTPUT);
pinMode(LED5678,OUTPUT);
serv.attach(MOTOR);
}
void loop()
{
INFRAREDval = analogRead(INFRAREDSENSOR);
LIGHTval = analogRead(LIGHTSENSOR);
Serial.println(LIGHTval);

if (LIGHTval > 401) //meaning dark
{
digitalWrite(LED1234,HIGH);
digitalWrite(LED5678,HIGH);
serv.write(0);
}
else if (LIGHTval < 400) //meaning bright
{
digitalWrite(LED1234,LOW);
digitalWrite(LED5678,LOW);
serv.write(180);

}

delay(1000);

if (INFRAREDval > detected)
{
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
digitalWrite(LED5678,LOW);
delay(1000);
digitalWrite(LED5678,HIGH);
delay(1000);
}

}

=============================

When I ran the code, it works fine with the first parts. The leds will turn on and off depending on the values withing the LDR. However, when the IR is activated (or the code with the IR started executing) the project went haywire. The LEDS would just turn on and off forever, and the other codes dont work.

My main goal is to make a simple house. If its morning, the mini house LEDS will turn off. If its evening, the mini house LEDS will turn on. If there is a burglar (IR detection) the LEDS will turn on and off.

Thanks!

Nothing else can run when delay() is running.

Yes, I know. I counted how many blinks was supposed to be executed and it should be only around 6-7 blinks. What I am concerned with is that it doesn't stop after 6 blinks. It would just keep on blinking even though THERE IS NO OBJECT in front of the infrared sensor. Take note that the blinking would only be executed if an OJBECT IS DETECTED.

If anyone can advice me or help me fix my code.. thanks in advance :confused:

A couple of things: First, please read the two posts at the top of this Forum on how to use code tags when posting code here.

Second, what happens when LIGHTval == 400?

 if (LIGHTval > 401) //meaning dark
   {
     digitalWrite(LED1234,HIGH);
     digitalWrite(LED5678,HIGH);
     serv.write(0);
   }
 else if (LIGHTval  < 400) //meaning bright
   {
     digitalWrite(LED1234,LOW);
     digitalWrite(LED5678,LOW);
     serv.write(180);
     
   }

Third, can you simplify this code:

delay(1000);

 if (INFRAREDval > detected) 
   { 
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
     digitalWrite(LED5678,LOW);
     delay(1000);
     digitalWrite(LED5678,HIGH);
     delay(1000);
   }

with something like (untested):

for (int i = 1; i < 14; i++) {
     digitalWrite(LED5678, i % 2);
     delay(1000);
}

econjack:
A couple of things: First, please read the two posts at the top of this Forum on how to use code tags when posting code here.

Second, what happens when LIGHTval == 400?

 if (LIGHTval > 401) //meaning dark

{
    digitalWrite(LED1234,HIGH);
    digitalWrite(LED5678,HIGH);
    serv.write(0);
  }
else if (LIGHTval  < 400) //meaning bright
  {
    digitalWrite(LED1234,LOW);
    digitalWrite(LED5678,LOW);
    serv.write(180);
   
  }




Third, can you simplify this code:



delay(1000);

if (INFRAREDval > detected)
  {
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
    digitalWrite(LED5678,LOW);
    delay(1000);
    digitalWrite(LED5678,HIGH);
    delay(1000);
  }




with something like (untested):



for (int i = 1; i < 14; i++) {
    digitalWrite(LED5678, i % 2);
    delay(1000);
}

Thanks! Noted. But my code up there was clean and has no errors that caused my problem? (Looping wont stop)

Print out the value of both LIGHTval and INFRAREDval. What do you see?

I dont know how to print them in the same time, so I took the values it step by step. for the first test, I focused on getting the value of LDR. It is fine, giving up values consistently (400-500 if normal), but during the test, when I let the infrared sensor detect my hand, the values became inconsistent.

For the second test, I focused on the infrared sensor. The values just go from 0 to 700-ish and then 0.

It's pretty easy test:

void loop()
{
 INFRAREDval = analogRead(INFRAREDSENSOR);
 LIGHTval = analogRead(LIGHTSENSOR);
 Serial.print("LIGHTval = ");
 Serial.print(LIGHTval);
 Serial.print("   INFRAREDval = ");
 Serial.println(INFRAREDval);
// rest of your code

and then tell us what you see.