0

enter image description here

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3,GPIO.OUT)     #Define pin 3 as an output pin

while True:
if(time.hour(18)&&time.hour(5)) #The function turns on between  6pm and 5am
GPIO.output(3,1)   #Outputs digital HIGH signal (5V) on pin 3
time.sleep(1)      #Time delay of 1 second

GPIO.output(3,0)   #Outputs digital LOW signal (0V) on pin 3
time.sleep(1)      #Time delay of 1 second

I used the above code to work the blinking of led light from 6pm to 5am but its not working and how should the code be changed if i'm using the pir sensor ,so that the light blinks when PIR sensor detects something and if the motion is more than 1 minute it has to send the mail as intruder.

if time 6pm to 6am
{
     if pir detects
     {
         light blinking
         if(motion detected for more than 1 minute)
              {
                   send mail as intruder
              }

     }
}

Please help out.

  • 3
    that does this mean? but its not working .............. you need to learn about proper indentation of python code .............. also, think very carefully about the if statement .... when will it evaluate to true? – jsotola Jan 27 at 5:13
  • 1
    in python, the indentation of the code is very important .... the proper solution is to indent the code properly .... read this .... python-course.eu/python3_blocks.php ...... then correct your code ..... if you still have a problem after that, then update your question ............... also, i asked you a question .... please answer it – jsotola Jan 27 at 5:24
  • 3
    at any time it can only be a single hour ... therefore it can't be 18 AND 5 at the same time - you need a course in boolean logic – Jaromanda X Jan 27 at 5:43
0

It's going to work better with some indentation and a colon after the if clause. Also your if statement can never be true. My logic says if the time is NOT 5am to 6pm then blink the light (so that means it'll be on from midnight to 5am or 6pm to midnight).

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3,GPIO.OUT)     #Define pin 3 as an output pin

while True:
    current_time = time.now()
    if(!(5<= current_time.hour() <= 18)): #The function turns on between  6pm and 5am
        GPIO.output(3,1)   #Outputs digital HIGH signal (5V) on pin 3
        time.sleep(1)      #Time delay of 1 second

        GPIO.output(3,0)   #Outputs digital LOW signal (0V) on pin 3
        time.sleep(1)      #Time delay of 1 second
2

This line:

if(time.hour(18)&&time.hour(5)) #The function turns on between  6pm and 5am

is wrong in two ways.

First, there is no function time.hour(n). To get the current hour in local time, use

time.localtime().tm_hour

Next, you want this code to be active between 6pm and 5am, so the hour must be < 5 or >= 18. So, the correct line is this:

if(time.localtime().tm_hour < 5  or  time.localtime().tm_hour >=18):

Python does not have a && or ||, this is written as word.

With this, your code would work and blink the LED at night, but it still has one flaw. At day time, the while loop will execute over and over without pause, causing 100% CPU load. This can be prevented by adding more sleep:

while True:
    if(time.localtime().tm_hour < 5  or  time.localtime().tm_hour >=18):
        GPIO.output(3,1)   #Outputs digital HIGH signal (5V) on pin 3
        time.sleep(1)      #Time delay of 1 second
        GPIO.output(3,0)   #Outputs digital LOW signal (0V) on pin 3
        time.sleep(1)      #Time delay of 1 second
    else:
        time.sleep(1)

Another issue: You write about 5V output... The Pi outputs 3.3V only!

Not the answer you're looking for? Browse other questions tagged or ask your own question.