terça-feira, 4 de março de 2014

RaspberryPi + GPIO + Python


This is my first time accessing RaspberryPi GPIO using Python script to do an Cylon animation.


This is my Python code: http://tny.cz/024ae2a7


Vídeo: http://www.youtube.com/watch?v=BJRX3ylw_2w









The Python Code:



#!/usr/bin/env python
# Must be run as root - sudo python blink_cylon.py 
import RPi.GPIO as GPIOimport time
def setup( led ):
        GPIO.setup( led, GPIO.OUT );
        return;
def Liga( led, onoff, wait):
        LED = GPIO.output( led, onoff ); time.sleep(wait);
        return; 
def Padrao( padrao, wait):
        for i in range( 0, 8):
                Liga( LEDs[i] , int(padrao[i]), 0 )
        time.sleep(wait);
        return;
#Estes sao os PINs em modo BOARD
LEDs = [7, 11, 12, 13, 15, 16, 18, 22]
#Estes sao os PINs em modo BCM
#LEDs = [4, 17, 18, 27, 22, 23, 24, 25]
Delay = [0.15, 0.07]
BOTAO = 23 #not set yet ...

GPIO.cleanup();
#GPIO.setmode (GPIO.BCM );
GPIO.setmode (GPIO.BOARD );
ON = GPIO.HIGH; OFF = GPIO.LOW
for a in LEDs: setup( a )
for a in LEDs: Liga( a , ON, 0 )
time.sleep(1)
for a in LEDs: Liga( a , OFF, 0 )
#Testar padrao
Padrao( "11111111",  1)
Padrao( "00000000",  1)
while True:

        i=0;
        Padrao( "10000000",  Delay[1])
        while i<5:
                Padrao( "11000000",  Delay[1])
                Padrao( "01100000",  Delay[1])
                Padrao( "00110000",  Delay[1])
                Padrao( "00011000",  Delay[1])
                Padrao( "00001100",  Delay[1])
                Padrao( "00000110",  Delay[1])
                Padrao( "00000011",  Delay[1])
                Padrao( "10000001",  Delay[1])
                i=i+1;

        i=0;
        Padrao( "11000000",  Delay[1])
        while i<10:
                Padrao( "11000000",  Delay[1])
                Padrao( "01100000",  Delay[1])
                Padrao( "00110000",  Delay[1])
                Padrao( "00011000",  Delay[1])
                Padrao( "00001100",  Delay[1])
                Padrao( "00000110",  Delay[1])
                Padrao( "00000011",  Delay[1])
                Padrao( "00000011",  Delay[1])
                Padrao( "00000110",  Delay[1])
                Padrao( "00001100",  Delay[1])
                Padrao( "00011000",  Delay[1])
                Padrao( "00110000",  Delay[1])
                Padrao( "01100000",  Delay[1])
                Padrao( "11000000",  Delay[1])
                i=i+1;          

        i=0;
        while i<20:
                Padrao( "10000001",  Delay[1])
                Padrao( "01000010",  Delay[1])
                Padrao( "00100100",  Delay[1])
                Padrao( "00011000",  Delay[1])
                Padrao( "00100100",  Delay[1])
                Padrao( "01000010",  Delay[1])
                i=i+1;


Sem comentários:

Enviar um comentário