SlideShare a Scribd company logo
1 of 5
Download to read offline
Enablingthe
AstroPimission
SenseHat
Python3Cheatsheet
Toadd​SenseHAT​functionalitytoyourpythonprogramsaddthefollowinglinestoimportthelibraryfortheSenseHAT
library:
from​sense_hat​import​SenseHat
sense=SenseHat()
FromthatpointforwardsyoucanuseanyofthesetoffunctionsfromtheSenseHATLibrary.
sense.​set_pixel​(0,0,255,0,0) SetsthetopleftLEDtothecolourred.
sense.​show_letter​(“J”,0,0,255) Displaystheletter“J”onthescreeninblue.
sense.​show_message​(“msg”,​​text_colour​=​[​0​,​255​,​0​]) Displaysthemessage“msg”onthematrixingreen.
sense.​load_image​(“creeper.png”,redraw​=​True​) Loadan8x8image“creeper.png”imageanddisplayit.
sense.​clear​() ClearstheLEDandswitchesthemalloff.
sense.​set_rotation(r=0) SetstherotationoftheLEDmatrix.
sense.​set_pixels​(​pixelList​) Usespixellisttodrawapicture,eachitemisan[R,G,B]list
yaw,pitch,roll=sense.get_orientation().values() Getstheorientationdataandstorestheirvaluesas​yaw,pitch,
roll
m_x,m_y,m_z=sense.get_compass_raw().values() Getsthecompassdataandstores as​m_x,m_y,m_z
x,y,z=sense.get_accelerometer_raw().values() Getstheaccelerometerdataandstoresas​x,y,z
g_x,g_y,g_z=sense.get_gyroscope_raw().values() Getstheorientationdataandstoresas​g_x,g_y,g_z
t=sense.get_temperature_from_humidity() Usesthehumiditysensortogettemperatureandstoresitas​t​.
t=sense.get_temperature_from_pressure() Usesthepressuresensortogettemperatureandstoresitas​t​.
h=sense.get_humidity() Measuresthehumidityandstoresitas​h​.
p=sense.get_pressure() Measuresthepressureandstoresitas​p​.
Thereareanumberofwaystocapturetheinputfromthejoystick.Youcouldusetheeitherthe​pygame​or​curses
library.Howeverforthisexamplewe’regoingtousetheevdevsystem,whichyou’llneedtoinstallusing“sudopip3
installevdev”
fromevdevimportInputDevice,ecodes,list_devices
fromselectimportselect
devices=[InputDevice(fn)forfninlist_devices()]
fordevindevices:
ifdev.name=="RaspberryPiSenseHATJoystick":
js=dev
whileTrue:
r,w,x=select([dev.fd],[],[],0.01)
forfdinr:
foreventindev.read():
ifevent.type==ecodes.EV_KEY:#andevent.value==1:
ifevent.code==ecodes.KEY_UP:
print("up")
elifevent.code==ecodes.KEY_LEFT:
print("left")
elifevent.code==ecodes.KEY_RIGHT:
print("right")
elifevent.code==ecodes.KEY_DOWN:
print("down")
else:
print("enter")
Thecodeontheleftlooksthroughthe
availableinputdevicesandfindstheSense-HAT
joystick.
Itthencontinuallycheckthejoystickdevice
andcreatesalistofeventscall​r.
Foreacheventinthelistitcheckswhetherit
wasakeyboardstyleevent.
Itthencomparesthethekeycodetothevalues
forup,down,leftandrightandpirntsa
correspondingmessage
ScrollingMessage
fromsense_hatimportSenseHat
sense=SenseHat()
whileTrue:
sense.show_message("Spaaaaaaace!!",scroll_speed=0.05,text_colour=[255,255,0],back_colour=[0,0,255])
EnvironmentalSensing Rotatingletter“J”
fromsense_hatimportSenseHat
sense=SenseHat()
whileTrue:
t=sense.get_temperature()
p=sense.get_pressure()
h=sense.get_humidity()
t=round(t,1)
p=round(p,1)
h=round(h,1)
msg="Temp=%s,Pressure=%s,
Humidity=%s"%(t,p,h)
sense.show_message(msg,scroll_speed=0.05)
fromsense_hatimportSenseHat
importtime
sense=SenseHat()
sense.show_letter("J")
whileTrue:
x,y,z=sense.get_accelerometer_raw().values()
x=round(x,0)
y=round(y,0)
ifx==-1:
sense.set_rotation(180)
elify==-1:
sense.set_rotation(90)
elify==1:
sense.set_rotation(270)
else:
sense.set_rotation(0)
time.sleep(0.1)
ReactionGame
fromsense_hatimportSenseHat
importtime
importrandom
sense=SenseHat()
#setupthecolours(white,green,red,empty)
w=[150,150,150]
g=[0,255,0]
r=[255,0,0]
e=[0,0,0]
#createthreedifferentcolouredarrows
arrow=[e,e,e,w,w,e,e,e,
e,e,w,w,w,w,e,e,
e,w,e,w,w,e,w,e,
w,e,e,w,w,e,e,w,
e,e,e,w,w,e,e,e,
e,e,e,w,w,e,e,e,
e,e,e,w,w,e,e,e,
e,e,e,w,w,e,e,e]
arrow_red=[e,e,e,r,r,e,e,e,
e,e,r,r,r,r,e,e,
e,r,e,r,r,e,r,e,
r,e,e,r,r,e,e,r,
e,e,e,r,r,e,e,e,
e,e,e,r,r,e,e,e,
e,e,e,r,r,e,e,e,
e,e,e,r,r,e,e,e]
arrow_green=[e,e,e,g,g,e,e,e,
e,e,g,g,g,g,e,e,
e,g,e,g,g,e,g,e,
g,e,e,g,g,e,e,g,
e,e,e,g,g,e,e,e,
e,e,e,g,g,e,e,e,
e,e,e,g,g,e,e,e,
e,e,e,g,g,e,e,e]
pause=3
score=0
angle=0
play=True
sense.show_message("Keepthearrowpointingup",text_colour=[100,100,100])
whileplay==True:
last_angle=angle
whileangle==last_angle:
angle=random.choice([0,90,180,270])
sense.set_rotation(angle)
sense.set_pixels(arrow)
time.sleep(pause)
x,y,z=sense.get_accelerometer_raw().values()
x=round(x,0)
y=round(y,0)
ifx==-1andangle==180:
sense.set_pixels(arrow_green)
score=score+1
elifx==1andangle==0:
sense.set_pixels(arrow_green)
score=score+1
elify==-1andangle==90:
sense.set_pixels(arrow_green)
score=score+1
elify==1andangle==270:
sense.set_pixels(arrow_green)
score=score+1
else:
sense.set_pixels(arrow_red)
play=False
pause=pause*0.95
time.sleep(0.5)
msg="Yourscorewas%s"%(score)
sense.show_message(msg,scroll_speed=0.05,text_colour=[100,100,100])
Livraria Sense hat - resumo

More Related Content

Viewers also liked

Programando em python modulos
Programando em python   modulosProgramando em python   modulos
Programando em python modulos
samuelthiago
 

Viewers also liked (20)

EV3#5: Exercicios com o sensor cor
EV3#5: Exercicios com o sensor corEV3#5: Exercicios com o sensor cor
EV3#5: Exercicios com o sensor cor
 
EV3#6: Exercicios com o sensor rotação
EV3#6: Exercicios com o sensor rotação EV3#6: Exercicios com o sensor rotação
EV3#6: Exercicios com o sensor rotação
 
EV3#4: Exercicios com o sensor de toque
EV3#4: Exercicios com o sensor de toqueEV3#4: Exercicios com o sensor de toque
EV3#4: Exercicios com o sensor de toque
 
EV3#1: Blocos fundamentais
EV3#1: Blocos fundamentaisEV3#1: Blocos fundamentais
EV3#1: Blocos fundamentais
 
Arduino - iniciação à linguagem C: LCD 1602
Arduino - iniciação à linguagem C: LCD 1602Arduino - iniciação à linguagem C: LCD 1602
Arduino - iniciação à linguagem C: LCD 1602
 
EV3#3: Exercicios com o sensor de ultrassons
EV3#3: Exercicios com o sensor de ultrassonsEV3#3: Exercicios com o sensor de ultrassons
EV3#3: Exercicios com o sensor de ultrassons
 
Workshop Arduino + Scratch
Workshop Arduino + ScratchWorkshop Arduino + Scratch
Workshop Arduino + Scratch
 
Lâmpada controlada por relé e arduino programado em S4A
Lâmpada controlada por relé e arduino programado em S4ALâmpada controlada por relé e arduino programado em S4A
Lâmpada controlada por relé e arduino programado em S4A
 
Estudo orientado de circuitos com motor dc programados em S4A
Estudo orientado de circuitos com motor dc programados em S4AEstudo orientado de circuitos com motor dc programados em S4A
Estudo orientado de circuitos com motor dc programados em S4A
 
Arduino - iniciação à linguagem C (entradas e saídas digitais)
Arduino - iniciação à linguagem C (entradas e saídas digitais)Arduino - iniciação à linguagem C (entradas e saídas digitais)
Arduino - iniciação à linguagem C (entradas e saídas digitais)
 
EV3#7: Exercicios seguidor de linha
EV3#7: Exercicios seguidor de linhaEV3#7: Exercicios seguidor de linha
EV3#7: Exercicios seguidor de linha
 
Arduino - iniciação à linguagem C (servomotores)
Arduino - iniciação à linguagem C (servomotores)Arduino - iniciação à linguagem C (servomotores)
Arduino - iniciação à linguagem C (servomotores)
 
EV3#2: Exercícios introdutórios
EV3#2: Exercícios introdutóriosEV3#2: Exercícios introdutórios
EV3#2: Exercícios introdutórios
 
Programação de arduinos com S4A (exercícios com entradas e saídas digitais)
Programação de arduinos com S4A (exercícios com entradas e saídas digitais)Programação de arduinos com S4A (exercícios com entradas e saídas digitais)
Programação de arduinos com S4A (exercícios com entradas e saídas digitais)
 
LED RGB e saída PWM - estudo orientado com S4A
LED RGB e saída PWM - estudo orientado com S4ALED RGB e saída PWM - estudo orientado com S4A
LED RGB e saída PWM - estudo orientado com S4A
 
The can sat_book_2016-2017_versienov2016
The can sat_book_2016-2017_versienov2016The can sat_book_2016-2017_versienov2016
The can sat_book_2016-2017_versienov2016
 
Programando em python modulos
Programando em python   modulosProgramando em python   modulos
Programando em python modulos
 
Ensinando Computação e Fazendo Ciência com Python
Ensinando Computação e Fazendo Ciência com PythonEnsinando Computação e Fazendo Ciência com Python
Ensinando Computação e Fazendo Ciência com Python
 
Python - o que, porque, como e quando
Python - o que, porque, como e quandoPython - o que, porque, como e quando
Python - o que, porque, como e quando
 
Espaço Programação e Eletrónica - Sessão4
Espaço Programação e Eletrónica - Sessão4Espaço Programação e Eletrónica - Sessão4
Espaço Programação e Eletrónica - Sessão4
 

Similar to Livraria Sense hat - resumo

Chapter01_Python.ppt
Chapter01_Python.pptChapter01_Python.ppt
Chapter01_Python.ppt
PigPug1
 

Similar to Livraria Sense hat - resumo (18)

Introducing Reactive Machine Learning
Introducing Reactive Machine LearningIntroducing Reactive Machine Learning
Introducing Reactive Machine Learning
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying Objects
 
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
4Developers: Michał Szczepanik- Kotlin - Let’s ketchup it
 
MCL333_Building Deep Learning Applications with TensorFlow on AWS
MCL333_Building Deep Learning Applications with TensorFlow on AWSMCL333_Building Deep Learning Applications with TensorFlow on AWS
MCL333_Building Deep Learning Applications with TensorFlow on AWS
 
MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...
MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...
MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
 
Session 09 learning relationships.pptx
Session 09 learning relationships.pptxSession 09 learning relationships.pptx
Session 09 learning relationships.pptx
 
Session 09 learning relationships.pptx
Session 09 learning relationships.pptxSession 09 learning relationships.pptx
Session 09 learning relationships.pptx
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 
Python ppt
Python pptPython ppt
Python ppt
 
Assignment 6.1.pdf
Assignment 6.1.pdfAssignment 6.1.pdf
Assignment 6.1.pdf
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
 
Chapter01_Python.ppt
Chapter01_Python.pptChapter01_Python.ppt
Chapter01_Python.ppt
 
Lsl scripts
Lsl scriptsLsl scripts
Lsl scripts
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Blaise_UK_109_Max Kleiner_image2textAPI.pdf
Blaise_UK_109_Max Kleiner_image2textAPI.pdfBlaise_UK_109_Max Kleiner_image2textAPI.pdf
Blaise_UK_109_Max Kleiner_image2textAPI.pdf
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
java set1 program.pdf
java set1 program.pdfjava set1 program.pdf
java set1 program.pdf
 

More from Ana Carneirinho

More from Ana Carneirinho (10)

Arduino - iniciação à linguagem C (entradas analógica)
Arduino - iniciação à linguagem C (entradas analógica)Arduino - iniciação à linguagem C (entradas analógica)
Arduino - iniciação à linguagem C (entradas analógica)
 
Projeto Casa Inteligente (Arduino e Snap4Arduino)
Projeto Casa Inteligente (Arduino e Snap4Arduino)Projeto Casa Inteligente (Arduino e Snap4Arduino)
Projeto Casa Inteligente (Arduino e Snap4Arduino)
 
Circuitos com Sensor PIR (Arduino e S4A)
Circuitos com Sensor PIR (Arduino e S4A)Circuitos com Sensor PIR (Arduino e S4A)
Circuitos com Sensor PIR (Arduino e S4A)
 
BlocklyDuino e mBlock - um estudo comparativo
BlocklyDuino e mBlock - um estudo comparativoBlocklyDuino e mBlock - um estudo comparativo
BlocklyDuino e mBlock - um estudo comparativo
 
Movimento obliquo - simulação (arduino e S4A)
Movimento obliquo - simulação (arduino e S4A)Movimento obliquo - simulação (arduino e S4A)
Movimento obliquo - simulação (arduino e S4A)
 
Espaço Programação e Eletrónica - Sessão5
Espaço Programação e Eletrónica - Sessão5Espaço Programação e Eletrónica - Sessão5
Espaço Programação e Eletrónica - Sessão5
 
Controlo de motor de passo com Snap4Arduino
Controlo de motor de passo com Snap4ArduinoControlo de motor de passo com Snap4Arduino
Controlo de motor de passo com Snap4Arduino
 
Termómetro (Arduino & Scractch)
Termómetro (Arduino & Scractch)Termómetro (Arduino & Scractch)
Termómetro (Arduino & Scractch)
 
Projeto de S. Valentim
Projeto de S. ValentimProjeto de S. Valentim
Projeto de S. Valentim
 
Espaço Programação e Eletrónica - Sessão 3
Espaço Programação e Eletrónica - Sessão 3Espaço Programação e Eletrónica - Sessão 3
Espaço Programação e Eletrónica - Sessão 3
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Livraria Sense hat - resumo