Pier sensor slow response

hello,

I've bought an Arduino last week and I do therefore a bit of experimentation, but I have one problem , I have a PIR sensor and a photoressistor and the idea is that if it is dark and when the PIR sensor detects movement that then the LEDs turn on. But I tried many codes and i can't get it work, What I did succeed , the sensors separately controlling for example, that if the photoressistor doesn't see light that the LED turns on. does anyone know how I can get this working? I made this code:
int led = 13; // The pin where the LED light is attached (LED)
int sensor = 2; // The pin where the sensor is attached to (PIR sensor)
photoressistor int = 7;
int state = LOW;
int val = 0;

void setup () {
pinMode (LED, OUTPUT); // Led as exhausts
pinMode (sensor, INPUT); // Light sensor input
pinMode (sensor, INPUT); // Motion sensor input
Serial.begin (9600);
}

void run () {
val = digital read (sensor); // View motion sensor
if (val == HIGH)
val = digital read (photoressistor); // light sensor View
if (val == LOW) {
Digital write (LED, HIGH); // Do the led to

}
else {
Digital write (led, LOW); // Do the LED out
delay (200); // Waits 200 milliseconds

if (state == HIGH)
Serial.println ("movement stopped!");
state = LOW; // Update to "low"

if (state == LOW)
Serial.println ("Light");
state = HIGH;
}
}
I hope that someone can help me!

  pinMode (sensor, INPUT); // Light sensor input
  pinMode (sensor, INPUT); // Motion sensor input

This doesn't look right?

photoressistor int = 7;

needs a swap ..

if (state == HIGH)
Serial.println ("movement stopped!");
state = LOW; // Update to "low"

if (state == LOW)
Serial.println ("Light");
state = HIGH;

you need { } around statements that belong together.

Press CTRL-T to get autoformatting, that makes it easier to see how the compiler will interpret your code.

Hi everyone!

I am making a lantern that automaticly turn on when it sees movement and when there is no light but
the movement sensor (pir sensor) reacts very slow somethimes it takes a full 8 seconds to respond. I have a led atteched to the arduino for a respons. Can someone help me?

arduino_forum.ino (753 Bytes)

Please post that in code tags so we don't have to download it.

I'm sure that 2 will never be equal to 0.

int photoressistor = 2;
//...
      if (photoressistor == LOW) {

Piers don't move very fast, in my experience

Also, you need to learn about if ... else

The statements within if (state == LOW) will always be executed (because state can only be HIGH or LOW):

      if (state == HIGH) {
        Serial.println ("movement stopped!");
        state = LOW; // Update to "low"
      }
     
      if (state == LOW) {
        Serial.println ("Light");
        state = HIGH;
      }

(Complaining about the way that I located braces will only serve to obscure the point that I am making.)

The comment about "Update to "low"" is true but unhelpful.

Digital write (LED, HIGH); // Do the led to

Case matters. digitalWrite is spelled all one word with only the W capitalized. It MUST be that way. Also, you named the variable led but here write LED. Those two aren't the same.

You are confusing pin numbers with the state of the associated pins. aarg has one example, where you are checking whether photoresistor is LOW. You probably want to be using digitalRead(photoresistor) to get the pin value, and test that, but you are already doing that:

   val = digitalRead(photoressistor);
    if (val == LOW) {
      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

      if (photoressistor == LOW) {
        Serial.println("licht aan");
        led = HIGH;
      }

By the time you are checking whether photoresistor is LOW, you have already read the state of the photoresistor pin (the first line I quoted) and stored it in val. You then have an IF statement checking if VAL is LOW, and then inside there another IF statement checking if photoresistor is LOW? You could change that to check whether val is low, but that would be redundant with the first IF statement. Do you need this second IF statement? What are you trying to do here?

The “led” variable is another case of this. You are initializing it to 13 and using it as a pin number in the pinMode command. Then, you are using it as an argument to digitalWrite(), so you clearly intend it to be a pin number. However, there are two places in your code where you are changing the value of led to HIGH (which is really a 1) or LOW (which is really a 0.) You are not actually changing the state of the led pin with those calls, you are changing the pin number which will make subsequent calls to digitalWrite() not do what you expect (they will be writing to pins 0 or 1, which will likely affect your serial communications.)

For those things that you never want to change, like the pin number assignments, you are better off making them constants. You can do this by using #define (where it’s customary to use all capital letters for defined values) or by adding the const keyword to their declaration. Either of these methods will turn any attempt to change their value into a compile time error. For example:

#define LED 13
#define SENSOR 7
#define PHOTORESISTOR 2

or

const int led = 13;
const int sensor = 7;
const int photoressistor = 2;

You can also save some memory space by making these values smaller. All of these pin numbers, and even the HIGH or LOW values used by the “val” variable will fit in byte variables. This sketch is small enough that it doesn’t really matter, but eventually your sketches will become bigger and getting the habit of using the best data types will come in handy.

Another issue is near the top of the code I quoted: you set the led output HIGH, and then immediately set it LOW. You will never see this happen, it will be so fast. (You would need a logic analyzer or oscilloscope to be able to see the effect.)

AWOL:
Piers don't move very fast, in my experience

Not true. They run out to sea.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :slight_smile:

You might also be better off reading your photoresistor, (LDR), by connecting it to an analogue input and using 'analogueRead()' to get it's value. That way you can determine the threshold for "dark", instead of relying on a digital pin's threshold.

I haven't looked at your full code yet, and won't until it's posted, (between code tags).
Opening an attached *.ino file creates a folder in the "Temp" directory that isn't automatically deleted afterwards, so it has to be found and manually removed.

Hello everyone!,

I'm new with Arduino and i have a problem. I have a pir sensor and a photoressister and the idea was that if there is no light and the pir sensor sees movement that the led turn on. But if the pir sensor sees movement the led turns on but if the led turn off and you put your hand immediately for the sensor it response very slow it takes then 10 seconds to turn the led on. so there is a big delay but i don't now where that delay comes from. This is the code that i used:
int led = 13;
int sensor = 7;
int photoressistor = 2;
int val = 0;

void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
pinMode(photoressistor, INPUT);
Serial.begin(9600);

}

void loop() {
val = digitalRead(sensor);
if (val == HIGH) {
val = digitalRead(photoressistor);
if (val == LOW) {
digitalWrite(led, HIGH);
digitalWrite(led, LOW);

if (photoressistor == LOW) {
Serial.println("licht aan");
led = HIGH;
}
}
else {
digitalWrite(led, LOW);
delay(20);

if (photoressistor == HIGH) {
Serial.println("licht uit");
led = LOW;
}
}
}

}
I hope that someone can help me!

if (photoressistor == LOW) photoressistor is a pin number, not the value read from it.

... and it has the value 2, which is neither LOW nor HIGH

if (val == LOW) {
digitalWrite(led, HIGH);
digitalWrite(led, LOW);

this will be such a short pulse you probably won't see.

Please do not cross post as they waste time
3 threads merged