Created
December 5, 2015 21:21
-
-
Save RamonGilabert/71348b4e7eef6bf2f8cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# As I don't have the sensor light, what I'll do is the same setup without it, then I'll record the first button and second button actions and they will do the following: | |
# 1. None of the buttons pressed: Red is open. | |
# 2. Button 1 pressed: Green is open. | |
# 3. Button 2 pressed: Blue is open. | |
# The two buttons down will turn off the program. | |
# Touching one button or another will trigger an action, that one will be registered and will be saved into a file. | |
import RPi.GPIO as GPIO | |
import time | |
red_pin = 17 | |
green_pin = 27 | |
blue_pin = 22 | |
first_button = 24 | |
second_button = 25 | |
def define_basic_LED_information(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(red_pin, GPIO.OUT) | |
GPIO.setup(green_pin, GPIO.OUT) | |
GPIO.setup(blue_pin, GPIO.OUT) | |
def configure_buttons(): | |
private_configure_buttons(True) | |
private_configure_buttons(False) | |
def first_button_pressed(pin): | |
button_did_press(True) | |
def second_button_pressed(pin): | |
button_did_press(False) | |
# Helpers | |
def button_did_press(first): | |
button_pressed = "First" if first else "Second" | |
string = button_pressed + " button pressed at time: " + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) | |
print(string) | |
file = open( "buttons.txt", "a") | |
file.write(string + "\n") | |
file.close() | |
def private_configure_buttons(first): | |
pin = first_button if first else second_button | |
action = first_button_pressed if first else second_button_pressed | |
GPIO.setup(pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) | |
GPIO.add_event_detect(pin, GPIO.FALLING) | |
GPIO.add_event_callback(pin, action) | |
def build_RGB_value(red, green, blue): | |
GPIO.output(red_pin, red) | |
GPIO.output(green_pin, green) | |
GPIO.output(blue_pin, blue) | |
if __name__ == "__main__": | |
print("With this program you'll be able to do some combinations of colors by pressing the buttons.") | |
define_basic_LED_information() | |
configure_buttons() | |
alive = True | |
try: | |
while alive: | |
if not GPIO.input(first_button) and not GPIO.input(second_button): | |
alive = False | |
elif not GPIO.input(first_button): | |
build_RGB_value(0, 1, 0) | |
elif not GPIO.input(second_button): | |
build_RGB_value(0, 0, 1) | |
else: | |
build_RGB_value(1, 0, 0) | |
finally: | |
build_RGB_value(0, 0, 0) | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment