Jump to content

questions about python

Go to solution Solved by dany_boy,

Eliminate this:

while True:
    GPIO.output(22,False)
    GPIO.output(27,False)
    GPIO.output(17,False)
    while GPIO.input(24) == False: #rood
        print("rood aan")
        GPIO.output(17,True)

    while GPIO.input(23) == False: #groen
        print("groen aan")
        GPIO.output(27,True)

    while GPIO.input(18) == False: #blauw
        print("blauw aan")
        GPIO.output(22,True) 

use if statements instead:

while true:
	if GPIO.input(24) == False:
		print("rood aan")
    	GPIO.output(17,True)
	else:
		GPIO.output(17,True)

repeat for each channel

Hi there,

 

I recently wrote this tiny program just to control a LEDstrip from a raspberry pi with buttons for each color. So far all it can do is light 1 color at the time, when either one of the buttons is pressed. How could I make it so that I can have multiple buttons pressed at the same time? 

 

Another question: How can I make it so that I can use all those different colours? Right now I only know how to have either blue, red, green or all those at the same time.

 

My current code is:

 

import RPi.GPIO as GPIO
import time

 

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT) #port voor rood
GPIO.setup(27,GPIO.OUT) #port voor groen
GPIO.setup(22,GPIO.OUT) #port voor blauw
GPIO.setup(24,GPIO.IN,pull_up_down=GPIO.PUD_UP) #knop voor rood
GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP) #knop voor blauw
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_UP) #knop voor groen


while True:
    GPIO.output(22,False)
    GPIO.output(27,False)
    GPIO.output(17,False)
    while GPIO.input(24) == False: #rood
        print("rood aan")
        GPIO.output(17,True)

    while GPIO.input(23) == False: #groen
        print("groen aan")
        GPIO.output(27,True)

    while GPIO.input(18) == False: #blauw
        print("blauw aan")
        GPIO.output(22,True)

Link to comment
https://linustechtips.com/topic/762820-questions-about-python/
Share on other sites

Link to post
Share on other sites

Eliminate this:

while True:
    GPIO.output(22,False)
    GPIO.output(27,False)
    GPIO.output(17,False)
    while GPIO.input(24) == False: #rood
        print("rood aan")
        GPIO.output(17,True)

    while GPIO.input(23) == False: #groen
        print("groen aan")
        GPIO.output(27,True)

    while GPIO.input(18) == False: #blauw
        print("blauw aan")
        GPIO.output(22,True) 

use if statements instead:

while true:
	if GPIO.input(24) == False:
		print("rood aan")
    	GPIO.output(17,True)
	else:
		GPIO.output(17,True)

repeat for each channel

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to comment
https://linustechtips.com/topic/762820-questions-about-python/#findComment-9639233
Share on other sites

Link to post
Share on other sites

 

 

55 minutes ago, Cyborgium said:

Hi there,

 

I recently wrote this tiny program just to control a LEDstrip from a raspberry pi with buttons for each color. So far all it can do is light 1 color at the time, when either one of the buttons is pressed. How could I make it so that I can have multiple buttons pressed at the same time? 

Sample each of the buttons in your execution loop and set the LED state based on the state of the button. Assuming sampling the input spits out a 0 or 1 and the output can use a 0 or 1, then I would do the following:

while True:
    red_btn = GPIO.input(24)
    green_btn = GPIO.input(23)
    blue_btn = GPIO.input(18)

    GPIO.output(22, red_btn)
    GPIO.output(27, green_btn)
    GPIO.output(17, blue_btn)

And in fact, you could replace the variable names (red_btn, green_btn, blue_btn) when calling GPIO.output with the sampling function. But Python philosophy discourages that.

 

However, if they strictly take a True/False, then:

while True:
    red_btn = GPIO.input(24) == True
    green_btn = GPIO.input(23) == True
    blue_btn = GPIO.input(18) == True
    
    GPIO.output(22, red_btn)
    GPIO.output(27, green_btn)
    GPIO.output(17, blue_btn)

EDIT: Thanks to Python's loose typing, True = 1 and False = 0, but nothing else. So 0 or 1 should work with those functions.

Link to comment
https://linustechtips.com/topic/762820-questions-about-python/#findComment-9639509
Share on other sites

Link to post
Share on other sites

1 hour ago, dany_boy said:

Eliminate this:


while True:
    GPIO.output(22,False)
    GPIO.output(27,False)
    GPIO.output(17,False)
    while GPIO.input(24) == False: #rood
        print("rood aan")
        GPIO.output(17,True)

    while GPIO.input(23) == False: #groen
        print("groen aan")
        GPIO.output(27,True)

    while GPIO.input(18) == False: #blauw
        print("blauw aan")
        GPIO.output(22,True) 

use if statements instead:


while true:
	if GPIO.input(24) == False:
		print("rood aan")
    	GPIO.output(17,True)
	else:
		GPIO.output(17,True)

repeat for each channel

I tried the if statements instead and it worked, thanks!

Link to comment
https://linustechtips.com/topic/762820-questions-about-python/#findComment-9639758
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×