Posts: 6
Threads: 1
Joined: Mar 2017
Hello,
I wrote a programme a while back in C++ whilst at college, i am now trying to re-create it in python.
The project was a musical door bell that used 8 relays to hit bells, i am trying to make this work with a rasberry pi and its GPIO pins.
The structure of the C++ code was a loop that would run twice and would call up one of four labeled 'tunes' at random (the button was pushed and the bells would ring two random tunes of the four written in).
This used the 'goto' command and labels for each tune..... from what i can work out Python doesnt like that or work that way.
What is the easiest way to create the same kind of thing? i have the four tunes written and working, but no idea how to seperate them or to get the program to select at random........ (this is me trying to learn python so be kind)
Any help or direction would be appreciated.
Gill
Posts: 1,298
Threads: 38
Joined: Sep 2016
Firstly, welcome to Python and welcome to the forum.
Posting the Python code you've tried as well as any error codes (the full error code please) would be helpful. Also, explaining the output you are getting versus what you are expecting would also be helpful. Be sure to post any code and errors between the proper 'code tags'. Refer to the Help Document on how to do this properly.
To get you started,
Quote:... or to get the program to select at random
you will want to look into the Random library.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 1
Joined: Mar 2017
Hello,
I dont actually have any code yet other than the 4 tunes. I was trying to work out how to go about writing what i needed to.
Thank you for the Random library i will have a look into that  .
Posts: 4,220
Threads: 97
Joined: Sep 2016
If you are trying to get around the lack of goto and labels, I would look at using functions. Have a function for each tune, and call the functions at random. Better yet, have a function that can play any one of the tunes based on input provided to it, and call that function with random values.
Posts: 1,298
Threads: 38
Joined: Sep 2016
Before continuing, you said this is for a Raspberry Pi, what OS are you using? Raspbian or some other OS. Do you have access to Python 3.x or only Python 2.x?
I've not dealt with the Raspberry Pi, but do you 'talk' to the switches in a binary manner? For example: relay 1 = 00000001, relay 2 = 00000010 ....?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 12,038
Threads: 487
Joined: Sep 2016
Mar-26-2017, 03:48 PM
(This post was last modified: Mar-26-2017, 03:49 PM by Larz60+.)
for those of us who use c++, it would be helpful to post that code.
goto labels in c++ ... Ouch
Posts: 6
Threads: 1
Joined: Mar 2017
Mar-28-2017, 06:44 PM
(This post was last modified: Mar-28-2017, 07:25 PM by Larz60+.)
Hello again,
I have managed to work out the randomising and how to define each 'tune' but i cant get my loop to work properly, ity doesnt stop and runs forever.
As the code is on my Pi (which had NOOB on it when i got it and i am writting code in Python 3) i will write a short version of it below, with out the pretty colours.
import random
def R(): (these are the tunes)
def Q():
def RQ():
def T():
List = [R,Q,RQ,T]
count = 0
while count < 2:
random.choice(List)()
count + 1
if count > 2:
break As i say, it runs and randomises amazingly..... it just doesnt stop, do i need to import something at the begining to make it count? or have i just been a muppet a done something blatantly stupid?
Thank you for the help
Its ok i missed the = out of the count + line..... i think im starting to understand this stuff :) :)
Posts: 12,038
Threads: 487
Joined: Sep 2016
Mar-28-2017, 07:32 PM
(This post was last modified: Mar-28-2017, 07:32 PM by Larz60+.)
Not a good idea to name lists List
The break after is count > 2 is not part of the if statement
(these are the tunes) -- This is not a comment
comments have # to start
the functions R, Q, RQ and T have no body - they do nothing
This code will not run as presented
line 3 will give a syntax error
clean up your code, test and re-post
wlhile loop has no indentation.
Posts: 1,298
Threads: 38
Joined: Sep 2016
I agree with Lars60+. I don't see how it can 'run and randomizes amazingly'. Here is an example of runnable code:
import random
""" This is
a multiline comment """
# This is a single line comment
R, Q, RQ, T = "A", "B", "C", "D" # Assign some kind of value to R, Q, RQ, T. Can be anything i.e. integers, floats, strings
tunes = [R, Q, RQ, T] # Add variables to list
count = 0 # Initialize count to 0
while True:
if count <2: # Gives two selections, remember, 'count' comes in as zero
print("\nEntry count ", count)
print(random.choice(tunes)) # For testing, print what 'random is choosing
count += 1
print("Exit count ", count) # For testing, print value of count
else:
print("\nThe 'while' condition is now False, exiting the loop")
break Note: tunes could also be a dictionary, hard to tell with out knowing what they do (maybe correspond to the data pins on the GPIO?)
At some point, you may also want to check out Python Pigpio.
Do not be afraid to use 'print()', at least during your testing phase, it's cheap and easily removed when everything works as intended
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 1
Joined: Mar 2017
Mar-30-2017, 06:10 PM
(This post was last modified: Mar-30-2017, 07:09 PM by sparkz_alot.)
Sorry, the code i put up was just an idea of what i was doing not the actual code.
import random
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pinlist = [2,3,4,17,27,22,10,9]
GPIO.setup(pinlist, GPIO.OUT)
GPIO.output(pinlist,GPIO.HIGH)
def main():
SleepA = 0.2
SleepB = 1
def Rounds():
print ("Rounds")
GPIO.output(2, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(2, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(3, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(3, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(4, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(4, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(17, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(17, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(27, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(27, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(22, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(22, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(10, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(10, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(9, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(9, GPIO.HIGH)
time.sleep(SleepA);
GPIO.cleanup()
def Queens():
print ("Queens")
GPIO.output(2, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(2, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(4, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(4, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(27, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(27, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(10, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(10, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(3, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(3, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(17, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(17, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(22, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(22, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(9, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(9, GPIO.HIGH)
time.sleep(SleepA);
GPIO.cleanup()
def RQueens():
print ("RQueens")
GPIO.output(10, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(10, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(27, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(27, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(4, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(4, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(2, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(2, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(3, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(3, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(17, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(17, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(22, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(22, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(9, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(9, GPIO.HIGH)
time.sleep(SleepA);
GPIO.cleanup()
def Titums():
print ("Titums")
GPIO.output(2, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(2, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(27, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(27, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(3, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(3, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(22, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(22, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(4, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(4, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(10, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(10, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(17, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(17, GPIO.HIGH)
time.sleep(SleepA);
GPIO.output(9, GPIO.LOW)
time.sleep(SleepA);
GPIO.output(9, GPIO.HIGH)
time.sleep(SleepA);
GPIO.cleanup()
Tune = [Rounds,Queens,RQueens,Titums]
count = 0
while count < 2:
random.choice(Tune)()
count += 1
if count > 2:
break
main()
This is what i have so far. However i am getting 'RuntimeWarning: this channel is already in use, continuing anyway.' when the code is run.
If you take out all the GPIO stuff it runs fine, and i have written the GPIO 'tunes' in seperate file, shich also runs fine, however the relays arent triggering when it is all put together like this, any ideas??
Id also just like to say a massive thank you for helping in my muppetry
Moderator sparkz_alot:
Please put code, output and errors between the corresponding code tags. For assistance, please consult the Help Document
|