Page 1 of 1
Relay On Time
Posted: Tue Apr 09, 2019 12:03 pm
by sprinkfitter
I want to turn a relay on for a period of time. Can I just call millis
Code: Select all
if ds.DateTime() == (OnMin4, OnHour4, OnDay4, OnMonth4, OnYear4):
relay1.value = 0 # HIGH
millis(2000)
print('Relay 1 ON')
print(ds.DateTime)
print(OnMin1, OnHour1, OnDay1, OnMonth1, OnYear1)
else:
relay1.value = 1 # LOW
print('Relay 1 OFF')
Re: Relay On Time
Posted: Tue Apr 09, 2019 12:21 pm
by Roberthh
You can use utime.sleep_ms(sleep_time).
Re: Relay On Time
Posted: Tue Apr 09, 2019 12:24 pm
by sprinkfitter
Seems a little odd sleep time to be use to keep a relay or pin on. So you can use the sleep for high and low pins??
Re: Relay On Time
Posted: Tue Apr 09, 2019 1:31 pm
by ThomasChr
???
You turn the relay on. You let the uc sleep for a period of time. You turn the relay off.
What is odd here?
Yes, you can also use timers which have the benefit of letting your main code running and not sleeping, but sometimes you don't need to do anything in the sleep phase and so you can sleep...
Re: Relay On Time
Posted: Tue Apr 09, 2019 1:32 pm
by sprinkfitter
Thanks that does work!!
Re: Relay On Time
Posted: Tue Apr 09, 2019 4:59 pm
by pythoncoder
It seems to me that the above code will turn the relay on for one minute regardless of the delay. It turns on when minutes, day, month and year match. It turns off when they fail to match. If the code runs many times per minute, that will happen one minute later.
Or am I missing something?
Re: Relay On Time
Posted: Tue Apr 09, 2019 5:26 pm
by ThomasChr
I don‘t think the code makes much sense at all...
Re: Relay On Time
Posted: Wed Apr 10, 2019 1:14 pm
by sprinkfitter
Under construction... Trying to turn relay on and off a certain times of day! Very new to Micro-python.
Code: Select all
OnMin1 = arr.array([10, 20]) # Array Setup for triggering relay 1
OnHour1 = arr.array([8, 16])
OnDay1 = arr.array([1, 3, 5, 7, ])
OnMonth1 = arr.array([6, 7, 8, 9])
OnYear1 = arr.array([2019, 2020, 2021, 2022])
OnMin2 = arr.array([15, 30]) # Array Setup for triggering relay 2
OnHour2 = arr.array([9, 17])
OnDay2 = arr.array([17, 18, 19, 20])
OnMonth2 = arr.array([6, 7, 8, 9])
OnYear2 = arr.array([2019, 2020, 2021, 2022])
OnMin3 = arr.array([17, 18, 19, 20]) # Array Setup for triggering relay 3
OnHour3 = arr.array([17, 18, 19, 20])
OnDay3 = arr.array([17, 18, 19, 20])
OnMonth3 = arr.array([6, 7, 8, 9])
OnYear3 = arr.array([2019, 2020, 2021, 2022])
OnMin4 = arr.array([17, 18, 19, 20]) # Array Setup for triggering relay 4
OnHour4 = arr.array([17, 18, 19, 20])
OnDay4 = arr.array([17, 18, 19, 20])
OnMonth4 = arr.array([6, 7, 8, 9])
OnYear4 = arr.array([2019, 2020, 2021, 2022])
if ds.DateTime() == (OnMin4, OnHour4, OnDay4, OnMonth4, OnYear4):
relay1.value = 0 # HIGH
utime.sleep_ms(10000)
print('Relay 1 ON')
print(ds.DateTime)
print(OnMin1, OnHour1, OnDay1, OnMonth1, OnYear1)
else:
relay1.value = 1 # LOW
print('Relay 1 OFF')
Re: Relay On Time
Posted: Wed Apr 10, 2019 1:35 pm
by sprinkfitter
Mr.pythoncoder now I do not want to do that. What I want to do is turn a relay on at a certain time let it stay on for the delay and then turn off?
Re: Relay On Time
Posted: Wed Apr 10, 2019 1:48 pm
by ThomasChr
What about:
Code: Select all
while True:
relay1.on()
time.sleep(10000)
relay1.off()
time.sleep(10000)
This will turn the relay on for 10 seconds and off for 10 seconds endlessly...