Light switch mode: Make it switch on the LED when the button is pressed and released once and leave it on until the button is pressed and released again.Fewer messages: Only display a message when the button status changes, rather than on every iteration of the loop.I can think of several improvements you could try to make. That’s the input/output (I/O) part sorted, now it’s over to you to improve the script. It keeps going until CTRL+C is pressed, then the ports are cleaned up before exit.otherwise, if not pressed (input port 25 = 0), button status is displayed and the LED is switched off (output port 24 is set to 0).if pressed (input port 25 = 1), button status is displayed and the LED is switched on (output port 24 is set to 1).GPIO.output(24, 0) # set port/pin value to 0/LOW/Falseįinally: # this block will run no matter how the try block exitsĮvery 0.1s, this program checks the button status… GPIO.output(24, 1) # set port/pin value to 1/HIGH/True While True: # this will carry on until you hit CTRL+C tup(24, GPIO.OUT) # set GPIO24 as an output (LED) tup(25, GPIO.IN) # set GPIO25 as input (button) tmode(GPIO.BCM) # set up BCM GPIO numbering This takes bits from each of the previous two programs.įrom time import sleep # this lets us have a time delay (see line 15) This is simply a combination of the circuits from the last two exercises.Ĭircuit for combining button input and LED output with external pull-down resistor Here’s a simple Python program I = GPIO.input(port_or_pin) # read status of pin/port and assign to variable i GPIO.output(port_or_pin, 0) # set an output port/pin value to 0/LOW/False GPIO.output(port_or_pin, 1) # set an output port/pin value to 1/HIGH/True tup(port_or_pin, GPIO.OUT) # set a port/pin as an output tup(port_or_pin, GPIO.IN) # set a port/pin as an input Import RPi.GPIO as GPIO # import RPi.GPIO module But before we do that… Here’s a quick recap of inputs and outputs with RPi.GPIO We’re going to make a simple new program which switches the LED on when the button is pressed and switches it off again when the button is released. We’re going to make a new program which takes parts from both the “read a button press” and the “flash an led every half second” programs. To make it super-easy, we’ll even stick to the same port numbers we used for the last two days. It’s simply a case of doing what we’ve already done in the last two days’ of GPIO basics, but combining them. It’s nothing scary and it’s not new either. Third-party trademarks are used solely for distributing the games indexed herein and no license or other affiliation is implied.Today, it’s time for us to combine inputs and outputs into the same script. What are you waiting for? Game on!Ĭopyright © 2023 Addicting Games, Inc all rights reserved. Looking for more games? Shockwave has a large selection of fun online and download games. has games for everyone - free online games, free download games and new daily games each week, not to mention game reviews and ratings, a community full of gamers and plenty of exclusive member perks. You'll enjoy unlimited play on all download games, no ad interruptions on all online games and brand new games each week. If you're a gamer looking for some serious fun, join Shockwave® UNLIMITED and receive access to exclusive online games and downloads for a low monthly fee. Best of all, is updated daily, making our library of games virtually endless! is the ultimate destination for free online games, free download games, and more! This is your chance to play games 24-7, with a ton of great categories to choose from - Action Games, Adventure Games, Card & Board Games, Chess Games, Jigsaw Games, Family & Kids Games, Music & Photo Games, Puzzle Games, Racing Games, Shooting Games, Sports Games, Strategy Games, Word Games, and more.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |