Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Door sensor trouble

Mon Apr 05, 2021 6:32 pm

Hello again

I was trying to find some informations but either I'm searching wrongly or there's simply not much that can help me.

I need help with python code that will turn on my light in a specific sequence of closing and opening doors, here's what I mean:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Doors closed --> light is off
V
Opening doors --> light goes on
V
Closing doors --> light stays on

Then exiting room:

Opening doors --> light off
V
Closing doors --> light stays off

and again, and again... I hope you can at least understand what I'm trying to do :D
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

So, if you could help me and prompt me what should I search for, or maybe give me some clues (I'd rather avoid ready solutions, I would appreciate just some clues at first) I would be really grateful.

Ahh, and also I would have a physical button that can switch this whole system off (if it's a day time or whatever).

If it's any helpful I'm treating sensor like a normal button in gpiozero and it can switch relay on and off.

I also have code only for switching light on and off via physical switches:

Code: Select all

from gpiozero import LED, Button
from signal import pause
#physical buttons 
B1=Button(21)
B2=Button(13)
B3=Button(3)
#door sensor
S1=Button(15)
#relay "inputs"
R1=LED(8)
R2=LED(7)

B1.when_pressed = R1.on
B1.when_released = R1.off

B2.when_pressed = R2.on
B2.when_released = R2.off

pause()

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Mon Apr 05, 2021 7:13 pm

You need to use some simple logic to do what you want , you don't really need to worry about the door closing other than to stop your code endlessly cycling while the door is open.

Have a look at his code it was for another user who wanted to turn a camera on and off using a single switch , which is exactly what you want to do with your door.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

led1 = 40
led2 = 36
btn = 18
state = 0

GPIO.setup(led,GPIO.OUT)
GPIO.setup(btn,GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
    
        while (GPIO.input(btn) == 1):
            time.sleep(0.2)
        
        state = state + 1
           if state == 2:
            state = 0

        while (GPIO.input(btn) == 0):
            time.sleep(0.2)
        
        if state == 0:
            GPIO.output(led1,True)
            GPIO.output(led2,True)
            print "state0 Cam off";
            execfile("/home/pi/camOff.py")
            time.sleep(0.5)


        if state == 1:
            GPIO.output(led1,False)
            GPIO.output(led2,False)
            execfile("/home/pi/camOn.py")
            print "state1 Cam On";
            time.sleep(0.5)
    


finally:
    print("Cleaning up")
    GPIO.cleanup()

does not use gpiozero so you will have to write your own code to do the same if you want to use gpio zero.
Last edited by pcmanbob on Tue Apr 06, 2021 10:34 am, edited 2 times in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Tue Apr 06, 2021 10:10 am

Okay, so...

Code: Select all

        
while (GPIO.input(btn) == 1):
 time.sleep(0.2)
 
How does it know that ''GPIO.input(btn)'' is equal to ''1'' ?
Does it mean that when button is in high state (turned on I mean) it automatically set the value to 1 and if it's turned off it's value is 0 ?
If so, does it work the same way in gpiozero ?

User avatar
B.Goode
Posts: 11817
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Door sensor trouble

Tue Apr 06, 2021 10:25 am

Alchoni_ wrote:
Tue Apr 06, 2021 10:10 am
Okay, so...

Code: Select all

        
while (GPIO.input(btn) == 1):
 time.sleep(0.2)
 
How does it know that ''GPIO.input(btn)'' is equal to ''1'' ?
Does it mean that when button is in high state (turned on I mean) it automatically set the value to 1 and if it's turned off it's value is 0 ?
If so, does it work the same way in gpiozero ?


From the Developer's own documentation for RPi.GPIO:
Input
To read the value of a GPIO pin:

GPIO.input(channel)
(where channel is the channel number based on the numbering system you have specified (BOARD or BCM)). This will return either 0 / GPIO.LOW / False or 1 / GPIO.HIGH / True.
Ref: https://sourceforge.net/p/raspberry-gpi ... asicUsage/


Similarly for gpiozero's Button class:
value
Returns 1 if the button is currently pressed, and 0 if it is not.
Ref: https://gpiozero.readthedocs.io/en/stab ... tton.value

drgeoff
Posts: 11816
Joined: Wed Jan 25, 2012 6:39 pm

Re: Door sensor trouble

Tue Apr 06, 2021 11:43 am

Anything based solely on a door switch is going to get out of sync with room occupancy unless the door is closed after every time someone goes in or out. And what if for example two people come into the room and close the door then one goes out and closes the door?
Quis custodiet ipsos custodes?

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Tue Apr 06, 2021 11:52 am

drgeoff wrote:
Tue Apr 06, 2021 11:43 am
Anything based solely on a door switch is going to get out of sync with room occupancy unless the door is closed after every time someone goes in or out. And what if for example two people come into the room and close the door then one goes out and closes the door?
Yeah, I need closed doors all the time because of cat and noises in my house, if it "get out of sync" i will simply reopen and close doors

The thing is that I'm not treating this project strict like a "project", it's more like a mechatronics classes to learn something more in pandemic times

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Tue Apr 06, 2021 7:17 pm

Okay, I watched some tutorials but at the beginning it's still hard to connect all the dots together...

1. Does it just set value = 0 to a button when it's in a high state ? (assigns reversed state ?) And if it's not turned on then it's just = to 1 (because 0 +1 is still 1 ) ?

Code: Select all

 while True:

        while (GPIO.input(btn) == 1):
            time.sleep(0.2)

        state = state + 1
        if state == 2:
            state = 0
2. I have no idea what is that for :P

Code: Select all

    while (GPIO.input(btn) == 0):
        time.sleep(0.2)
3. If state == 0 turn on leds (does it mean that it will turn on LEDs when the "button" is off(by default), and when it's set on because of the previous loop ?

Code: Select all

 if state == 0:
        GPIO.output(led1, True)
        GPIO.output(led2, True)
        print
        "state0 Cam off";
        execfile("/home/pi/camOff.py")
        time.sleep(0.5)
4. And when the "button" is turned on it will turn LEDs off (by default) and it will be turned off when at the first loop we received 1because of button turned off ?

Code: Select all

    if state == 1:
        GPIO.output(led1, False)
        GPIO.output(led2, False)
        execfile("/home/pi/camOn.py")
        print
        "state1 Cam On";
        time.sleep(0.5)
5. On top of everything is "try" is it just for debugging, or to end and clear whole on/off section ?

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Wed Apr 07, 2021 8:28 am

Ok I will try to explain in more detail.

Code: Select all

while (GPIO.input(btn) == 1):
So in this line we read the state of the input on a specific gpio which is defined by the btn variable ( btn = 18 as set by the line at the start of the program )

Because this input has a pull up on it it will always be = 1 while the switch is not pressed, so the following indented line is executed which just sleeps the code for 0.2 sec before looping back to test the input again.

when the switch is pressed the the input will = 0 because the switch pull the input to ground, once the input is = 0 then the sleep line is skipped and the code moves on to the next lines.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

   state = state + 1
        if state == 2:
            state = 0
This is the part of the code that provides the logic of the on/off control.

So state is set to = 0 by the line at the start of the program, so the first time the code runs these lines it adds 1 to the state variable , making it = 1, this is used later in the code.

now when the code loops round and runs this for a second time again it adds 1 so state now = 2, and now the if statement is executed and it resets state to = 0.

So all this code does is change the state variable for 0 to 1 or 1 to zero on each pass of the code.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Code: Select all

 while (GPIO.input(btn) == 0):
        time.sleep(0.2)

This works in the same way as the version of it above , but now when reading the input form the switch , but now if the input is = 0 , switch pressed, then the code is paused for 0.2 sec, only passes is point in the code when the switch is released.

------------------------------------------------------------------------------------------------------------------------------------------------------------

If state == 0 turn on leds (does it mean that it will turn on LEDs when the "button" is off(by default), and when it's set on because of the previous loop ?
And when the "button" is turned on it will turn LEDs off (by default) and it will be turned off when at the first loop we received 1because of button turned off ?
State is the logic part of the program that controls the camera/leds no/off state , is is only switched by the input as I explained before , it is no directly linked to the switch input state.

I suspect the by setting the 2 gpio outputs to true the OP is actually switching the led's off because of how they are wired and is also switch the camera off at this point to and the reverse is true when the state = 1 when the led's are turned on and the camera is turned on.

-------------------------------------------------------------------------------------------------------------------------------------------------------------
On top of everything is "try" is it just for debugging, or to end and clear whole on/off section ?
Try is used to test for errors , in this case I suspect its to catch the CTRL C which is used to stop the program running ,
but is missing the usual except KeyboardInterrupt: lines from the end of the program which has been replaced with

Code: Select all

finally:
    print("Cleaning up")
    GPIO.cleanup()
Hope this helps...
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

boyoh
Posts: 1603
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Door sensor trouble

Wed Apr 07, 2021 7:28 pm

This comes under CLUES TO THE CLUE LESS

You must understand code will tell a project how to work,, But it will not BUILD it For you.

If your project is faulty Code will not repair it for you

You must learn some LOGIC and apply to this project . Then write your Code to control it

The Raspberry Pi is Not Plug And Play I suggest you learn some basic electronics on a breadboard

then you will have a better understanding of what you are being told

Regards BoyOh


Logic Gates. Logic gates are the basic building blocks of any digital system. It is an electronic circuit having one or more than one input and only one output. The relationship between the input and the output is based on a certain logic.
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Sat Apr 10, 2021 8:59 am

Ofcourse it helped me pcman, I understand code properly now but have a problem with converting it into a gpiozero library I was looking at the official documentation etc, tried many combinations and solutions but with no succeed.

ofcourse it's not working, it's more like a sketch:

Code: Select all

from gpiozero import Button, LED
import time

#Physical buttons

b1 = Button(21)     #desk light
b2 = Button(13)     #main light
b3 = Button(5)      #auto on/off light control

#realy outputs

R1 = LED(8)
R2 = LED(7)

#door sensor

S1 = Button(15)
state = 0

while b3.is_pressed:
    while True:

        while S1.is_pressed:
            time.sleep(0.2)

        state = state + 1
        if state == 2:
            state = 0

        while S1.is_released:
            time.sleep(0.2)

        if state == 0:
            R1.on
            time.sleep(0.5)

        if state == 1:
            R1.off
            time.sleep(0.5)
else:
    while True:
        time.sleep(0.2)
Also, should I maybe use a def function to make separately control with physical buttons or just paste the code under the last else statement ?

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Sat Apr 10, 2021 9:03 am

boyoh wrote:
Wed Apr 07, 2021 7:28 pm
This comes under CLUES TO THE CLUE LESS

You must understand code will tell a project how to work,, But it will not BUILD it For you.

If your project is faulty Code will not repair it for you

You must learn some LOGIC and apply to this project . Then write your Code to control it

The Raspberry Pi is Not Plug And Play I suggest you learn some basic electronics on a breadboard

then you will have a better understanding of what you are being told

Regards BoyOh


Logic Gates. Logic gates are the basic building blocks of any digital system. It is an electronic circuit having one or more than one input and only one output. The relationship between the input and the output is based on a certain logic.
I think my problem is that I don't know how to apply those logic into the python yet, I have logic gate states sticked on my wall but starting with a bigger project could be a mistake. At the other hand I feel like I will learn more trying to find solutions for that project other than creating useless "hello world" programs and blinks

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Sat Apr 10, 2021 9:53 am

So I think you need to first lay out exactly what each button does.

So for example

b1 = Button(21) #desk light > R1 = LED(8) pressing button turns light on second press turns light off.

do this for each button

Then we will have an understanding of exactly what you want to do, and we can put together some example code.

Gpiozero is not some thing I use very much, I normally use RPi.GPIO as you might have guessed from the code I posted , but given a little time I am sure I can come up with a working example.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
B.Goode
Posts: 11817
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Door sensor trouble

Sat Apr 10, 2021 10:15 am

Alchoni_ wrote:
Sat Apr 10, 2021 8:59 am
Ofcourse it helped me pcman, I understand code properly now but have a problem with converting it into a gpiozero library I was looking at the official documentation etc, tried many combinations and solutions but with no succeed.

ofcourse it's not working, it's more like a sketch:

Code: Select all

from gpiozero import Button, LED
import time

#Physical buttons

b1 = Button(21)     #desk light
b2 = Button(13)     #main light
b3 = Button(5)      #auto on/off light control

#realy outputs

R1 = LED(8)
R2 = LED(7)

#door sensor

S1 = Button(15)
state = 0

while b3.is_pressed:
    while True:

        while S1.is_pressed:
            time.sleep(0.2)

        state = state + 1
        if state == 2:
            state = 0

        while S1.is_released:
            time.sleep(0.2)

        if state == 0:
            R1.on
            time.sleep(0.5)

        if state == 1:
            R1.off
            time.sleep(0.5)
else:
    while True:
        time.sleep(0.2)
Also, should I maybe use a def function to make separately control with physical buttons or just paste the code under the last else statement ?



Possibly echoing ( or restating ) what @pcmanbob has said -

Your code 'sketch' above does not correspond with the 'word sketch' in your initial post, which had only ONE input:
I need help with python code that will turn on my light in a specific sequence of closing and opening doors, here's what I mean:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Doors closed --> light is off
V
Opening doors --> light goes on
V
Closing doors --> light stays on

Then exiting room:

Opening doors --> light off
V
Closing doors --> light stays off

It is difficult to advise on a solution without being able to understand the requirement.


That is maybe only a problem in the context of trying to discuss this in a public forum. As a 'tinkerer' (explorer) I often begin writing code without a clear idea of what the final solution will be, because playing (experimenting) with a combination of hardware and code itself opens up new understandings and possibilities that make new solutions viable.

For an implementation of a task that is waiting for buttons to be pressed I suggest sticking with the gpiozero library because those sorts of operations have already been provided as part of the python API.

User avatar
neilgl
Posts: 3045
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Door sensor trouble

Sat Apr 10, 2021 11:00 am

A simple gpiozero version that looks at doors opening and doors closing to turn light on, off.

Code: Select all

from gpiozero import Button
button = Button(21)
state=0

def my_function():
  global state
  if (state == 0):
    print("turn light on")
    state = 1
  elif (state == 1):
    print("turn light OFF")
    state = 0

button.when_pressed = my_function
print("doors are closed to start...")
input("press a key to exit\n")

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Sun Apr 11, 2021 8:55 am

So I made some assumptions, as to which relay was which and that the auto switch would only apply to the main light.

So this is a basic example using gpiozero

Code: Select all

from gpiozero import Button, LED
import time

# global variables we are using

dl = 0
ml = 0


#Physical buttons

b1 = Button(21)     #desk light
b2 = Button(13)     #main light
b3 = Button(5)      #auto on/off light control

#relay outputs

R1 = LED(8) #desk light
R2 = LED(7) #main light

def button_1_press():
    global dl
    dl = dl + 1
    if dl ==2:
        dl = 0


def button_2_press():
    global ml
    ml = ml + 1
    if ml == 2:
        ml = 0





# set up for button press detection

b1.when_pressed = button_1_press
b2.when_pressed = button_2_press
b3.when_pressed = button_2_press


while True:

    if dl == 1:
        R1.on()
        
    else:
        R1.off()
        
    if ml == 1:
        R2.on()
        
    else:
        R2.off()
        
    time.sleep(0.2)    

This is untested code so there could well be errors, but it shows you how to do the logic control.

if you run up against an error please post the error in it entirety and I will fix it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

boyoh
Posts: 1603
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Door sensor trouble

Mon Apr 12, 2021 8:07 pm

This system might be controlled using Opto Isolators switching solid stat relays Thyristors witch can be

latched and De Latched in a Logic Order . Also you can incorporate slotted Opto Isolators.

There seams to more talk on Code ,than on the hardware side , The Op as a lot to learn in Logic Principles

before attempting this project, He wants to realize it took me many years to learn what I know...

I've watched a few Capacitors blow up on my test bench, There are no Magic Books or Formulas.

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Thu Apr 22, 2021 1:00 pm

Sorry about the absence, I had exams and now I can spend more time on a project.

So, I have that "console" and idea is to use this green switch to control code that is responsible for automatic light turn on/off by a door sensor. So, if the green button is off the automatic light doesn't work, and I can use rest of the manual buttons. If it's turned on automatic lights are working and the rest of the manual switches aren't. Two red switches are manual control of the light so it's basically extension of a normal wall switches.

code for manual switches:

Code: Select all

led1 = LED(8)
button1 = Button(21)

led2 = LED(7)
button2 = Button(13)

button1.when_pressed = led1.on
button1.when_released = led1.off

button2.when_pressed = led2.on
button2.when_released = led2.off

pause()
I managed a few days ago to get this code to working but I had to make few changes and now I mistakenly deleted it and don't remember changes (it's for the automatic lights):

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

led1 = 40
led2 = 36
btn = 18
state = 0

GPIO.setup(led,GPIO.OUT)
GPIO.setup(btn,GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
    
        while (GPIO.input(btn) == 1):
            time.sleep(0.2)
        
        state = state + 1
           if state == 2:
            state = 0

        while (GPIO.input(btn) == 0):
            time.sleep(0.2)
        
        if state == 0:
            GPIO.output(led1,True)
            GPIO.output(led2,True)
            print "state0 Cam off";
            execfile("/home/pi/camOff.py")
            time.sleep(0.5)


        if state == 1:
            GPIO.output(led1,False)
            GPIO.output(led2,False)
            execfile("/home/pi/camOn.py")
            print "state1 Cam On";
            time.sleep(0.5)
    


finally:
    print("Cleaning up")
    GPIO.cleanup()

GPIO's :

B1 - 21/40
B2-13/33
B3-5/29

R1-8/24
R2-7/26

sensor (basically a button)-15/10

GPIO numbers/physical pins

EDIT:
This code works fine, turns on the light when I want it, and works fine with doors. I know that it is not compatible with gpiozero but now I have to think of something to combine it with the previous code for manual light control and add a switch to it. (I mean an on/off automatic light switch)

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

led1 = 24
btn = 10
state = 0

GPIO.setup(led1,GPIO.OUT)
GPIO.setup(btn,GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
    
        while (GPIO.input(btn) == 1):
            time.sleep(0.2)
        
        state = state + 1
        if state == 2:
            state = 0

        while (GPIO.input(btn) == 0):
            time.sleep(0.2)
        
        if state == 0:
            GPIO.output(led1,True)                       
            time.sleep(0.5)


        if state == 1:
            GPIO.output(led1,False)           
            time.sleep(0.5)
    


finally:
    print("Cleaning up")
    GPIO.cleanup()


Attachments
1.jpg
1.jpg (105.51 KiB) Viewed 293 times
Last edited by Alchoni_ on Thu Apr 22, 2021 2:42 pm, edited 2 times in total.

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Thu Apr 22, 2021 1:59 pm

pcmanbob wrote:
Sun Apr 11, 2021 8:55 am

Code: Select all

from gpiozero import Button, LED
import time

# global variables we are using

dl = 0
ml = 0


#Physical buttons

b1 = Button(21)     #desk light
b2 = Button(13)     #main light
b3 = Button(5)      #auto on/off light control

#relay outputs

R1 = LED(8) #desk light
R2 = LED(7) #main light

def button_1_press():
    global dl
    dl = dl + 1
    if dl ==2:
        dl = 0


def button_2_press():
    global ml
    ml = ml + 1
    if ml == 2:
        ml = 0





# set up for button press detection

b1.when_pressed = button_1_press
b2.when_pressed = button_2_press
b3.when_pressed = button_2_press


while True:

    if dl == 1:
        R1.on()
        
    else:
        R1.off()
        
    if ml == 1:
        R2.on()
        
    else:
        R2.off()
        
    time.sleep(0.2)    

It's lacking sensor I think, the switches just sometimes "randomly" turn lights on and of when they are turned on or off

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Thu Apr 22, 2021 5:36 pm

Your physical layout has changed so much from you original question , I think as I said before you need to provide us with a full list of switches / sensors including exactly what each does and how they are related.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
neilgl
Posts: 3045
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Door sensor trouble

Thu Apr 22, 2021 5:39 pm

Yes what Bob said and the random on/off stuff could be caused by electrical “noise” getting picked up in the long wires.

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Thu Apr 22, 2021 7:58 pm

neilgl wrote:
Thu Apr 22, 2021 5:39 pm
Yes what Bob said and the random on/off stuff could be caused by electrical “noise” getting picked up in the long wires.
No, no, I meant, that the way how buttons work just doesn't make any sense, they are sometimes turning light when they are on, sometimes when off and sometimes they depend on each other, sometimes not.


Never the less, refer to the previous photo:


Beginning from right, the end-side red button is called B2 --- GPIO 13 / PHYSICAL 33 --- It's turning main light on and off

The left red one is B1 --- GPIO21 / PHYSICAL 40 --- it's turning desk light on and off

The green one is B3 --- GPIO 5/PHYSICAL 29 --- it should enable and disable the operation of the code responsible for automatically turning the light on and off

The door sensor, named as S --- GPIO15/physical 10 --- is basically a button, the entire code for automatically turning the light on and off depends on it

R1 -- relay output or I think it's an input in reality --- GPIO8/PHYSICAL 24 --- it's connected to a light
R1 -- relay input --- GPIO7/PHYSICAL 26 --- it's connected to a light

So, when the green button is turned on, the whole system based on a sensor should work, and the rest of the red buttons shouldn't turn on the light. When green button is turned off, it should disable automatic light system and enable manual steering for light (by the red switches).

Now, the thing is that I have code both for manual steering and automatic light turn on/off but those are written in different libraries and I need to merge them together somehow, I also need to get the green button to work.

I hope, this is clear now, if not let me know what should I explain better, also here's a sensor photo if it's any useful:
Attachments
3.jpg
3.jpg (85.34 KiB) Viewed 210 times

User avatar
neilgl
Posts: 3045
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Door sensor trouble

Thu Apr 22, 2021 8:19 pm

Yes that photo looks like a simple magnet and reed switch. The buttons "not making sense" is probably due to logic errors in the script(s). Can you post the current script(s) using Code tags

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Thu Apr 22, 2021 8:53 pm

I posted it somewhere higher but it was a little bit confusing so here it goes:

FOR MANUAL STEERING:

Code: Select all

from gpiozero import LED, Button
from signal import pause

led1 = LED(8)
button1 = Button(21)

led2 = LED(7)
button2 = Button(13)

button1.when_pressed = led1.on
button1.when_released = led1.off

button2.when_pressed = led2.on
button2.when_released = led2.off

pause()
AUTOMATIC LIGHT STEERING:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

led1 = 24
btn = 10
state = 0

GPIO.setup(led1,GPIO.OUT)
GPIO.setup(btn,GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    while True:
    
        while (GPIO.input(btn) == 1):
            time.sleep(0.2)
        
        state = state + 1
        if state == 2:
            state = 0

        while (GPIO.input(btn) == 0):
            time.sleep(0.2)
        
        if state == 0:
            GPIO.output(led1,True)                       
            time.sleep(0.5)


        if state == 1:
            GPIO.output(led1,False)           
            time.sleep(0.5)
    


finally:
    print("Cleaning up")
    GPIO.cleanup()

I have done some changes, but still mostly they aren't mine, keep in mind that both of them doesn't have green button function

pcmanbob
Posts: 11061
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Door sensor trouble

Fri Apr 23, 2021 9:57 am

So much has changed because you originally said you were using push buttons that you wanted to press once for on and again for off, now you have simple rocker switches.

When B3 is switched on/off what do you want the automatic light to do " go out and stay out" or "come on and stay on" or "just freeze as it is" ?

and in your code you have B2 what's that doing because I thought you were using the door switch to control the main light ?

you need to specify exactly what happens between the sensor and the physical switch which also controls the main light.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Alchoni_
Posts: 53
Joined: Fri Feb 26, 2021 12:18 pm

Re: Door sensor trouble

Fri Apr 23, 2021 12:14 pm

When B3 is switched on/off what do you want the automatic light to do " go out and stay out" or "come on and stay on" or "just freeze as it is" ?
When B3 is on, light should:
Doors closed --> light is off
V
Opening doors --> light goes on
V
Closing doors --> light stays on

Then exiting room:

Opening doors --> light off
V
Closing doors --> light stays off

and again, and again... I hope you can at least understand what I'm trying to do
If it's any helpful, look at the RPi.GPIO code cause it's working fine and do what I wanted. I think it is better if I just wrote an sequence other than trying to explain it in sentences.
and in your code you have B2 what's that doing because I thought you were using the door switch to control the main light ?
B2 and B1 are just basically extensions of normal wall switch, they should turn on the lights one at a time IF THE GREEN BUTTON IS OFF.

If you want to, and have a time we could just meet on discord or other platform and talk in "real time" so I will be hopefully able to explain everything better, just let me know

Return to “Beginners”