MG 996R servo Raspberry Pi angle control - python

I am using an MG 996R servo connected to a Raspberry Pi and an external power supply. I am using this code:
import RPi.GPIO as GPIO
import time
servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
p.start(2.5) # Initialization
try:
while True:
p.ChangeDutyCycle(5)
time.sleep(0.5)
p.ChangeDutyCycle(7.5)
time.sleep(0.5)
p.ChangeDutyCycle(10)
time.sleep(0.5)
p.ChangeDutyCycle(12.5)
time.sleep(0.5)
p.ChangeDutyCycle(10)
time.sleep(0.5)
p.ChangeDutyCycle(7.5)
time.sleep(0.5)
p.ChangeDutyCycle(5)
time.sleep(0.5)
p.ChangeDutyCycle(2.5)
time.sleep(0.5)
except KeyboardInterrupt:
p.stop()
GPIO.cleanup()
But all I get is a continuous rotation with some random slowing downs.
My aim is to be able to rotate +90 and -90 degrees.

Some MG996R servos have been modified for continuous rotation. This means that what you send it doesn't set the angle, but the direction and speed of rotation.
I suspect you have one of those modified servos.
Strangely, here's a post of somebody having about the opposite problem with the same type of servo.

Related

Wait for input during several iterations of a loop in python

I'm trying to write a python code to control the voltage of the GPIO Pins.
The idea behind it is that in a frequency of 1KHz if half of the time the pin is off and half of the time it is on than the voltage will be 50%.
I need to keep the while loop running while waiting for user input so that I can change the speed of the motor.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
# The higher the number for speed the slower the motor will rotate
def motor(speed):
i = 0
run = True
# Runs until user tells it to stop
while run == True:
# Pins should vary at every second
time.sleep(0.001)
i += 1
if i % speed == 0:
GPIO.output(4, GPIO.HIGH)
else:
GPIO.output(4, GPIO.LOW)
# This block is causing the issue
# I need the loop to keep running while waiting for the input
if i % speed * 10 == 0:
com = int(input("Enter 0 to quit"))
if com == -1:
GPIO.output(4, GPIO.LOW)
break
GPIO.output(4, GPIO.LOW)
run = True;
while (run):
speed = int(input("Select Speed"))
if speed == 0:
run = False
else:
motor(speed)
GPIO.cleanup()

ultrasonic sensor didn't work with raspberry pi

I have a problem with raspberry pi 3b and ultrasonic sensor
I want to sense and indicate the obstacle
I connect it as many tutorial on google and measure it a while ago and it was working before
today i came and execute same code with same connection, it didn't need to measure
the problem is that the echo pin didn't become 1
this is the code i used , I made a print statement to debug, but only testttt was printed.
enter code here
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO_TRIGGER = 12
GPIO_ECHO = 16
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
while True:
GPIO.output(GPIO_TRIGGER, 0)
time.sleep(2)
GPIO.output(GPIO_TRIGGER, 1)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, 0)
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
print"testttt"
While GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
print"test"
TimeElapsed = StopTime - StartTime
distance = TimeElapsed * 17150
dis=round(distance,2)
print"distabce=" +str(dis)+"cm"
distance()

Ultrasonic sensor, dc motor and sw420 in Python

I am developing a system where I am avoiding an accident by slowing down the motor speed using distance provided by ultrasonic sensor. Also, if accident happens, it must be sense through sw420 and accident occurred result should be displayed.
The problem is that, i have code for both motor mgmt. - distance sensing and impact detection. But as I want them to run parallel i.e.., both are running at same time. How to do it?
Eg. If car is running smoothly, suddenly another car hits you from, let's say, from back.....then automatically, sw420 sensor should sense it and alert the accident occurred message.
Or in other words, some type of multiprocessing concept is needed which i am unaware of.
Here's what i have done till now -
1. Motor is perfectly slowing down when ultrasonic sensor senses shorter distance.
import RPi.GPIO as GPIO
import time
# Define GPIO For Driver motors
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(7,gpio.OUT)
pwm=GPIO.PWM(18,100)
# Define GPIO for ultrasonic central
GPIO_TRIGGER_CENTRAL = 23
GPIO_ECHO_CENTRAL = 24
GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT) # Trigger > Out
GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN) # Echo < In
# Functions for driving
def goforward():
pwm.start(100)
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def goforwardslow():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def stopmotors():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
def buzzer():
try:
while True:
gpio.output(7,0)
time.sleep(0.2)
gpio.output(7,1)
time.sleep(0.2)
except KeyboardInterrupt:
gpio.cleanup()
exit
#Detect front obstacle
def frontobstacle():
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
# Allow module to settle
time.sleep(0.2)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER_CENTRAL, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
stop = time.time()
# Calculate pulse length
elapsed = stop - start
# Distance pulse travelled in that time is time
# Multiplied by the speed of sound (cm/s)
distance = elapsed * 17150 # distance of both directions so divide by 2
print "Front Distance : %.1f" % distance
return distance
# Check front obstacle and stop if there is an obstacle
def checkanddrivefront():
while frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
time.sleep(3)
goforward()
# Avoid obstacles and drive forward
def obstacleavoiddrive():
goforward()
start = time.time()
# Drive 5 minutes
while start > time.time() - 300: # 300 = 60 seconds * 5
if frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
goforwardslow()
checkanddrivefront()
#elif rightobstacle() < 30:
# stopmotors()
# checkanddriveright()
#elif leftobstacle() < 30:
# stopmotors()
#checkanddriveleft()
# Clear GPIOs, it will stop motors
#cleargpios()
#def cleargpios():
#print "clearing GPIO"
#GPIO.output(12, False)
#GPIO.output(13, False)
#print "All GPIOs CLEARED"
def main():
# First clear GPIOs
#cleargpios()
print "start driving: "
# Start obstacle avoid driving
obstacleavoiddrive()
if __name__ == "__main__":
main()
Vibrations is also detecting by sw420
import RPi.GPIO as GPIO
from time import sleep
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
sleep(0.1)
while True:
result = GPIO.input(channel)
if result == 1:
print ("Vibration")
Now the problem is that, where should i place or how can i use vibration/sw420 code in the MAIN CODE so that both run parallel and whenever accident happens, sw420'code occupies higher priority than motor code and gives accident occurred message.
Please share your advice.
NOW THIS IS WHAT LATEST I HAVE DONE TILL NOW -
import RPi.GPIO as GPIO
import time
from multiprocessing import Process
# Define GPIO For Driver motors
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
pwm=GPIO.PWM(18,100)
channel = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(channel, GPIO.IN)
sleep(0.1)
# Define GPIO for ultrasonic central
GPIO_TRIGGER_CENTRAL = 23
GPIO_ECHO_CENTRAL = 24
GPIO.setup(GPIO_TRIGGER_CENTRAL, GPIO.OUT) # Trigger > Out
GPIO.setup(GPIO_ECHO_CENTRAL, GPIO.IN) # Echo < In
# Functions for driving
def goforward():
pwm.start(100)
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def goforwardslow():
GPIO.output(12, True)
GPIO.output(16, False)
GPIO.output(18, True)
def stopmotors():
GPIO.output(12, False)
GPIO.output(16, False)
GPIO.output(18, False)
def buzzer():
while True:
GPIO.output(7,0)
time.sleep(0.2)
GPIO.output(7,1)
time.sleep(0.2)
def vib():
while True:
result = GPIO.input(channel)
if result == 1:
print ("Vibration")
#Detect front obstacle
def frontobstacle():
# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
# Allow module to settle
time.sleep(0.2)
# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER_CENTRAL, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER_CENTRAL, False)
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 0:
start = time.time()
while GPIO.input(GPIO_ECHO_CENTRAL) == 1:
stop = time.time()
# Calculate pulse length
elapsed = stop - start
# Distance pulse travelled in that time is time
# Multiplied by the speed of sound (cm/s)
distance = elapsed * 17150 # distance of both directions so divide by 2
print "Front Distance : %.1f" % distance
return distance
# Check front obstacle and stop if there is an obstacle
def checkanddrivefront():
while frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
time.sleep(3)
goforward()
# Avoid obstacles and drive forward
def obstacleavoiddrive():
goforward()
start = time.time()
# Drive 5 minutes
while start > time.time() - 300: # 300 = 60 seconds * 5
if frontobstacle() < 15:
pwm.ChangeDutyCycle(25)
buzzer()
goforwardslow()
checkanddrivefront()
#elif rightobstacle() < 30:
# stopmotors()
# checkanddriveright()
#elif leftobstacle() < 30:
# stopmotors()
#checkanddriveleft()
# Clear GPIOs, it will stop motors
#cleargpios()
#def cleargpios():
#print "clearing GPIO"
def main():
print "start driving: "
# Start obstacle avoid driving
obstacleavoiddrive()
if __name__ == "__main__":
p1 = multiprocess.Process(target = main)
p2 = multiprocess.Process(target = vib)
p1.start()
p2.start()
I HAVE USED MULTIPROCESSING ....BUT YET AGAIN ONLY MOTOR DRIVE FUNCTION IS EXECUTING, VIBRATION CODE IS NOT ACTIVE IN THE CODE....means no output from it.

Raspberry PI: 2 buttons, 2 LEDs

I'm trying to make a Python program with Raspberry Pi that when I push a button, only the red LED brightens, and when I press another button, only the green LED brightens. When no buttons are pushed, all the LEDs are off. But when I try to run it, both LEDs stay on, and when I press the button, the monitor says I pressed it, but nothing changes. What can I do to fix this? I'm 100% certain there are no wiring problems, and the monitor shows no errors. BTW, here's the code:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
red_walk_LED = 16
green_traf_LED = 15
Btn_one = 22 # pin12 --- button
Btn_two = 29 # pin29 --- 2nd button
# GLOBAL VARIABLES
red_Led_status = 1
Green_Led_status = 1
flag_btn_one_pushed = 0
flag_btn_two_pushed = 0
def all_leds_off():
GPIO.output(green_traf_LED, GPIO.HIGH)
GPIO.output(yellow_traf_LED, GPIO.HIGH)
GPIO.output(red_traf_LED, GPIO.HIGH)
GPIO.output(red_walk_LED, GPIO.HIGH)
GPIO.output(white_walk_LED, GPIO.HIGH)
def setup():
global green_LED_frequence
global red_LED_frequence
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
#set LEDs as outputs
GPIO.setup(green_traf_LED, GPIO.OUT)
GPIO.setup(red_traf_LED, GPIO.OUT)
GPIO.setup(Btn_one, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode as input, and pull up to high level(3.3V)
GPIO.setup(Btn_two, GPIO.IN, pull_up_down=GPIO.PUD_UP)
red_LED_frequence = GPIO.PWM(red_traf_LED, 1000) # set Frequece to 1KHz
green_LED_frequence = GPIO.PWM(green_traf_LED, 1000)
red_LED_frequence.start(0) # Duty Cycle = 0
green_LED_frequence.start(0)
def Btn_one_push(ev=None):
print('OK, the 1st button was pushed')
global red_Led_status #we are allowed to change these variables in this function
global my_counter
global flag_btn_one_pushed
global red_LED_frequence
red_Led_status = not red_Led_status #change LED status 0-1 or 1-0
flag_btn_one_pushed = 1
my_delay = 0.2
GPIO.output(green_traf_LED, GPIO.HIGH)
if red_Led_status == 1: #1-on
print('ok, reds on')
for dc in range(0, 101, 4): # Increase duty cycle: 0~100
red_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.02)
for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
red_LED_frequence.ChangeDutyCycle(dc)
time.sleep(0.02)
all_leds_off() #turn all LEDs off!!
flag_btn_pushed = 0
def Btn_two_push(ev=None):
print('OK, the 2nd button was pushed')
global Green_Led_status
global flag_btn_two_pushed
global green_LED_frequence
Green_Led_status = not Green_Led_status #change LED status 0-1 or 1-0
flag_btn_two_pushed = 1
my_delay = 0.2
GPIO.output(red_traf_LED, GPIO.HIGH)
if Green_Led_status == 1: #1-on
print('This is supposed to work!')
for dc in range(0, 101, 4): # Increase duty cycle: 0~100
print(green_LED_frequence)
green_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.08)
for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
green_LED_frequence.ChangeDutyCycle(dc)
time.sleep(0.08)
all_leds_off() #turn all LEDs off!!
flag_btn_two_pushed = 0
def loop():
global flag_btn_one_pushed
global flag_btn_two_pushed
GPIO.add_event_detect(Btn_one, GPIO.FALLING, callback=Btn_one_push) # wait for change in GPIO 0-1 or 1-0
GPIO.add_event_detect(Btn_two, GPIO.FALLING, callback=Btn_two_push)
while True: #when the button isn't pushed, do this
all_leds_off()
else:
pass
def destroy():
all_leds_off()
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
You should try removing these lines:
while True: #when the button isn't pushed, do this
all_leds_off()
else:
pass
and replacing them with:
all_leds_off()
The callbacks should be fired every button press.
Here is another example to look at:
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=102631&p=760629
Also, have you tried some simple snippets of code to individually turn the LED's on and off and detect the button presses to prove that's its not a wiring problem? It doesn't take much to get the wiring wrong!

LED control on Raspberry Pi by GPIO with Python

I am using momentary switches wired to GPIO pins on a Raspberry Pi to control 4 LEDs.
I have five buttons wired up.
The first 4 buttons when pressed toggle the state of a connected LED from on to off or off to on depending on the current state.
The fifth button turns on all 4 LEDs or turns off all 4 LEDs based on the on/off state of GPIO 18.
I also have an extra LED wired to GPIO pin 18, this simply turns on or off based on the state of pin 18.
The idea of this project is to be able to independently control the LEDs and have a master control button as well. The LED attached to pin 18 is a monitoring LED, it should be on if ANY of the 4 LEDs is on and it should only be off if all 4 of the LEDs are off simultaneously.
This is all based on python scripts that monitor for button presses and act accordingly.
The code I've written to control all LEDs at once looks like this:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
chan_list = (4,17,22,27)
GPIO.setup(chan_list,GPIO.OUT)
GPIO.output(chan_list,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
while True:
input_state = GPIO.input(26)
if input_state == False:
chan_list = (4,17,22,27)
GPIO.output(18, not GPIO.input(18))
time.sleep(0.1)
GPIO.output(chan_list, GPIO.input(18))
time.sleep(0.4)
This code appears to operate perfectly. As you can see it toggles the current state of pin 18 and then applies that state to pins 4,17,22 and 27 which are the pins the LEDs are connected to.
The code I've written to control the individual LEDs is a bit more complicated and it looks like this:
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
while True:
input_state = GPIO.input(5)
if input_state == False:
if GPIO.output(17,0) == False:
GPIO.output(4, not GPIO.input(4))
elif GPIO.output(22,0) == False:
GPIO.output(4, not GPIO.input(4))
elif GPIO.output(27,0) == False:
GPIO.output(4, not GPIO.input(4))
else:
GPIO.output(4, not GPIO.input(4))
time.sleep(0.1)
GPIO.output(18, GPIO.input(4))
time.sleep(0.4)
There are 4 versions of this with the pins adjusted, one version for each LED, This version uses pin 5 as an input to detect button presses and pin 4 as an output to activate the LED. What I want is to do is this:
When pressed (if LED1 is off)
- Toggle LED1 on, also toggle pin 18 on to activate the indicator light.
- Take no further action.
When pressed (if LED1 is on)
- Check if pin 17 is on or off;
- If pin 17 is on toggle LED1 off and take no further action.
- If pin 17 is off check if pin 22 is on or off;
- If pin 22 is on toggle LED1 off and take no further action.
- If pin 22 is off check if pin 27 is on or off;
- If pin 27 is on toggle LED1 off and take no further action.
- If pin 27 is off toggle LED1 off then set pin 18 to the current status of pin 4 (LED1).
However, what is actually happening is this:
When pressed (if LED1 is off)
- Turns off all of the other 3 LEDs then toggles LED1 on and toggles pin 18 on.
When pressed (if LED1 is off)
- Turns off all LEDs and toggles pin 18 off.
I can not for the life of me figure this out.
Your help is hugely appreciated.
P.S. Pardon my ignorance, I started learning python yesterday and have no prior programming experience at all. I'm sure it's something simple but I can't seem to solve it.
Sounds like you have 3 logical blocks:
Individual LED control
Master LED control
Monitor-LED indicator control
Decouple your code into functions, let's name them checkIndividualButton, checkMasterButton and updateMonitorLed, one for each logical block, and call them from your main loop
import RPi.GPIO as GPIO
import time
# our 3 functions will go here, yet to be written
# setup pins here
all_leds = [???,???,???,???]
GPIO.setup blah blah blah
while True:
checkIndividualButton(button_pin=17, led_pin=4) # assuming button wired to pin 17 controls LED on pin 4
checkIndividualButton(????, ????) # fill this in
checkIndividualButton(????, ????) # fill this in
checkIndividualButton(????, ????) # fill this in
checkMasterButton(master_button_pin=26, monitor_led_pin=18, all_leds) # notice reference to all_leds which we setup above
updateMonitorLed(all_leds, monitor_led_pin=18)
Now all you have to do is implement individual functions, each doing Just One Job(TM):
def checkIndividualButton(button_pin, led_pin):
is_pressed = GPIO.input(button_pin)
if is_pressed:
GPIO.output(led_pin, not GPIO.input(led_pin))
def checkMasterButton(master_button_pin, monitor_led_pin, all_led_pins):
is_pressed = GPIO.input(master_button_pin)
if is_pressed:
GPIO.output(monitor_led_pin, not GPIO.input(monitor_led_pin))
time.sleep(0.1)
GPIO.output(all_led_pins, GPIO.input(monitor_led_pin))
time.sleep(0.4)
def updateMonitorLed(all_leds_pins, monitor_led_pin):
is_any_led_on = False
for led_pin in all_leds_pins:
if GPIO.input(led_pin):
is_any_led_on = True
GPIO.output(monitor_led_pin, is_any_led_on)
time.sleep(0.1)
Paste this block of functions into the right place in your main program.
DISCLAIMER: I have not tested this. There are ways to optimize and cleanup the code further, happy to guide you in comments if you'd like.
Thank you #pbkhrv for an incredibly helpful answer.
I learned a lot from it and managed to get a piece of code that works perfectly for my needs.
In the end I have 2 python scripts running, one which changes the pin status on the press of a button:
import RPi.GPIO as GPIO
import webiopi
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
chan_list = (4,17,22,27)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)
GPIO.setup(22,GPIO.OUT)
GPIO.output(22,0)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
while True:
#This portion is pin 4 control from physical switches
if GPIO.input(5) == False:
GPIO.output(4, not GPIO.input(4))
time.sleep(0.3)
#This portion is pin 17 control from physical switches
if GPIO.input(6) == False:
GPIO.output(17, not GPIO.input(17))
time.sleep(0.3)
#This portion is pin 22 control from physical switches
if GPIO.input(19) == False:
GPIO.output(22, not GPIO.input(22))
time.sleep(0.3)
#This portion is pin 27 control from physical switches
if GPIO.input(13) == False:
GPIO.output(27, not GPIO.input(27))
time.sleep(0.3)
#This portion is pins 4,17,22,27 as one control from physical switches. Toggles all on/off
# based on the current state of pin 18.
if GPIO.input(26) == False:
chan_list = (4,17,22,27)
GPIO.output(18, not GPIO.input(18))
# time.sleep(0.01)
GPIO.output(chan_list, GPIO.input(18))
time.sleep(0.3)
and this which looks after the indicator LED:
import webiopi
import time
GPIO = webiopi.GPIO
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,0)
GPIO.setup(22,GPIO.OUT)
GPIO.output(22,0)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,0)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)
# loop function is repeatedly called by WebIOPi
while True:
#Block to control pin 18 state by pin 4 state
if (GPIO.digitalRead(4) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(4) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 17 state
if (GPIO.digitalRead(17) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(17) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 22 state
if (GPIO.digitalRead(22) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(22) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(27) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
#Block to control pin 18 state by pin 27 state
if (GPIO.digitalRead(27) == GPIO.HIGH):
#webiopi.sleep(0.1)
GPIO.digitalWrite(18, GPIO.HIGH)
if (GPIO.digitalRead(27) == GPIO.LOW):
webiopi.sleep(0.01)
if (GPIO.digitalRead(4) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(17) == GPIO.HIGH):
webiopi.sleep(0.01)
elif (GPIO.digitalRead(22) == GPIO.HIGH):
webiopi.sleep(0.01)
else:
GPIO.digitalWrite(18, GPIO.LOW)
webiopi.sleep(0.01)
These two python scripts launch at the boot of the raspberry pi as well as webiopi which gives me a web UI that can control the LEDs as well.
These 3 things together give me exactly what I was after and a web interface that live updates the button for each LED based on it's current HIGH or LOW status.
This has been a proof of concept, THE LEDs will shortly be replaced by relays which will turn on or off connected sets of speakers in different rooms of my house, Raspberry Pi multiroom audio controller. The raspberry pi will also be the source of the audio to the connected zones with airplay and bluetooth streaming.

Categories