Elvy Collado
Elvy Collado

Reputation: 39

How to put together two python script Pi3 Gpio

I need help coding with Python.

I have a Raspberry Pi3. I have two scripts that perform different functions, but I want them to work together.

The first one drives a pir sensor with a LED as an output. When the pir goes high, it start to count down, given enough time to detect a person again. During these time if it doesn't detect anything, the LED goes off.

The second one drives a LDR sensor. It reads the changing values of the LDR sensor and turns a LED on or off. I already have all of the wiring set up for these.

The main question is how to put together these two scripts in order to make the pir sensor wait until it's dark (value from the LDR) to start driving a LED to turn on or off when it detects/does not detect a person. This is just to turn off the pir sensor in order to not turn on the LED during daylight.

By the way I have only one pir sensor and one LED in this separate configuration, but I want to use just one Python code with one LDR as a global light sensor to manage 4 pir sensors and 4 LED's. For example, all of the pir sensors would wait until it's dark to start working as an input, and when it's dark, each pir sensor can control a specific LED

pir1=led1, pir2=led2, pir3=led3, pir4=led4

here is the code for the pir sensor and the led:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN) #pir sensor put as input
GPIO.setup(25, GPIO.OUT) # led put as output
GPIO.output(25, GPIO.LOW)

delay = 10 # set number of seconds delay before light turns off

while True:
#wait for pir to trigger.
print "waiting "
while GPIO.input(21) == 0:
time.sleep (0.5)

print "turn light on here"
GPIO.output(25, GPIO.HIGH)
count = 0

#start count down to turn off
print "count down started "
while count < delay:
count = count + 1

# here if the input goes high again we reset the counter to 0
if GPIO.input(21) == 1:
count = 0

print "count down now ", (delay - count)
time.sleep(1)

print "Turn light off"
GPIO.output(25, GPIO.LOW)

where is the code for the ldr sensor and the led:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
delayt = .1
value = 0 # this variable will be used to store the ldr value
ldr = 12 #ldr is connected with pin number 12
led = 25 #led is connected with pin number 25

GPIO.output(led, False) # keep led off by default
def rc_time (ldr):
count = 0

#Output on the pin for
GPIO.setup(ldr, GPIO.OUT)
GPIO.output(ldr, False)
time.sleep(delayt)

#Change the pin back to input
GPIO.setup(ldr, GPIO.IN)

#Count until the pin goes high
while (GPIO.input(ldr) == 0):
count += 1

return count

#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print("Ldr Value:")
value = rc_time(ldr)
print(value)
if ( value >= 70):
print("It is dark turn on led")
GPIO.output(led, True)
if (value < 69):
print("It is light turn off led")
GPIO.output(led, False)

except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()

Any help is highly appreciated. Remember I am Really Noob with Python coding. All my work is by trial and error.

Upvotes: 1

Views: 167

Answers (1)

Esdras Xavier
Esdras Xavier

Reputation: 863

I think the code bellow could work... I did not tried because I cannot test this, but give it a try.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN) #pir sensor put as input
GPIO.setup(25, GPIO.OUT) # led put as output
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, False)
GPIO.output(25, False) # keep led off by default

delayt = .1
ldr = 12 #ldr is connected with pin number 12
led = 25 #led is connected with pin number 25
delay = 10 # set number of seconds delay before light turns off

def is_dark():
  global ldr, led, delayt

  count = 0
  #Output on the pin for
  GPIO.setup(ldr, GPIO.OUT)
  GPIO.output(ldr, False)
  time.sleep(delayt)

  #Change the pin back to input
  GPIO.setup(ldr, GPIO.IN)

  #Count until the pin goes high
  while (GPIO.input(ldr) == 0):
    count += 1

  if count >= 70:
    return True

  return False

def has_someone():
  if GPIO.input(21) == 1:
    return True

  return False


def main():
  global delay

  while True:
    if has_someone() and is_dark():
      print "turn light on here"
      GPIO.output(25, GPIO.HIGH)
      count = 0
      while count < delay:
        count = count + 1

        # here if the input goes high again we reset the counter to 0
        if has_someone() == 1:
          count = 0

        print "count down now ", (delay - count)
        time.sleep(1)

      print "Turn light off"
      GPIO.output(25, GPIO.LOW)


if __name__ == "__main__":
  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    GPIO.cleanup()


Upvotes: 1

Related Questions