Outils pour utilisateurs

Outils du site


Panneau latéral

GPIO Fun

First test with the GPIO from the RPi. Simple copy from http://www.raspberrypi.org/phpBB3/viewtopic.php?f=45&t=8666 with some simple python code to control 3 leds and get status from 2 Switch. More detail in the comments bellow.

#!/usr/bin/python
 
#based on: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=45&t=8666
#GPIO information: http://elinux.org/RPi_Low-level_peripherals
 
#----------------------IMPORT MODULE---------------------#
 
#Import RPi python GPIO module from http://pypi.python.org/pypi/RPi.GPIO/0.3.1a 
import RPi.GPIO as GPIO
 
#Module time for sleeping specified amount of time...
import time
 
#Ramdom to generate random number
import random
 
#------------------SET UP GPIO---------------------------#
 
#Use the BCM GPIO Number and not the Rpi physical pin number
#Listing of GPIO pin number: http://elinux.org/images/2/2a/GPIOs.png
GPIO.setmode(GPIO.BCM)
 
#GPIO 17,21,22 for LED
GPIO.setup(17, GPIO.OUT) #LED1
GPIO.setup(21, GPIO.OUT) #LED2
GPIO.setup(22, GPIO.OUT) #LED3
 
#GPIO 23,24 for switch
GPIO.setup(23, GPIO.IN) #SW1
GPIO.setup(24, GPIO.IN) #SW2
 
#-------FUNCTION TO GET/SEND VALUE TO GPUI--------------#
 
#Function that return Value (True or False) of both switch
def refresh_sw():
        SW1 = GPIO.input(23)
        SW2 = GPIO.input(24)
        return SW1,SW2
 
#Function that turn LED on/off
 
def led_on(led_n):
        if led_n == 1:
                GPIO.setup(17, False)
        if led_n == 2:
                GPIO.setup(21, False)
        if led_n == 3:
                GPIO.setup(22, False)
 
def led_off(led_n):
        if led_n == 1:
                GPIO.setup(17, True)
        if led_n == 2:
                GPIO.setup(21, True)
        if led_n == 3:
                GPIO.setup(22, True)
 
#------------------ DO SOMETHING ------------------------#
while True:
#       SW1, SW2 = refresh_sw()
#       print SW1,SW2
 
        x = random.randint(1,3)
        y = random.randint(1,3)
        time.sleep(.05)
        led_on(x)
        time.sleep(.05)
        led_off(y)
association/people/arekkusu/projects/gpio_fun.txt · Dernière modification: 2012/11/02 21:14 par sinux