Smart Street Light Program 2

Hi folks, I have posted before but don't know why I could get access. I had to re-register. Anyway, I am here because I am new to arduino and programming and struggling having spend many hours trying to program.

Here is a program I started with but but doesn't integrate with PIR well- Motion and Light Sensors with Arduino (PIR sensor)

I have upload a code of where I am now which is different from the link about, I have also uploaded a flow chat. I will be greateful for your help and guidance.

-So far my code switches on the LDR. Next stage I need help with is to over-ride the LDR with a timer (details in flow chart) by switching OFF LED after 3 secs if motion is undetected, remain ON, if detected within 3 secs.

LDR_Insight.ino (1.89 KB)

Flow Chart Review.pdf (154 KB)

Can you explain the logic in your flow chart in a bit more detail:

  1. What is Sens Status S1 ?
  2. What is Sens Status S2 ?
  3. What is Sens Status S3 ?
  4. When, for example, 3 sec is mentioned, what is this ? Elapsed time from trigger or what ?
  5. From decision point "Dim Led" there are 2 possible paths, but it is not clear which one is the correct one.

Thank you for your message 6v6gt.

1-3. Sensor Status- if sensors are HIGH or LOW
There are 3 sensors each one light and led

  1. That's correct- 3 secs, is time elapsed. In other words, after 3 secs if S1, S2 or S3 is not HIGH, DIM LEDS, after another 2 secs if either sensor is not HIGH (i.e LOW), switch OFF ALL LEDS

  2. The deviation form the core (next branch) implies; if at any time between "DIM" and "LED OFF" sensor status S1,S2 or S3 is HIGH, then the sequence begins again.

Sorry about lack of clarity in the flow chat.

I have learnt a few things but i'm being held up at the LDR stage. Over-riding the LDR is proving difficult.
I have tested the PIR code separately and it appears to be working but merging them together doesn't work. I have since noticed that I can't over-ride the LDR after few seconds. So yeah I look forward to your assistance.

Thank you- code below

#define LDR 0
#define PIRA 9
#define PIRB 10
#define LEDA 3
#define LEDB 5
#define LEDC 6

int pirAState;
int pirBState;
int pirCState;
int ldrValue;
int dark;
int motion; // do you have a PIN????? Check!!! Is there need for one?


void setup() {
 Serial.begin(9600);
 pinMode(LEDA, OUTPUT);
 pinMode(LEDB, OUTPUT);
 pinMode(LEDC, OUTPUT);
 pinMode(PIRA, INPUT);
 pinMode(PIRB, INPUT);
}
void loop()  {
 ldrValue = analogRead(LDR);      
 Serial.print("Analog reading = ");
 Serial.println(ldrValue);
 //dark = 0;
 
while(1){ 
ldrValue = analogRead(LDR);                   // read LDR value
 if(ldrValue >= 601) {              
 dark = 1;
 }
 if(ldrValue < 601) {                         // read LDR value
 dark = 0;
 }
           if (dark == 1)                     // condition to switch OFF LED
             {
              digitalWrite(LEDA, LOW);        // set output LOW (LEDs OFF)
              digitalWrite(LEDB, LOW);
              digitalWrite(LEDC, LOW);
             } 
              if (dark == 0)                  // condition to switch ON LED
             {
               digitalWrite(LEDA, HIGH);      // set output HIGH (LEDs ON)
               digitalWrite(LEDB, HIGH);
               digitalWrite(LEDC, HIGH);
               break;
             } delay(5000);  
 ldrValue = analogRead(LDR);      
 Serial.print("Analog reading = ");
 Serial.println(ldrValue);
} 
while(1)  {
 pirAState = digitalRead(PIRA);
 if (pirAState == 1)                
           {
               motion = 0;                    // pir detect motion?
           }
 if(pirAState == 0)                 
             {
               motion = 1;                    // pir does not detect motion?
               
             }
             if(motion == 0 && dark == 0)     // if motion detected and dark(night) detected
             {
               digitalWrite(LEDA, HIGH);      // set output HIGH (LEDs ON)
               digitalWrite(LEDB, HIGH);
               digitalWrite(LEDC, HIGH);
             } 
               digitalWrite(LEDA, LOW);       // set output HIGH (LEDs ON)
               digitalWrite(LEDB, LOW);
               digitalWrite(LEDC, LOW);
               break;                         // break to get out of the loop
               delay(100);
}
}

There seems to be a mismatch between your code and the flowchart.
From the code, it looks like you have 2 passive infra red sensors to detect movement (the flow chart mentions 3 sensors). You also have a light dependent resistor presumably to ensure that the lights (LEDS) are only illuminated if it is dark.
The dimming is to ensure that the lights go out gradually if no movement is detected after a certain period ? There appears not to be any code to handle the dimming.

Two general points:

  1. Often, a clear text description of a problem is better than a flow chart. For example:

3 Leds are wired to light together.
The Leds should light only when the ambient light level is > 600.
If a movement sensor detects movement, the Leds should light at full brightness during the period that movement is detected.
After no further movement is detected, the Leds should remain at full brightness for 3 seconds then dim (be on half brightness ? ) for 2 seconds then switch fully off.

  1. You are processing while statements in your loop(). This is almost certainly wrong in your case. You should use if then else statements and status flags eg:

if ( LedsOn == true && movementDetected == false && inDimPeriod == false ) {
inDimPeriod =true ;
startOfDimPeriodMillis = millis() ;
}
if (inDimPeriod == true && millis() - startOfDimPeriodMillis > 2000 ) {
LedsOn == false ;
inDimPeriod = false ;
}
etc.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Hi 6v6gt
I appreciate your feedback and time to look into this.

  1. I had the impression that people don’t like reading descriptive problems. I have taken that on-board. I agree there’s a mismatch. The idea was to test smaller parts (e.g. get it to switch off first, then try dimming) of the project before adding other parts but it is a mess. It is now clear that it is confusing to everyone else. Your description of sequence of operation is perfect.
  2. Please elaborate on ‘inDimPeriod’ and ‘startOfDimPeriod’, I believe they are flags but flags for what? What do they imply? Do I need to declare it or attach to any value?

Hi TomGeorge,
thank you for the info. I have now implemented it.

Hi sjimoh112 just asking u finally resolve the problem??
im having the same problem right know

Hi guys,

I would appreciate it if someone could look into my code and help with just a function.

This code function the following way-

  1. 3 LED turn on at minimum intensity depending on LDR's resistance
  2. 3 PIR sensors, if any detects motion, the corresponding LED intensity increases to 100 % and return to dim state after a few second which I have no control over but by the PIRs

Problem:

A.I will like to switch off the LEDs after say, 3 seconds after last 'motion is detected'
B. This is not important just now but I will appreciate it nonetheless; How can I control have control of 2 above?

I have looked at Millis tutorials but cant get it to work. I'll be grateful for urgent assistance. Due in 2 days. Thank you!

#define LDR 0
#define PIRA 2
#define PIRB 4
#define PIRC 7
#define LEDA 6
#define LEDB 9
#define LEDC 10

int pirAState;
int pirBState;
int pirCState;
int ldrValue;
int dark;
int motion;
int override;

void setup() {
  Serial.begin(9600);
  pinMode(LEDA, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDC, OUTPUT);
  pinMode(PIRA, INPUT);
  pinMode(PIRB, INPUT);
  pinMode(PIRC, INPUT);
}

void ldr()

{
  ldrValue = analogRead(LDR);           // Reads LDR Values
  Serial.print("LDR- Day or Night = ");
  Serial.println(ldrValue);
  delay(1000);
  
  pirAState = digitalRead(PIRA);        // Reads sensorA values
  Serial.print("Motion Detected by A = ");
  Serial.println(pirAState);
  
  pirBState = digitalRead(PIRB);        // Reads sensorB values
  Serial.print("Motion Detected by B = ");
  Serial.println(pirBState);
  
  pirCState = digitalRead(PIRC);        // Reads sensorC values
  Serial.print("Motion Detected by C = ");
  Serial.println(pirCState);


  if (ldrValue >= 601)
{             
  dark = 1;                                        // Daylight condition, hence LEDs are switched off
}
  else
{                         
  dark = 0;                                        // Night condition to switch ON LEDs
                
}


//
  if (dark == 1)                                    //  Daylight condition- All output is LOW (LEDs OFF)
  {
   analogWrite(LEDA, 0);                  
   analogWrite(LEDB, 0);
   analogWrite(LEDC, 0);
  }
  

                        if (pirAState == 0 && dark == 0)   // Night and sensorA does not detect motion
                           {
                            analogWrite(LEDA, 50);      // Set output HIGH (LEDs ON) minimum intensity
                           }
                           
                                      else  if(pirAState == 1 && dark == 0)  // Night and sensorA detects motion
                                     {
                                      analogWrite(LEDA, 255);     // set output HIGH (LEDA ON) 
                                     }
                                      delay(100);                // A second delay
                            
                        if (pirBState == 0 && dark == 0)    // Night and sensorB does not detect motion
                           {
                            analogWrite(LEDB, 50);
                           }
                           
                                      else  if(pirBState == 1 && dark == 0)
                                     {     
                                      analogWrite(LEDB, 255);     // set output HIGH (LEDB ON)
                                     }delay(100);                // A second delay
                           
                        if (pirCState == 0 && dark == 0)    // Night and sensorA does not detect motion
                           {
                            analogWrite(LEDC, 50);    
                           }
                           
                                      else  if(pirCState == 1 && dark == 0)
                                     {
                                      analogWrite(LEDC, 255);     // set output HIGH (LEDC ON)
                                     }delay(100);                // A second delay

                         If (pirAState == 0 && dark == 0 && pirBState == 0 && pirCState == 0)
                         
                           
}


void loop()
{
    ldr();

}[code]

[/code]

carmelof:
Hi sjimoh112 just asking u finally resolve the problem??
im having the same problem right know

Not completely bro,

My update is close though.

Hello people, please any assistant will be well appreciated.