Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Automating Multiple Valves...

Wed May 21, 2014 6:58 pm

I am creating a filter that will start a cleaning process every 24 hrs. It doesn't matter what time of the day it is, I just want the cleaning cycle to start every 24 hrs from when power is initially supplied to the Raspberry Pi. So if the filter is first plugged in at 4pm it will clean everyday at 4pm. The cleaning process will act as follows...
send power to relay 1 for 20 seconds
then
send power to relay 2 for 20 seconds
then
send power to relay 3 for 20 seconds
then
do nothing for 20 seconds
then
send power to relay 4 for 20 seconds
then
do nothing for 20 seconds
then
send power to relay 5 for 20 seconds
then
send power to relay 6 for 20 seconds

I am a super newbie to programming and I have no idea how to get this kind of code figured out. Any help is much appreciated.

Thanks,
Zac

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Automating Multiple Valves...

Wed May 21, 2014 8:27 pm

Just dive in and start writing. One of the features of Python is its 'near-English' syntax.

Perhaps the outline of your code might look like this -

Code: Select all

import time

def send_power(relay, duration):
    print("Sending power to relay " + repr(relay) + " for " + repr(duration) +  " seconds.")

def run_sequence():
    send_power(1, 20)
    send_power(2, 20)
    send_power(3, 20)
    time.sleep(20)
    send_power(4, 20)
    time.sleep(20)
    send_power(5, 20)
    send_power(6, 20)

run_sequence()
You haven't said anything about how the relays are going to be connected and controlled, so you will have to flesh out the working details of the send_power function based on the circuitry you connect.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Wed May 21, 2014 9:51 pm

Wow that is a great start and I need to look through some definitions to figure out what your words mean ;) I was planning on using six gpio's to send the power to the relays. so lets say...
gpio 4 sends power to relay 1
gpio 14 sends power to relay 2
gpio 15 sends power to relay 3
gpio 17 sends power to relay 4
gpio 18 sends power to relay 5
gpio 27 sends power to relay 6

I was also going to include a manual cleaning button on this filter so if I wanted to start a cleaning cycle I could just push the button, it would go through the sequence, and then 24 hrs later it would automatically clean again.
So when...
gpio 22 receives 5v from the pushbutton, it starts the manual cleaning cycle

Thanks again for your help,
Zac

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Thu May 22, 2014 4:42 pm

So by including the gpio's into the situation would it look similar to this...

Code: Select all

import time

def send_power(relay, duration, gpio):
    print("Sending power to relay " + repr(relay) + " for " + repr(duration) +  " seconds via GPIO " + repr(gpio).")

def run_sequence():
    send_power(1, 20, 4)
    send_power(2, 20, 14)
    send_power(3, 20, 15)
    time.sleep(20)
    send_power(4, 20, 17)
    time.sleep(20)
    send_power(5, 20, 18)
    send_power(6, 20, 27)

run_sequence()

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Automating Multiple Valves...

Thu May 22, 2014 6:12 pm

Nothing to do with the RPi or GPIOs really. This is just basic program design, and NO, I wouldn't do that.

The principle is called 'abstraction' or 'information hiding'. The run_sequence command just needs to state which relay to send power to - it doesn't need to know which GPIO you have connected it to, because you might change it later.

Give the send_power function some sort of look-up (a list or a directory) that makes it the keeper of the knowledge about which GPIO pins drive which relays. That way if you need to change it you only need to change it in one place. If you force yourself to change it in two places Murphy's Law states that one day you will forget one of them...

The print statement in send_power can stay as a summary of what information the lookup provided.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

Re: Automating Multiple Valves...

Thu May 22, 2014 6:34 pm

The manual start with a button push is also fairly easy to implement, but you're going to have to have the program running in a continual loop looking for a button press, and otherwise counting down to the next 24-hour start.

So, at the start of run_sequence, I'd note the start time of the loop, for starters, then modify the program to loop continually.

Code: Select all

import time

StartTime = 0

def send_power(relay, duration):
    print("Sending power to relay " + repr(relay) + " for " + repr(duration) +  " seconds.")

def run_sequence():
    StartTime = time.time()
    send_power(1, 20)
    send_power(2, 20)
    send_power(3, 20)
    time.sleep(20)
    send_power(4, 20)
    time.sleep(20)
    send_power(5, 20)
    send_power(6, 20)

def buttonPushed():
    # check your GPIO for the button being pushed.  Depending on how fast the loop below runs (ie. do you have a sleep in there?) you may have to hold the button for a second or more.
    do_something_with_gpio_here
    if buttonIsPushed:
        return True
    return False

while 1:
    # first, see if it has been >24 hours since running, or if we just started.  Or, is the button being pushed?
    if time.time() - StartTime > 24*60*60 or buttonPushed():
        run_sequence()


    # if you don't want to run the CPU at max at all times, then do a small sleep here.  But, you'll have to hold the button at least that long to manually start it to ensure it gets sensed
    time.sleep(0.5)


User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Automating Multiple Valves...

Fri May 23, 2014 5:52 pm

A quick follow-up with an example of using a lookup for the link between the relay numbers and the gpio pins controlling them.

Code: Select all

import time

active_time = 2 #should be 20
dwell_time = 5 #should be 20

def gpio_for_relay(relay_number):
    gpio_list = [0, 4, 14, 15, 17, 18, 27]
    return gpio_list[relay_number]

def send_power(relay_to_use, duration):
    gpio_pin = gpio_for_relay(relay_to_use)
    print("Sending power to relay " + repr(relay_to_use) + " for " + repr(duration) +  " seconds via GPIO " + repr(gpio_pin))
    time.sleep(duration)
    
def do_nothing(duration):
    print("Doing nothing for " + repr(duration) +  " seconds" )
    time.sleep(duration)
    
def sequence():
    send_power(1, active_time)
    send_power(2, active_time)
    send_power(3, active_time)
    do_nothing(dwell_time)
    send_power(4, active_time)
    do_nothing(dwell_time)
    send_power(5, active_time)
    send_power(6, active_time)

sequence()
When executed, it looks like this -

Code: Select all

Sending power to relay 1 for 2 seconds via GPIO 4
Sending power to relay 2 for 2 seconds via GPIO 14
Sending power to relay 3 for 2 seconds via GPIO 15
Doing nothing for 5 seconds
Sending power to relay 4 for 2 seconds via GPIO 17
Doing nothing for 5 seconds
Sending power to relay 5 for 2 seconds via GPIO 18
Sending power to relay 6 for 2 seconds via GPIO 27
This doesn't gain much for this example. But if you later need to do some subsequent action on a filter/relay, perhaps turning it off, or sending a refresh pulse, you don't have to remember the right matched pair of relay/gpio numbers to use: just give the relay number and the gpio will be selected 'by magic'.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Mon May 26, 2014 3:27 pm

Thanks again for the help. I have combined the two pieces of code to what I think is correct...

Code: Select all

import time

StartTime = 0

active_time = 2 #should be 20
dwell_time = 5 #should be 20

def gpio_for_relay(relay_number):
    gpio_list = [0, 4, 14, 15, 17, 18, 27]
    return gpio_list[relay_number]

def send_power(relay_to_use, duration):
    gpio_pin = gpio_for_relay(relay_to_use)
    print("Sending power to relay " + repr(relay_to_use) + " for " + repr(duration) +  " seconds via GPIO " + repr(gpio_pin))
    time.sleep(duration)
    
def do_nothing(duration):
    print("Doing nothing for " + repr(duration) +  " seconds" )
    time.sleep(duration)
    
def sequence():
    StartTime = time.time()
    send_power(1, active_time)
    send_power(2, active_time)
    send_power(3, active_time)
    do_nothing(dwell_time)
    send_power(4, active_time)
    do_nothing(dwell_time)
    send_power(5, active_time)
    send_power(6, active_time)

sequence()

def buttonPushed():
    # Instantly start the “sequence” when the button is pushed, which sends power to GPIO 23.
    DO_SOMETHING_WITH_GPIO_HERE I don’t know what you mean by this?
    if buttonIsPushed:
        return True
    return False

while 1:
    # first, see if it has been >24 hours since running, or if we just started.  Or, is the button being pushed?
    if time.time() - StartTime > 24*60*60 or buttonPushed():
        run_sequence()
However i am confused about the do_something_with_gpio_here portion? I want to use GPIO 23 as an input so that when it reads true IE supplied with power from the button press, it will start the sequence.

Next thing is to make this program automatically start when power is supplied to the Raspi. I will not have this hooked up to a monitor so in case the power goes out and the Pi turns off I want it to reload the program automatically when power is restored.

Thanks again,
Zac

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Automating Multiple Valves...

Mon May 26, 2014 5:24 pm

A couple of thoughts.

1] It doesn't matter what you think is correct... the Python interpreter is the final arbiter of whether or not your script is correct. I don't think you can have tried running your script as I think it will report errors. It doesn't matter that the script is not functionally complete - you can still test that what you have written so far is correct.

2] I suggest you need to take a step sideways from developing this script to find and follow some basic tutorials that explain how to connect and control an LED and a switch. This will give you a better understanding of what your other helper means by DO SOMETHING WITH GPIO. (GPIO is the name for the programmable input/output pins on the RPi, to which you will connect your external circuitry.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

MadCow42
Posts: 106
Joined: Sun Jul 01, 2012 12:48 am

Re: Automating Multiple Valves...

Tue May 27, 2014 6:23 pm

DeeJay wrote:A couple of thoughts.

1] It doesn't matter what you think is correct... the Python interpreter is the final arbiter of whether or not your script is correct. I don't think you can have tried running your script as I think it will report errors. It doesn't matter that the script is not functionally complete - you can still test that what you have written so far is correct.

2] I suggest you need to take a step sideways from developing this script to find and follow some basic tutorials that explain how to connect and control an LED and a switch. This will give you a better understanding of what your other helper means by DO SOMETHING WITH GPIO. (GPIO is the name for the programmable input/output pins on the RPi, to which you will connect your external circuitry.)
Yep... it sounds like a good project, but you're biting off a bit more than you can chew for a first project. That's not a bad thing, but you need to do some of the basic legwork to get there (like experimenting with reading a switch and lighting an LED through the GPIO... examples of both can be found on the forums).

For code, you've made a few errors in combining them together. I've patched and commented below.

Code: Select all

import time

StartTime = 0

active_time = 2 #should be 20
dwell_time = 5 #should be 20

def gpio_for_relay(relay_number):
    gpio_list = [0, 4, 14, 15, 17, 18, 27]
    return gpio_list[relay_number]

def send_power(relay_to_use, duration):
    gpio_pin = gpio_for_relay(relay_to_use)
    print("Sending power to relay " + repr(relay_to_use) + " for " + repr(duration) +  " seconds via GPIO " + repr(gpio_pin))
    # you're actually missing the GPIO commands to actually DO anything here... but this is where it goes.  Toggle the GPIO for the valve, then sleep below, then turn the GPIO back off.
    time.sleep(duration)
    # remember to turn the GPIO off here, after the sleep.
    
def do_nothing(duration):
    print("Doing nothing for " + repr(duration) +  " seconds" )
    time.sleep(duration)
    
def sequence():
    StartTime = time.time()
    send_power(1, active_time)
    send_power(2, active_time)
    send_power(3, active_time)
    do_nothing(dwell_time)
    send_power(4, active_time)
    do_nothing(dwell_time)
    send_power(5, active_time)
    send_power(6, active_time)

#sequence() YOU DON"T NEED THIS.  The "while 1" loop below is what runs the program continually/repeatedly.  This actually wouldn't harm anything, but it would force the system to cycle the valves once before doing anything else - every time the system started.

def buttonPushed():
    # Instantly start the “sequence” when the button is pushed, which sends power to GPIO 23.
    DO_SOMETHING_WITH_GPIO_HERE I don’t know what you mean by this?
    # What I mean is to read the input from the GPIO pins, to see if the button is being pressed.
    # if the button IS pressed, then set the variable buttonIsPressed to True, or just return True.  Otherwise, return False, to tell the program that the button is not currently pressed.
    if buttonIsPushed:
        return True
    return False

while 1:
    # this is the main loop of the program, that checks when the valves were last cycled, or if the button is pushed, then acts accordingly.  The "while 1" loop repeats the action continually.
    # first, see if it has been >24 hours since running, or if we just started.  Or, is the button being pushed?
    if time.time() - StartTime > 24*60*60 or buttonPushed():
        run_sequence()
    time.sleep(0.5)  # I put this here so that the while 1 loop doesn't cycle at full processor speed.  That'll burn a lot of unnecessary processing power and energy, heating up the CPU, etc.  A 0.5 wait means that you'll have to hold the button at least 0.5 seconds to ensure it's read though, so you may want to use 0.1 or 0.25 seconds instead.

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Tue May 27, 2014 6:54 pm

I did some more research and since I don't really understand what you guys wrote for me does this type of code work the same way? Lets just assume that there is no more cleaning cycle every 24 hrs, and it only is initiated by a button push...

Code: Select all

import time

active_time = 20

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.setup(4, GPIO.OUT)
GPIO.output(4,1)

GPIO.setup(14, GPIO.OUT)
GPIO.output(14,1)

GPIO.setup(15, GPIO.OUT)
GPIO.output(15,1)

GPIO.setup(17, GPIO.OUT)
GPIO.output(17,1)

GPIO.setup(18, GPIO.OUT)
GPIO.output(18,1)

GPIO.setup(27, GPIO.OUT)
GPIO.output(27,1)

While True:
    if (GPIO.input(23) == 1):
            GPIO.output(4, 0, active_time)
            GPIO.output(14, 0, active_time)
            GPIO.output(15, 0, active_time)
            time.sleep(20)
            GPIO.output(17, 0, active_time)
            time.sleep(20)
            GPIO.output(18, 0, active_time)
            GPIO.output(27, 0, active_time)
            GPIO.output(4, 0, active_time)
            

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Automating Multiple Valves...

Tue May 27, 2014 8:07 pm

I may regret trying to persuade you that programming is not difficult.

Having seen what is possible you really need to do some background reading about how to write a Python script or program.

Once again: the python interpreter will tell you whether what you have written is acceptable. I don't think you can have checked.

If it doesn't fail earlier with anything I haven't spotted, it will certainly fail when it gets to

Code: Select all

GPIO.output(4, 0, active_time)
The RPi.GPIO.output statement only has 2 arguments: the pin number and the state. You can't simply add on additional values and expect them to be understood by some sort of magic. That's why I created the send_power function as an example, which @MadCow24 annotated with details of how it could be used with RPi.GPIO

I'm wondering how much help I have actually been, so I am going to withdraw from this discussion.

Good luck with your project.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Tue May 27, 2014 8:19 pm

MadCow42 wrote:
DeeJay wrote:A couple of thoughts.

1] It doesn't matter what you think is correct... the Python interpreter is the final arbiter of whether or not your script is correct. I don't think you can have tried running your script as I think it will report errors. It doesn't matter that the script is not functionally complete - you can still test that what you have written so far is correct.

2] I suggest you need to take a step sideways from developing this script to find and follow some basic tutorials that explain how to connect and control an LED and a switch. This will give you a better understanding of what your other helper means by DO SOMETHING WITH GPIO. (GPIO is the name for the programmable input/output pins on the RPi, to which you will connect your external circuitry.)
Yep... it sounds like a good project, but you're biting off a bit more than you can chew for a first project. That's not a bad thing, but you need to do some of the basic legwork to get there (like experimenting with reading a switch and lighting an LED through the GPIO... examples of both can be found on the forums).

For code, you've made a few errors in combining them together. I've patched and commented below.

Code: Select all

import time

StartTime = 0

active_time = 2 #should be 20
dwell_time = 5 #should be 20

def gpio_for_relay(relay_number):
    gpio_list = [0, 4, 14, 15, 17, 18, 27]
    return gpio_list[relay_number]

def send_power(relay_to_use, duration):
    gpio_pin = gpio_for_relay(relay_to_use)
    print("Sending power to relay " + repr(relay_to_use) + " for " + repr(duration) +  " seconds via GPIO " + repr(gpio_pin))
    # you're actually missing the GPIO commands to actually DO anything here... but this is where it goes.  Toggle the GPIO for the valve, then sleep below, then turn the GPIO back off.
    time.sleep(duration)
    # remember to turn the GPIO off here, after the sleep.
    
def do_nothing(duration):
    print("Doing nothing for " + repr(duration) +  " seconds" )
    time.sleep(duration)
    
def sequence():
    StartTime = time.time()
    send_power(1, active_time)
    send_power(2, active_time)
    send_power(3, active_time)
    do_nothing(dwell_time)
    send_power(4, active_time)
    do_nothing(dwell_time)
    send_power(5, active_time)
    send_power(6, active_time)

#sequence() YOU DON"T NEED THIS.  The "while 1" loop below is what runs the program continually/repeatedly.  This actually wouldn't harm anything, but it would force the system to cycle the valves once before doing anything else - every time the system started.

def buttonPushed():
    # Instantly start the “sequence” when the button is pushed, which sends power to GPIO 23.
    DO_SOMETHING_WITH_GPIO_HERE I don’t know what you mean by this?
    # What I mean is to read the input from the GPIO pins, to see if the button is being pressed.
    # if the button IS pressed, then set the variable buttonIsPressed to True, or just return True.  Otherwise, return False, to tell the program that the button is not currently pressed.
    if buttonIsPushed:
        return True
    return False

while 1:
    # this is the main loop of the program, that checks when the valves were last cycled, or if the button is pushed, then acts accordingly.  The "while 1" loop repeats the action continually.
    # first, see if it has been >24 hours since running, or if we just started.  Or, is the button being pushed?
    if time.time() - StartTime > 24*60*60 or buttonPushed():
        run_sequence()
    time.sleep(0.5)  # I put this here so that the while 1 loop doesn't cycle at full processor speed.  That'll burn a lot of unnecessary processing power and energy, heating up the CPU, etc.  A 0.5 wait means that you'll have to hold the button at least 0.5 seconds to ensure it's read though, so you may want to use 0.1 or 0.25 seconds instead.

Sorry guys for some reason i didn't see your response fixing my code. In your example you do not import the RPi.GPIO as GPIO

Is that not needed the way your wrote the code? The raspberry pi will know what to do? I am going to build the circuitry tomorrow so I can test this then.

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Thu May 29, 2014 11:50 pm

Okay I just don't have the time to work this out right now and so I am offering to pay for someone to fully write this script for me so that I can just load it onto the pi and get it working. I don't know if this is allowed here but I figured I would at least ask. If you want to write this script for me I will give you all the info you need and I can Paypal the money to you.

I have already outlined what i want it to do but here it is again...
Pi is connect to power and the program automatically starts running.
Every 24 hours the following program is executed
send power to relay 1 for 20 seconds
Turn off relay 1
send power to relay 2 for 20 seconds
turn off relay 2
send power to relay 3 for 20 seconds
turn off relay 3
do nothing for 20 seconds
then
send power to relay 4 for 20 seconds
turn off relay 4
do nothing for 20 seconds
then
send power to relay 5 for 20 seconds
turn off relay 5
send power to relay 6 for 20 seconds
turn off relay 6

There will also be a manual start button connected to a GPIO inlet pin that executes that exact program and then restarts the counter, so 24 hrs from the button push the program executes again.

My e-mail address is Zac@DeepwaterKoi.com for payment inquires.
Zac

johndough
Posts: 254
Joined: Sun Jan 13, 2013 2:00 pm

Re: Automating Multiple Valves...

Fri May 30, 2014 8:32 am

Zakki wrote:Okay I just don't have the time to work this out right now and so I am offering to pay for someone to fully write this script for me so that I can just load it onto the pi and get it working. I don't know if this is allowed here but I figured I would at least ask. If you want to write this script for me I will give you all the info you need and I can Paypal the money to you.

I have already outlined what i want it to do but here it is again...
Pi is connect to power and the program automatically starts running.
Every 24 hours the following program is executed
send power to relay 1 for 20 seconds
Turn off relay 1
send power to relay 2 for 20 seconds
turn off relay 2
send power to relay 3 for 20 seconds
turn off relay 3
do nothing for 20 seconds
then
send power to relay 4 for 20 seconds
turn off relay 4
do nothing for 20 seconds
then
send power to relay 5 for 20 seconds
turn off relay 5
send power to relay 6 for 20 seconds
turn off relay 6

There will also be a manual start button connected to a GPIO inlet pin that executes that exact program and then restarts the counter, so 24 hrs from the button push the program executes again.

My e-mail address is Zac@DeepwaterKoi.com for payment inquires.
Zac

Hi

Well a multi-channel time clock would probably do what you want. OR 5 single channel.

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Mon Jun 02, 2014 3:17 pm

Ok I haven't had success with someone writing it for me so I am back at it a different way. I am getting a syntax error on line 27, and I don't understand why...

Code: Select all

import time

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.setup(18, GPIO.OUT)
GPIO.output(18,1)

GPIO.setup(27, GPIO.OUT)
GPIO.output(27,1)

GPIO.setup(23, GPIO.OUT)
GPIO.output(23,1)

GPIO.setup(17, GPIO.OUT)
GPIO.output(17,1)

GPIO.setup(8, GPIO.OUT)
GPIO.output(8,1)

def my_callback(channel):
    GPIO.output(18, 0)
    time.sleep(20)
    GPIO.output(18, 1)
    GPIO.output(27, 0)
    time.sleep(20)
    GPIO.output(27, 1)
    GPIO.output(23, 0)
    time.sleep(20)
    GPIO.output(23, 1)
    time.sleep(20)
    GPIO.output(17, 0)
    time.sleep(20)
    GPIO.output(17, 1)
    time.sleep(20)
    GPIO.output(8, 0)
    time.sleep(20)
    GPIO.output(8, 1)

GPIO.add_event_detect(25, GPIO.RISING, callback=my_callback, bouncetime=1000)

GPIO.cleanup()

User avatar
DougieLawson
Posts: 35790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Automating Multiple Valves...

Mon Jun 02, 2014 3:25 pm

When I cut and pasted your code and ran it with sudo python (python2) it worked without any errors.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Mon Jun 02, 2014 6:34 pm

I saved that piece of code as test.py in the home directory.
I then open LXTerminal and type... sudo python test.py and it returns the following...
File "test.py", line 27
SyntaxError: Non-ASCII character '\xc2' in file test.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


I don't know what I am doing wrong.

Zakki
Posts: 16
Joined: Thu Feb 27, 2014 12:58 pm

Re: Automating Multiple Valves...

Tue Jun 03, 2014 2:38 pm

Here is something a friend of mine created and i am going to test it out to see if it works...

Code: Select all

__author__ = 'Patrick'
import sched, RPIO, time
#Setup...
RPIO.setup(4, RPIO.OUT)
RPIO.setup(14, RPIO.OUT)
RPIO.setup(15, RPIO.OUT)
RPIO.setup(17, RPIO.OUT)
RPIO.setup(18, RPIO.OUT)
RPIO.setup(27, RPIO.OUT)
RPIO.output(4, 0)
RPIO.output(14, 0)
RPIO.output(15, 0)
RPIO.output(17, 0)
RPIO.output(18, 0)
RPIO.output(27, 0)
RPIO.setup(23, RPIO.IN)
sleep_time = 20
#Just turn power on for 20 seconds and then off.
def cycle(pin):
    print('pin is %d',pin)
    global sleep_time
    if pin is not 0:
        RPIO.output(pin, 0)
    time.sleep(sleep_time)
    if pin is not 0:
        RPIO.output(pin, 1)
# 0 is used as a non-cycle. It will just cause the timeout but no other pins turning on.
def main():
        cycle(4)
        cycle(14)
        cycle(15)
        cycle(0)
        cycle(17)
        cycle(0)
        cycle(18)
        cycle(27)
#Execute on startup
main()
#Add the hook for button functionality
RPIO.add_interrupt_callback(23, main, pull_up_down=RPIO.PUD_DOWN)
#Wait for input
RPIO.wait_for_interrupts()

Return to “Python”