Wednesday, February 24, 2016

Binary LED with Raspberry Pi



Please see the video to do this -

The python code for LED timing and switching on/off is below -


#!/usr/bin/python

import time
import RPi.GPIO as GPIO
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
#GPIO.setup(16,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(8,GPIO.OUT)
def Blink(numTimes,speed):
    j=1
    for i in range(0,numTimes):## Run loop numTimes
        print "Iteration " + str(i+1)## Print current loop
       
        GPIO.output(3,True)## Switch on pin 7
        time.sleep(speed/j*2)## Wait
        GPIO.output(3,False)## Switch off pin 7
        time.sleep(speed/j*2)## Wait
        GPIO.output(8,True)## Switch on pin 7
        time.sleep(speed/j*3)
        GPIO.output(8,False)## Switch on pin 7
        time.sleep(speed/j*3)
        j=j+1
        print j
       
    print "Done" ## When loop is complete, print "Done"
    GPIO.cleanup()
iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")

## Start Blink() function. Convert user input from strings to numeric data types and pass to Blink() as parameters
Blink(int(iterations),float(speed))

for any help feel free to mail me at medhrk@gmail.com

No comments:

Post a Comment

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...