Showing posts with label LED. Show all posts
Showing posts with label LED. Show all posts
Friday, 5 December 2014
User Desired LED Blinking
STEP 1:
Open LX-Terminal.
Create a file with .py extension
Ex: nano
userinput.py
Nano is a type of editor used to write the code in
that file named userinput.
Type the following code in that file by insert key or by typing letter I on keyboard.
STEP 2:
Program:
#! /usr/bin/python
Import os
Import time
Import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup (17, GPIO.OUT) //configuring 17th
pin as output
GPIO.setup (27, GPIO.OUT) // configuring 27th
pin as output
#Setup variables for user input
led_choice = 0
count = 0
os.system (‘clear’)
print “which LED would you like to blink”
print “1: Red?”
print “2: Blue?”
led_choice = input (“choose your option: “) //user choice
if led_choice == 1: //
select led to blink
os.system (‘clear’)
print “You picked the Red LED”
count = input (“How many times would you
like it to blink? : ”) //user
enter purpose
while count > 0 :
GPIO.output
(27, GPIO.HIGH)
time.sleep (1) //to
sleep for one second
GPIO.output (27, GPIO.LOW)
time.sleep
(1) // to sleep for one second
count
= count-1
if led_choice == 2: // select led to blink
os.system
(‘clear’)
print
”You picked the Blue LED”
count
= input (“How many times would you like to blink? : ”) //user
enter purpose
while
count > 0 :
GPIO.output
(17, GPIO.HIGH)
time.sleep
(1) // to sleep for one second
GPIO.output
(17, GPIO.LOW)
Time.sleep
(1) // to sleep for one second
count
= count-1
Save the file by typing :wq
and then press enter.
To check the
output:Type python userinput.py
Output 1:
Output 2:
Output 3:
Output 4:
Program to blink LED
STEP 1:
Open LXTerminal.
Create a file with .py extension
Ex: nano example.py
Nano is a type of editor used to write the code in
that file named example.
Type the following code in that file by insert key or by typing letter I on keyboard.
Program:
#!
/usr/bin/python
Import
time //importing
library called time
Import
RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup
(17, GPIO.OUT) //configuring
17th pin as output
GPIO.setup
(27, GPIO.OUT) //configuring
27th pin as output
#Turn LED’S ON
GPIO.output
(17, GPIO.HIGH)
GPIO.output
(27, GPIO.HIGH)
Time.sleep
(1) //sleep
for one second
#Turn LED’S OFF
GPIO.output
(17, GPIO.LOW)
GPIO.output
(27, GPIO.LOW)
Time.sleep
(1)
# Turn LED’S ON
GPIO.output
(17, GPIO.HIGH)
GPIO.output
(27, GPIO.HIGH)
Time.sleep
(1)
#Turn LED’S OFF
GPIO.output
(17, GPIO.LOW)
GPIO.output
(27, GPIO.LOW)
GPIO.cleanup
LED Blinking using WHILE Loop:
#! /usr/bin/python
Import time
Import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings (False)
GPIO.setup (17, GPIO.OUT) //configuring 17th
pin as output
GPIO.setup (27, GPIO.OUT) //configuring 27th
pin as output
#while basically means loop foreverIt executes the code if some condition is true.
WHILE 1: //give
one tab in the while loop to overcome the indentation error while compiling
GPIO.output
(17, GPIO.HIGH)
GPIO.output
(27, GPIO.HIGH)
Time.sleep
(1)
GPIO.output
(17, GPIO.LOW)
GPIO.output
(27, GPIO.LOW)
Time.sleep
(1)
To check the output:Type python example.py
in lx-terminal.
Output 1:
Output 2: