Sunteți pe pagina 1din 7

Final Raspberry Pi lab

This activity puts together everything we have done with the Raspberry Pi:
basic electronics;
running GNU/Linux applications in the L!" environ#ent and the LTer#inal;
using the Python $!L" editor and shell% and the Turtle Graphics #odule&
Step 1: draw a traffic light control panel
'& (pen the $!L" ) progra#&
*& +lic, the #enu File : New Window to create a blan, -ile&
)& .ave the -ile to /home/pi with the na#e trafficpanel1.py
/& Type the code below and save it&
0& 1it the F5 ,ey to test it&
from turtle import *
def square(color):
fillcolor(color)
begin_fill()
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(180)
end_fill()
def draw_panel():
setworldcoordinates(0, 0, 300, 300)
speed(0)
penup()
goto(100, 300)
square('red')
square('yellow')
square('green')
draw_panel()
ainloop()
'/2
Step 2: make the control panel interactive
'& Use the #enu co##and File : Save as to save the trafficpanel1.py -ile as trafficpanel2.py
*& 3dd the code between the # step 2 co##ents and save to trafficpanel2.py&
)& 1it the F5 ,ey to test it& +lic, on the colored s4uares and note the output in the shell&
from turtle import *
def square(color):
fillcolor(color)
begin_fill()
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(180)
end_fill()
def draw_panel():
setworldcoordinates(0, 0, 300, 300)
speed(0)
penup()
goto(100, 300)
square('red')
square('yellow')
square('green')
# step 2: type code FROM this line
def switch(!, y):
print('clic" at y # ', y)
if 0 <= y < 100:
bgcolor('dar" green')
elif 100 <= y < $00:
bgcolor('%880')
else:
bgcolor('dar" red')
onscreenclic"(switc&)
# step 2: type code TO this line
draw_panel()
ainloop()
*/2
Step 3: build the traffic light circuit
Step 31: connect wires from Raspberry Pi to breadboard
'& Re-er to the picture above to identi-y the Raspberry Pi GP$( pins 3 5 ! and 25&
Note: all these pins are on the le-t side o- the connector 5away -ro# the edge o- the board6&
*& +onnect a red wire -ro# the Raspberry Pi GP$( pin 3 to col"mn # o- the breadboard&
)& +onnect a yellow wire -ro# the Raspberry Pi GP$( pin 5 to col"mn $ o- the breadboard&
/& +onnect a %reen wire -ro# the Raspberry Pi GP$( pin ! to col"mn 12 o- the breadboard&
0& +onnect a &l"e wire -ro# the Raspberry Pi GP$( pin 25 to the &l"e line o- the breadboard&
Step 32: install resistors on breadboard
'& +onnect a #!'( 5or si#ilar6 resistor -ro# col"mn 3 to the &l"e line o- the breadboard&
*& +onnect a #!'( 5or si#ilar6 resistor -ro# col"mn ! to the &l"e line o- the breadboard&
)& +onnect a #!'( 5or si#ilar6 resistor -ro# col"mn 11 to the &l"e line o- the breadboard&
Step 33: install !"#s on breadboard
'& +onnect a red L"! with anode 5long lead6 in col"mn # and
cathode 5short lead6 in col"mn 3.
*& +onnect a yellow L"! with anode 5long lead6 in col"mn $ and
cathode 5short lead6 in col"mn !.
)& +onnect a %reen L"! with anode 5long lead6 in col"mn 12 and
cathode 5short lead6 in col"mn 11.
)/2
Step 3$: revise all the connections
Re-er to the picture below to #a,e sure everything is connected correctly&
//2
Step $: write test program to make !"#s blink
Step $1: type code for ledspy
'& $n the $!L" ) progra#% clic, the #enu File : New Window to create a new -ile&
*& .ave the -ile to /home/pi with the na#e leds.py
)& Type the code below and save it to leds.py&
from tie import sleep
import ate!it
import '(i)*(+, as *(+,
ate!it.register(*(+,.cleanup)
*(+,.setode(*(+,.-,.'/)
01/_(+23 = 4('red', 3), ('yellow', 5), ('green', 6)7
def activate(acti8e_color):
for color, pin in 01/_(+23:
*(+,.output(pin, color == acti8e_color)
for color, pin in 01/_(+23:
*(+,.setup(pin, *(+,.,9:)
*(+,.output(pin, 0)
if __nae__=='__ain__':
for i in range(3):
for color, pin in 01/_(+23:
print color
acti8ate(color)
sleep(.5)
Step $2: test the ledspy program
'& (pen the )*+erminal progra#&
*& 3t the 7 pro#pt% type: s"do python leds.py
Note: Python progra#s that control the GP$( pins #ust be executed by root 5the super user% or
syste# ad#inistrator6& There-ore the progra# above cannot be tested using 80 -ro# $!L" i- you
are logged in as the pi user& 9ou #ust open the ter#inal and use the s"do co##and to run the
progra# as root 5sudo is short -or :super;user do<6&
0/2
Step %: &ntegrate the traffic panel to the !"#s
Step %1: "dit the traffic panel program
9ou need to add =ust / lines to the trafficpanel2.py progra# to #a,e trafficpanel3.py
'& $n the $!L" ) progra#% open the trafficpanel2.py progra#&
*& Use the #enu co##and File : Save as to save it as trafficpanel3.py in the /home/pi -older&
)& 3dd the / lines o- code #ar,ed with # <-- step 5
from turtle import *
from leds import acti8ate # <-- step 5
def square(color):
fillcolor(color)
begin_fill()
fd(100)
rt(90)
fd(100)
rt(90)
fd(100)
rt(180)
end_fill()
def draw_panel():
setworldcoordinates(0, 0, 300, 300)
speed(0)
penup()
goto(100, 300)
square('red')
square('yellow')
square('green')
# step 2: type code FROM this line
def switch(!, y):
print('clic" at y # ', y)
if 0 <= y < 100:
bgcolor('dar" green')
acti8ate('green') # <-- step 5
elif 100 <= y < $00:
bgcolor('%880')
acti8ate('yellow') # <-- step 5
else:
bgcolor('dar" red')
acti8ate('red') # <-- step 5
onscreenclic"(switc&)
# step 2: type code TO this line
draw_panel()
ainloop()
>/2
Step %2: test the integrated traffic panel
'& (pen the )*+erminal progra#&
*& 3t the 7 pro#pt% type these two lines:
, -host .
, s"do python trafficpanel3.py
)& $- everything wor,ed% you should be able to control de tra--ic L"!s by clic,ing on the
colored s4uares in the graphical inter-ace o- trafficpanel3.py
Note: this last step shows how to run a graphical progra#% trafficpanel3.py as root using s"do&
The proble# is that we are logged in as the pi user and when we use s"do to run a graphical
progra# as the root user% GNU/Linux will not allow it because another user cannot control the
current graphics display& The -host . co##and allows other users% including root% to connect to
the display&
? *@'/ by Luciano Ra#alho&
Text% code and i#ages created Luciano Ra#alho using Libre(--ice and 8ritAing&
License: +reative +o##ons B9;.3 5copies and derived wor,s should give credit to the original author and be shared
under the sa#e license; co##ercial use is (C6&
.ource -iles available at: http://github&co#/garoa/hardware;do=os
2/2

S-ar putea să vă placă și