ãã¢ã»ã³ãµãŒã䜿ã£ãŠãRaspberry Piã§æ€ç¥ã§ããããã«ããŸãããã¢ã®ééãæ€ç¥ããŠãã£ã¹ãã¬ã€ã«å šç»é¢è¡šç€ºãšã¹ããŒã«ãŒããé³å£°ãæµããŠããŸããã»ã³ãµãŒãé»åéšåã¯æ°çŸåçšåºŠã§ãã®ã§ãéåžžã«å®äŸ¡ã«è£œäœå¯èœã§ãïŒ
å šç»é¢ã§å€§ããªæååãç»åã衚瀺ããããå¹æé³ã鳎ããææ³ã¯ãä»åã«éããæ§ã ãªå¿çšã«äœ¿ããŸãããªããä»åã®ã³ã³ãã³ãã§ã¯ããã³ãä»ããªãã§åºæ¥ãããã«äœ¿çšéšåã工倫ããŠããŸãã
ã©ãºãã€ãã©ãºãã€åšèŸºæ©åšã®è³Œå ¥ã«ã€ããŠã¯ãå šäœæŠèŠããã£ãã¿ãŒã®ãå¿ èŠãªã¹ãã«ãã®ãªã³ã¯å ãåç §ããŠãã ãããæ¬ãã£ãã¿ãŒã§ã¯ãã以å€ã«å¿ èŠãšãªãéšåãæããŠããŸãã
ïŒã€ãã€èª¬æããŠãæåŸã«å ·äœçãªè³Œå ¥å äŸããªã³ã¯ã§ãŸãšããŸãã
ä»åã¯ãã³ãããŠãäžå䜿ããªããŠãäœããããã«éšåã工倫ããŠããŸããïŒç¹ãããŸãããéæé€ãã°600åçšåºŠã§ãããã³ãä»ãåºæ¥ã人ã¯ãã¬ããããŒããšç«¯åå°ã¯äžèŠã§ãã
ãã©ã¹ãã©ã€ããŒã¯ç«¯åå°ã®ããžãç· ããã®ã«äœ¿çšããŸãã倧ããã¯ããã€ãœãŒã®ç²Ÿå¯ãã©ã€ããŒã»ããã®äžã«å
¥ã£ãŠãããã©ã¹ãã©ã€ããŒã®å Žåã#0
ãããã¯#1
ã§ç· ããããšãåºæ¥ãŸããã
äž¡é¢ããŒãã¯ãã¢ã»ã³ãµãŒãä»®æ¢ãããã®ã«äŸ¿å©ã§ããã»ãããŒããªã©ã§ã代çšå¯èœã§ãã
ãããã家ã«ããã¢ãã§åé¡ãããŸããã100åã§ã賌å
¥å¯èœã§ãã
å
·äœçãªè³Œå
¥å
ã®ãŸãšãã§ãã
ããã¯ãããŸã§äžäŸã§ãã代çšã§ããä»ã®éšåã䜿ã£ãŠããã ããŠæ§ããŸããã
æ©åš | è³Œå ¥å | ãåè | |
ãã¢ã»ã³ãµãŒ | |||
ãžã£ã³ããŒã¯ã€ã€ïŒãªã¹ïŒã¡ã¹ïŒ | |||
ãããã¬ããããŒã | ãã³ãä»ãåºæ¥ã人ã¯äžèŠ | ||
ã¿ãŒããã«ãããã¯2Pã»çžŠã»å° | ãã³ãä»ãåºæ¥ã人ã¯äžèŠ | ||
粟å¯ãã©ã€ã㌠| ãã€ãœãŒãç§æé»åéå | ãã³ãä»ãåºæ¥ã人ã¯äžèŠã家ã«ãããã®ã§ãOK | |
äž¡é¢ããŒã | ãã€ãœãŒãAmazonãªã© | 家ã«ãããã®ã§ãOK |
â»ãã€ãœãŒãšAmazonã®ãªã³ã¯ã¯å²æããŸãã
â»ãã€ãœãŒã®ãããã·ã§ããã¯ïŒååäœã§ã¯è²·ããŸããã®ã§ããè¿ãã®ã·ã§ããã§çŽæ¥è³Œå
¥ããããšããå§ãããŸãã
ããããã¯ãã©ãºãã€åã³åšèŸºéšåã®æ¥ç¶ãRaspberry Piã®åæèšå®ãVNCæ¥ç¶ãªã©ã®æºåãåºæ¥ãŠããåæã§è©±ãé²ããŸãã
ãGeanyãã¯Raspberry Pi OSã«æšæºã§ã€ã³ã¹ããŒã«ãããŠããGUIããã°ã©ãã³ã°ãšãã£ã¿ã§ãã
ç»ååŒçšå ïŒhttps://www.geany.org/
Raspberry Pi OS ã«ã¯ããäžã€ãPythonããã°ã©ãã³ã°å°çšã®GUIãšãã£ã¿ãThonnyããã€ã³ã¹ããŒã«ãããŠããã®ã§ããã2021幎12æã®æç¹ã§ã¯æ¥æ¬èªå ¥åãåºæ¥ãªãäžå ·åããããããä»åã¯Geanyãæ¡çšããŸãã
å§ãã«ã³ãŒããä¿åããããã®äœæ¥å ŽæãäœæããŸãããããŠãŒã¶ãŒã®ããŒã ãã£ã¬ã¯ããªã®çŽäžã«workspace
ãšãããã©ã«ããäœæããŸãããã¡ã€ã«ãããŒãžã£ãŒã§äœæããŠãæ§ããŸããã
cd ~ mkdir workspace
以äžã®æé ã§éããŸãã
ã¡ãã¥ãŒã¢ã€ã³ã³ > ããã°ã©ãã³ã°
> Geany
ã³ãŒããæžããŠå®è¡ããŠã¿ãŸãããã
â»èŠãããã®ããã«ãã©ã³ããµã€ãºã倧ããããŠããŸãã
以äžã®æååãã³ããããŠããçŽé£è¡æ©ã¢ã€ã³ã³ããã¯ãªãã¯ããŠãã ããã
print('hello')
ã©ãã«ãã¡ã€ã«ãä¿åããããèãããã®ã§ãå
ã»ã©äœã£ãworkspace
ãããã«ã¯ãªãã¯ããŸãã
ãã¡ã€ã«åã¯test.py
ãšããŠãã ããã
ã¿ãŒããã«ãç«ã¡äžãããããã°ã©ã ãå®è¡ãããçãäžã«hello
ãšããæååãåºãŠããã°æåã§ãã
â»èŠãããã®ããã«ãã©ã³ããµã€ãºã倧ããããŠããŸãã
ã¿ãŒããã«äžã§Enter
ããŒãæŒããšçµäºã§ãã
ã¿ãŒããã«ã®ååã«ä»¥äžã®æååã衚瀺ãããŠããŸãããããã¯ããã¹ã¯ãŒããå€ããæ¹ãè¯ããããšããã»ãã¥ãªãã£ã®èŠåã§ãããããã°ã©ã ãšã¯çŽæ¥é¢ä¿ãããŸãããæ°ã«ãªã人ã¯Raspberry Piã®èšå®
ãããã¹ã¯ãŒããå€æŽããŸãããã
SSH is enabled and the default password for the 'pi' user has not been changed. This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
äžåºŠä¿åãåºæ¥ãŠããã®ã§ãhello
ãå¥ã®æååã«ããŠãçŽé£è¡æ©ã¢ã€ã³ã³ããã¯ãªãã¯ããã ãã§ãå®è¡ãããããã«ãªããŸãã
æ§ã ãªã¡ãã¥ãŒã衚瀺ããããŸãããä»åŸäœ¿ãã®ã¯ãã³ãŒããæžãå ŽæããšãïŒã€ã®ã¢ã€ã³ã³ãã®ã¿ã§ãã
ä¿åããŠãªãç¶æ ã§å®è¡ããå Žåã¯ãèªåçã«ä¿åãããŠããå®è¡ãããŸãã®ã§ã3ã«ã€ããŠã¯å®è³ªããŸã䜿ããªããããããŸããã
ã³ãŒããæžãå Žæã®æåã倧ãããããå Žåã¯è¡šç€º
> ãã©ã³ããå€æŽ
ãã Size
ãå€ããŠäžãããããã©ã«ãã¯10
ã§ãã
PCã§ã³ããŒããæååãVNCã®Geanyã«è²Œãä»ããéãç»åã®ããã«éžæãããšãã³ããŒæ å ±ãäžæžããããŠããŸãã®ã§æ³šæããŸãããã
解決çã¯éžæããã«è²Œãä»ããããšã§ãã
å°ããããããããšæããŸãã®ã§ãåç»ãäœæããŸããã
å°ãæéãå¢ããŸãããå ã«åé€ããŠãã貌ãä»ããããã«ããŸãããïŒ
次ã®ãã£ãã¿ãŒä»¥éã§ã¯ããããã³ãŒããäœã£ãŠãããŸããããã¡ã€ã«ãæ°èŠäœæããŠãè¯ãã§ãããtest.pyã«è¿œèšãäžæžãã§ãè¯ãã§ããçãããé²ããããããã«è¡ã£ãŠãã ããã
Pythonã¯æè¿ãã䜿ãããŠããããã°ã©ãã³ã°èšèªã®ãã¡ã®ïŒã€ã§ãã
äž»ãªç¹åŸŽãïŒã€æããŸãã
ã±ã£ãšèŠã®ç¹åŸŽãšããŠãã€ã³ãã³ããããããŸããããã¯äžã«ãã³{ }
ã䜿ãããã¹ããŒã¹ã§ãããã¯ã®ãŸãšãŸããè¡šçŸããŠããŸãããªã®ã§ãèŠãç®ãã¹ãããªãšããŠããã®ãç¹åŸŽã§ãã
å
çšã®ç»åã®å Žåã ãšprint('hello')
ãša = 100
ãïŒã€ã®ãããã¯ã«çžåœããŸãã
ããã°ã©ãã³ã°èšèªã«ãã£ãŠã¯ãããã«ããããã³ã³ãã€ã«ããšããå·¥çšãçµãŠãšã©ãŒããŒãã«ããªããšå®è¡ã§ããªãå ŽåããããŸããPythonã¯ãã®ãããªå·¥çšã¯äžèŠã§ãã³ãŒããæžããŠããã«å®è¡ã§ããŸãããšã©ãŒãèµ·ããæç¹ã§ããã°ã©ã ãçµäºãšãªããããåå¿è ã«ãšã£ãŠã䜿ããããèšèªã«ãªã£ãŠããŸãã
Pythonã¯ä»åã®ã³ã³ãã³ãã§äœ¿ããããªãçµã¿èŸŒã¿ãçšéã ãã§ã¯ãªããæ©æ¢°åŠç¿ããã£ãŒãã©ãŒãã³ã°ãªã©ã®ãããŒã¿ãµã€ãšã³ã¹ãããµãŒããŒåŽã®åŠçãæ ããWebããã¯ãšã³ããã«ã䜿ãããšãå¯èœã§ãéåžžã«å€åœ©ã§ãã
ã§ã¯ãããããããã°ã©ãã³ã°ã«å
¥ããŸãã
åå¿è
ã®æ¹ã¯ãèªãã ãã§ã¯ãªããå®éã«Geanyã«ã³ããããŠå®è¡ãè²ã
ãšã³ãŒããèªåãªãã«æ¹é ããªããé²ããããšããå§ãããŸãã
èšå·#
ãæžãããšã§ããã®ïŒè¡ãã³ã¡ã³ãè¡ã«ãªããŸãã
ã³ã¡ã³ãã¯ããã°ã©ã å®è¡æã«ç¡èŠãããŸããäž»ã«ããã°ã©ã ã«é¢ãã泚éãèšè¿°ããããã«äœ¿çšããŸãã
#ãããªæãã§æžããŸã
print('hello')
以äžã®ããã«éäžãããæžããŸãã
print('hello') #ãã®æžãæ¹ãOK
ã©ã¡ããå®è¡çµæãšããŠã¯å€ãããŸããã
ã³ã¡ã³ãã¯æ³šéãæžãç®ç以å€ã«ãããã°ã©ã ã§äžå
·åãåºãæã®åãåãã«ã掻çšã§ããŸãã
å
·äœçã«ã¯ãäžå
·åããããããªéšåã«é¢ããŠãäžæçã«ã³ãŒãã®è€æ°è¡ãã³ã¡ã³ãåããŸãã#
ãïŒè¡ãã€æžããŠããã®ã¯å€§å€ã§ãã®ã§ããšãã£ã¿ã®äŸ¿å©ãªæ©èœã䜿ããŸãã
Geanyã®å Žåã¯ã察象ç®æãéžæããåŸã«Ctrl
+E
ããŒã§ãéžæããè¡ãã³ã¡ã³ãåããããšãã§ããŸããéã«ã³ã¡ã³ãåããå
ã«æ»ãå ŽåãåãããŒã§è¡ãããšãã§ããŸãã
ä»ã®ãšãã£ã¿ã®å Žåã¯ããŒãç°ãªããŸãã®ã§ã泚æãã ããã
ãªããGeanyã®å Žåã¯ããã©ã«ãèšå®ã ãš#
ã ãã§ã¯ãªã# ~
ãä»å ããŸãã~
èªäœã¯æ¶ããŠãåé¡ã¯ãããŸããããæ¶ããå Žåã¯Ctrl
+E
ããŒã§ã¯ã³ã¡ã³ãåãå
ã«æ»ããŸããã®ã§ãæåã§ä¿®æ£ãå¿
èŠãšãªããŸãã
å€æ°ãšã¯ïŒã€ã®å€ãä¿åã§ããäœåºŠãæžãæãå¯èœãªåœ¹å²ãæã£ãŠããŸãã=
ãæžãããšã«ãããå·ŠåŽã®å€æ°ã«å³åŽã®å€ãå
¥ãããšããæå³ã«ãªããŸããæ°åŠã®=
ãšã¯æå³ãç°ãªãã®ã§æ³šæãå¿
èŠã§ãã äŸãã°a = 3776
ãšããã®ã¯å€æ°a
ã«æ°å€3776
ã代å
¥ããŠãããšããæå³ã§ãã Pythonã®å Žåã¯ãæŽæ°ãå°æ°ãæååãåãããã«æ±ããŸãã
ãã ãæååã®å Žåã¯ãå€æ°ãªã®ãå€ãªã®ãå€å¥ãã€ããªããããã¯ã©ãŒãèšå·'
ã§å²ãå¿
èŠããããŸãã
a = 3776
b = 3.14
c = 'jellyfish'
d = ''
count = 100
å®è¡ããŠãäœã衚瀺ãããŸããã
Pythonã®å Žåã¯ããã«ã¯ã©ãŒããšã·ã³ã°ã«ã¯ã©ãŒãã®ã©ã¡ãã§ãè¯ãã®ã§ãããæ¬ã³ã³ãã³ãã§ã¯ã·ã³ã°ã«ã¯ã©ãŒã'
ã«çµ±äžããŸãã
d = ''
ãšãããŸããã空ã®æååãšããæå³ã§ããæ°å€ã®åæå€ãšããŠ0
ããã䜿ãããŸãããæååã®åæå€ãšããŠã¯''
ãè¯ãçšããããŸãã
=
ã®ååŸã«åè§ã¹ããŒã¹ããããŸããããã£ãŠããªããŠãæ§ããŸãããèŠãããã®ããã«å
¥ããŠããŸãããã ããå
šè§ã§ã¯ãªãåè§ã§æžããªããšãšã©ãŒã«ãªããŸãã®ã§ã泚æãã ããã çµæ§ãããã¡ãªãšã©ãŒãªã®ã§ãè©Šãã«å
šè§ã¹ããŒã¹ãå
¥ããŠã¿ãŠã以äžã®ãããªãšã©ãŒè¡šç€ºã«ãªãããšã確ãããŠã¿ãŠäžããã
SyntaxError: invalid character in identifier
æ¢ã«äœ¿ã£ãŠããã®ã§åãããšæããŸããprint
ã¯ãæååãå€æ°ã衚瀺ããŸãã
å¿
ã()
ã䜿ã£ãŠããã®äžã«æååãèšè¿°ããŸãã
print('Mt.Fuji is 3776 meters high')
以äžã¯å€æ°ã衚瀺ããŠããäŸã§ãã
a = 3776
print(a)
ã«ã³ãèšå·,
ã䜿ãããšã«ãããè€æ°ã®æååãå€æ°ãåãè¡ã«è¡šç€ºãããããšãã§ããŸãã
a = 3776
print('Mt.Fuji', a)
æã¡èŸŒã¿ãããããã«è±èªã«ããŠããŸãããæååã«ã¯æ¥æ¬èªã䜿çšå¯èœã§ãã
èšå· +
-
*
/
ãçšããŠååæŒç®ãå¯èœã§ãã
a = 100 + 200
print(a)
a = 1000 - 1
print(a)
a = 60 * 60
print(a)
a = 300 / 60
print(a)
å³åŽã«å€æ°ãçšããããšãå¯èœã§ãã
a = 5
b = a * 60
print(b)
å·ŠåŽãšå³åŽã§åãå€æ°ã䜿çšããããšãå¯èœã§ãã
a = 100
a = a + 1
print(a)
a = a + 1
㯠a
ã®å€ã 1
å¢ããããšããæå³ã«ãªããŸãã
ãã®ãããªåœ¢ã§æ°å€ãïŒå¢ããããšãããã°ã©ãã³ã°çšèªã§ãã€ã³ã¯ãªã¡ã³ãããšåŒãã§ããŸãã
int
ã䜿ããšãæŽæ°å€ãã«å€æããããšãã§ããŸãã
以äžã¯ãå°æ°å€ãããæŽæ°å€ãã«å€æããäŸã§ãã
pi = 3.14
print(pi)
pi = int(pi)
print(pi)
å ã®å€ããæååãã§ãã£ããšããŠãåè§æ°åã§ãããªãã°ãæŽæ°å€ãã«å€æãããŸãã
fuji = '3776'
print(fuji)
fuji = int(fuji)
print(fuji)
åºååŸã¯ã©ã¡ããå€ãããŸããããå€æ°fuji
ãšããŠã¯åè
ããæååãã§åŸè
ã¯ãæ°å€ããšããéãããããŸããprint
ã®å Žåã¯ãæååããå°æ°å€ããæŽæ°å€ãã®ã©ãã§ãåé¡ãªãã®ã§ããããæŽæ°å€ãã§ãªããšãšã©ãŒã«ãªãå Žé¢ãããããããã®ãããªãšãã«int
ã掻çšããŸãã
ããŒããŒãããå
¥åãããããŒã¿ãæååãšããŠååŸã§ããŸãã
ã«ãã³å
ã«ã¯è¡šç€ºããããæååãæžããŸãã
num = input('number : ')
print(num)
èšå·:
ã¯èŠãããã®ããã«å
¥ããŠããæååã§ãããpythonã®ææ³ãšã¯é¢ä¿ãããŸããã
input
ã§åŸãããå€ã¯æ°å€ã§ã¯ãªãæååã§ããããšããã€ã³ãã§ããäŸãã°ã以äžã®ãããªã³ãŒãã¯ãšã©ãŒãåºãŸãã
#ãšã©ãŒã«ãªã
num = input('number : ')
num = num * 2
print(num)
äŸãã°10
ãå
¥åãããš1010
ãšããçµæã«ãªããŸããæãç®ãšã¯ç°ãªãã10
ãšããæååã2åç¹°ãè¿ããšããåŠçã«ãªã£ãããã§ãã
察çäŸãšããŠint
ã䜿ãææ³ããããŸãã
num = input('number : ')
num = int(num) * 2
print(num)
int(æ°å€)
ãšæžãããšã§æååããæ°å€ã«å€æãããŸãã
ãã®åŸã«äœ¿ãwhile
ãif
ã«ãããŠã€ã³ãã³ãïŒåäžãïŒãšãããã®ã䜿ããŸãã
éåžžã¯åè§ã¹ããŒã¹ããŒ2ã€ä»¥äžã§è¡šããŸãããTabã§ä»£çšããããšãå¯èœã§ãã
ã³ãŒããšãã£ã¿ã«ãã£ãŠã€ã³ãã³ãã®åæèšå®ãç°ãªããŸãã
Geanyã§ã¯ãTabãã䜿ãããŠããŸãããæšæºçã«äœ¿ããããåè§ã¹ããŒã¹4ã€ãã«å€æŽããŸãã
ãã ããããŒããŒãã§æã¡èŸŒããšãã¯ãåè§ã¹ããŒã¹4ã€ããããããæã¡èŸŒãŸãªããŠã倧äžå€«ã§ããGeanyåŽã§èªåçã«æ¿å
¥ããŠãããããTab
ããŒã䜿ãããšã§ãåè§ã¹ããŒã¹4ã€ãããŸãšããŠæ¿å
¥ãããŸãã
ã¡ãã¥ãŒãã ç·šé
> èšå®
ãéžã³ãŸãã
ãšãã£ã¿
> ã€ã³ãã³ã
ã«ãŠã圢åŒããã¿ã
ãã空çœ
ã«å€æŽããŠOK
ãæŒããŸãã
èšå®å€æŽãåæ ãããããã«äžæŠããå°ãã¯ãªãã¯ããŠéããŸãã
åã³ãã¡ã€ã«ãéããŠãã©ããã§Tab
ããŒãæŒããŠãã ããã
äžèšã®GIFã¢ãã¡ã®ããã«ã«ãŒãœã«ã§ç¢ºèªããŠãã¹ããŒã¹4ã€åãåºæ¥ãŠããã°æåã§ãã
ããã4ã€ã®ã¹ããŒã¹ã«ãªã£ãŠãªãå Žåã¯ãæ°èŠã§ãã¡ã€ã«ãäœæããŠã¿ãŠäžããã
匷å¶çµäºããããŸã§ç¡éã«ããã°ã©ã ãç¹°ãè¿ãããšãåºæ¥ãŸããwhile True:
ã®äžã«ç¹°ãè¿ãããåŠçãããã¯ãã€ã³ãã³ãããŠèšè¿°ããŸãã
while True:
a = input('input : ')
print(a)
ç¡éã«ã«ãŒããããããã匷å¶çµäºãããšãã¯Ctrl
+ C
ããŒãæŒããŸãã
次ã«printã5ã€è¿œå ã2ã€ã ãã€ã³ãã³ãã3ã€ã¯ã€ã³ãã³ããªãã«ããŠã¿ãŸããã
while True:
a = input('input : ')
print(a)
print('hello1')
print('hello2')
print('hello3')
print('hello4')
print('hello5')
ã©ã®ãããªå®è¡çµæã«ãªã£ãã確ãããŠã¿ãŠäžããã
ãŸããã€ã³ãã³ããå€ãããšã©ããªããè²ã
æ¹é ããŠã¿ãŠäžããã
åŠçãããã¯ãé·ãå Žåãªã©ããŸãšããŠã€ã³ãã³ãå¯èœã§ãã
ã€ã³ãã³ããå¢ããããå Žåã¯ããããã¯ãéžæããåŸã«Tab
ããŒãæŒãããšã§äžæ°ã«ã€ã³ãã³ããããŸãã
éã«ã€ã³ãã³ããæžããããå Žåã¯ããããã¯éžæåŸ Shift
+ Tab
ããŒãæŒããŠãã ããã
å€æ°ã®å€ã«å¿ããŠåŠçãå€ãããå Žåã«ãæ¡ä»¶åå²ããè¡ããŸãã
Pythonã®ããŒã¿åã«ã€ããŠãæŽæ°ãå°æ°ãæååãæ±ã£ãŠããŸãããããã以å€ã«ããŒã«åãšãããã®ããããŸããããã¯True
ãããã¯False
ã®ã©ã¡ããã®å€ã®ã¿æã€åã§ããäž»ã«æ¡ä»¶åå²ãšçµåããŠäœ¿çšããŸãã
working = True
if working:
print('Busy')
if å€æ°:
ãšæžãããšã§ããå€æ°ããTrueã®ãšãã«ã€ã³ãã³ãããããããã¯ã®åŠçãè¡ããŸããä»åã¯True
ãªã®ã§Busy
ãšãã衚瀺ã«ãªããŸããã
ã§ã¯å€æ°ãFalse
ã«ããå ŽåãèŠãŠã¿ãŸãã
working = False
if working:
print('Busy')
ä»åã¯False
ãªã®ã§äœãåŠçãããã«ããã°ã©ã ãçµäºããŸãããFalse
ã®ãšãã«äœãåŠçãããå Žåã¯else:
ãè¿œå ããŸãã
working = False
if working:
print('Busy')
else:
print('Not Busy')
ããã§True
ã®å Žåã¯Busy
ãFalse
ã®å Žåã¯Not Busy
ãšè¡šç€ºãããŸãã
ä»åºŠã¯å€æ°ã«ããŒã«å以å€ã䜿ãå Žåã®äŸã§ããå
·äœçã«ã¯ if æ¡ä»¶åŒ:
ãšèšè¿°ããŸãã
ä»åã¯input
ã掻çšããŸããã以äžã®ã³ãŒããå®è¡ããŠyes
ãå
¥åãããããã以å€ã®æååãå
¥åããŠã¿ãŠãã ããã
answer = input('Do you like Python? ')
if answer == 'yes':
print('Good!')
==
ã¯å·Šã®å€æ°ãšå³ã®å€ãæ¯èŒããŠåããªãã°True
ããã以å€ã¯False
ãšåŠçããŸãã
æ¡ä»¶åŒãTrue
ã®ãšãã«åŠçãããã¯ãå®è¡ããæ¡ä»¶åŒãFalse
ã®ãšãã¯åŠçãããã¯ã¯ç¡èŠãããŸãã
åæ§ã«else:
ã䜿ããŸãã
answer = input('Do you like Python? ')
if answer == 'yes':
print('Good!')
else:
print('Too bad')
æ¡ä»¶ãããã«åãããå Žåã¯elif æ¡ä»¶åŒ:
ã䜿ããŸãã
answer = input('Do you like Python? ')
if answer == 'yes':
print('Good!')
elif answer == 'no':
print('Too bad')
else:
print('What?')
yes
ã®ãšãã¯Good!
ãno
ã®ãšãã¯Too bad
ããã以å€ã®ãšãã¯What?
ãšè¡šç€ºãããŸãã
and
ã䜿ãããšã§è€æ°ã®æ¡ä»¶ãç°¡åã«æ±ãããšãã§ããŸãã
answer1 = input('Do you like Python? ')
answer2 = input('Do you like Raspberry Pi? ')
if answer1 == 'yes' and answer2 == 'yes':
print('Great!!')
ã©ã¡ãã®å
¥åãyes
ã ã£ãå Žåã®ã¿Great!!
ã衚瀺ãããŸãã
ifãããã¯ã®äžã«ifãæžãããšã§ãå®çŸå¯èœã§ãããand
ã䜿ã£ãæ¹ãã·ã³ãã«ã§ãã
while
ã¯while True:
ã ãã§ã¯ãªããifã®ããã«æ¡ä»¶åŒãæ±ããŸãã
isExecution = True
while isExecution:
answer = input('Do you like Python? ')
if answer == 'yes':
print('Good!')
isExecution = False
else:
print('Too bad')
print('end')
yes
ãšå
¥åããããŸã§ããã°ã©ã ã¯ç¡éã«ç¶ããŸããisExecution
ã¯æ®éã®å€æ°ã§ããå€æ°ã¯ãã®ããã«å°æåãšå€§æåãæ··ããŠäœ¿ã£ãŠãåé¡ãããŸãããyes
ãå
¥åããããšãã ãisExecution
ãFalse
ã«ãªããŸãããããåããŠæ¬¡ã®ã«ãŒãéå§æã«ã¯while False:
ãšåçã«ãªããããã«ãŒãåŠçããæããŠend
ã衚瀺ããŠããã°ã©ã ãçµäºããŠããŸãã
ã«ãŒããå§ãŸãåã«isExecution
ãTrue
ã§åæåããŠããã®ããã€ã³ãã§ãã
ããã°ã©ã äžã«ãšã©ãŒãçºçãããšãšã©ãŒãåºãŠåŒ·å¶çµäºããŸãã
äŸãã°ã以äžã®ã³ãŒãã¯æ°å€ãå
¥åãããšïŒåã®å€ãåºåãã0ãå
¥åããããšçµäºããäŸã§ããåè§æ°å€ãå
¥åããŠãããšãã¯åé¡ãªãã§ãããæååãå
¥åããŠã¿ãŠãã ããã
isExecution = True
while isExecution:
num = input('input number : ')
num = int(num) * 2
print(num)
if num == 0:
isExecution = False
print('end')
ValueError: invalid literal for int() with base 10:
ãªã©ãšãããšã©ãŒãåºãŠåŒ·å¶çµäºããŠããŸããŸãã
ãšã©ãŒããã£ãå Žåãéäžã§åŒ·å¶çµäºããããšã©ãŒã®ç®æã¯ã¹ãããããŠæåŸãŸã§é²ãããå ŽåããããŸãããã®ãããªãšãã«try except
ã掻çšããŸãã
以äžã®ã³ãŒãã®å Žåã¯æååãå
¥åãããšã©ãŒã«ãªããšãéäžã§åŒ·å¶çµäºããError
ãšãã衚瀺ãè¡ããend
ã衚瀺ããŠããã°ã©ã ãçµäºããŸãã
try:
isExecution = True
while isExecution:
num = input('input number : ')
num = int(num) * 2
print(num)
if num == 0:
isExecution = False
except:
print('Error')
print('end')
ããã°ã©ã äžã«ãšã©ãŒããã£ãå Žåã匷å¶çµäºãããäŸå€çãªåŠçãšããŠexcept
ã®ãããã¯ã«åŠçã移ããŸãã
ãã®ãŸãŸã ãšãšã©ãŒå 容ãåãããŸããã®ã§ããšã©ãŒå 容ã衚瀺ããããã«ããŸãã
try:
isExecution = True
while isExecution:
num = input('input number : ')
num = int(num) * 2
print(num)
if num == 0:
isExecution = False
except Exception as e:
print(e)
print('end')
å€æŽç®æã¯except
ã®ãšããã®ïŒè¡ã§ãã
ãã®ããã«èšè¿°ããããšã§ãšã©ãŒå
容ãåºåãããããšãã§ããŸãã
ä»ãŸã§ã¯ãçµã¿èŸŒã¿é¢æ°ããšåŒã°ããPythonã³ãŒãã ããæ±ã£ãŠããŸããã
ããã§ã¯ããã°ã©ã ãéäžã§äžæåæ¢ããã³ãŒããæ±ããŸããtime
ãšããã¢ãžã¥ãŒã«ã®äžã«å
¥ã£ãŠããã³ãŒãã䜿ããŸããããçµã¿èŸŒã¿é¢æ°ãã«ãªãã³ãŒããæ±ãå Žåã¯ãæåã«ã¢ãžã¥ãŒã«ãåã蟌ãå¿
èŠããããŸãã
import time
ããã ãå®è¡ããŠãäœã衚瀺ãããŸãããããã®èšè¿°ã®åŸã«time.xxxã®ãããªæžãæ¹ã§åã³ãŒããæ±ãããšãå¯èœãšãªããŸãã
以äžã®ã³ãŒããå®è¡ããŠã¿ãŠäžããã
import time
print('hello')
time.sleep(3)
print('hello')
hello
ãšè¡šç€ºããŠãã次ã®hello
ãåºããŸã§ã®ïŒç§éã¯ããã°ã©ã ãäžæåæ¢ããŠããŸãã
time.sleep(sec)
ãšæžãããšã§sec
ç§éãããã°ã©ã ãäžæåæ¢ã§ããŸããäž»ã«ã«ãŒãã®äžã§äœ¿ããŸããå°æ°å€ãèšå®ããããšãå¯èœã§ãã
import time
while True:
print('hello')
time.sleep(0.5)
ããã°ã©ã çµäºã«ã¯åŒ·å¶çµäºãå¿ èŠã§ãã
ãªã¹ãã¯ä»ã®ããã°ã©ãã³ã°ã§ã¯é
åãšåŒã°ããŠããŸãã
ç°¡åã«èšããšãäžã€ã®å€ã§ã¯ãªããäžé£ã®å€ãæ ŒçŽã§ããå€æ°ã§ãã
åèŠçŽ ãã«ã³ãã§åºåããã«ãã³[ ]
å
ã«èšè¿°ããŸãã
numbers = [10, 20, 30, 40, 50]
print(numbers)
ãã®ãŸãŸprintãããšãªã¹ãã®åœ¢ã®ãŸãŸè¡šç€ºãããŸãã
Pythonã§ã¯ã€ã³ããã¯ã¹ïŒèŠçŽ ã®çªå·ïŒã¯0
ããå§ãŸããŸããäŸãã°ã€ã³ããã¯ã¹2
ã®å Žåã¯3çªç®ã®èŠçŽ ãã¢ã¯ã»ã¹ããããšã«ãªããŸãã
numbers = [10, 20, 30, 40, 50]
print(numbers[2])
å€ãå šéšåãåºãã«ã¯ä»¥äžã®ããã«æžããŸãã
numbers = [10, 20, 30, 40, 50]
print(numbers[0])
print(numbers[1])
print(numbers[2])
print(numbers[3])
print(numbers[4])
èŠçŽ æ°ãå€ãå Žåãäžèšã®ãããªæžãæ¹ã ãšéåžžã«é·ããªã£ãŠããŸããŸãããŸãèŠçŽ æ°ãå€æŽãã床ã«ä¿®æ£ãå¿
èŠã§ããããã§ãfor in
ã䜿ãããšã§ç°¡åã«æ±ãããšãåºæ¥ãŸãã
while
ã䜿ã£ãç¹°ãè¿ã以å€ã«ãfor in
ã䜿ã£ãç¹°ãè¿ãããããŸããfor in
ã¯äž»ã«ãªã¹ããšäžç·ã«ãã䜿ãããŸãã
numbers = [10, 20, 30, 40, 50]
for n in numbers:
print(n)
for in
ã¯ãªã¹ãããé ã«å€ãåãåºããŠå€æ°ã«ä»£å
¥ãããããã¯éšã®åŠçãç¹°ãè¿ããŸãã
å
·äœçã«for n in numbers:
ã§ã¯numbers
ããèŠçŽ ãïŒã€åãåºãn
ã«ä»£å
¥ããããã¯ã®print(n)
ãå®è¡ãããšããããšãç¹°ãè¿ããnumbers
ããåãåºãèŠçŽ ããªããªã£ããçµäºãããšããæå³ã§ãã
ã¿ãã«ã¯ãç°¡åã«ãããšèŠçŽ ãå€æŽã§ããªããªã¹ãã§ãããªã¹ãã¯åŸããèŠçŽ ãå€æŽã§ããŸãããã¿ãã«ã¯ã§ããŸããã
åæåæã®ã«ãã³ã®åœ¢ã()
ã§ãªã¹ããšç°ãªããŸãããåç
§æã®ã«ãã³ã¯ãªã¹ããšåã[]
ã䜿ããŸãã
äŸãã°ãè²æ
å ±ïŒéã»ç·ã»èµ€ïŒãç»åã®å€§ããïŒå¹
ã»é«ãïŒãæå®ãããšãã«äœ¿ãããŸãã
color = (255, 255, 255)
print(color)
size = (1920, 1080)
print(size[0])
é¢æ°ãšã¯ããã£ããèšããšãå€ãæž¡ããšåŠçãè¡ãçµæãå€ã§è¿ããã®ã§ãã
æž¡ãå€ã®ããšããåŒæ°ïŒã²ãããïŒããè¿ã£ãŠããçµæã®å€ããæ»ãå€ïŒãã©ãã¡ïŒããšèšããŸãã
ãã ããåŒæ°ãç¡ãé¢æ°ãæ»ããç¡ãé¢æ°ãåŒæ°ãè€æ°ãããé¢æ°ãæ»ãå€ãè€æ°ããé¢æ°ãªã©ãæ§ã
ãªåœ¢åŒãååšããŸãã
å®ã¯æ¢ã«äœ¿çšããŠããint
ãprint
ã¯é¢æ°ã§ããããã€ãã®é¢æ°ã®åœ¢åŒãèŠãŠã¿ãŸãããã
#æ»ãå€ïŒåãåŒæ°ïŒå
pi = int(3.14)
#æ»ãå€ãªããåŒæ°ïŒå
print('hello')
#æ»ãå€ãªããåŒæ°ïŒå
print('Hello', 'Python', 'Programming')
以äžã®äŸã¯æ»ãå€ãè€æ°ããäŸã§ããtime.gmtime
ã¯ç§åäœã§æžãããUNIXæéãã幎ææ¥æåç§ãªã©ã«å€æããé¢æ°ã§ããããèªäœã¯ä»åã®ã³ã³ãã³ãã§ã¯äœ¿çšããŸãããæ»ãå€ãè€æ°è¿ãé¢æ°ãååšãããšããããšãé ã«å
¥ããŠäžããã
#æ»ãå€ïŒåãåŒæ°ïŒå
import time
year, mon, day, hour, minu, sec, wday, yday, isdst = time.gmtime(1617466516)
以äžã®ããã«ãåŒæ°ã«åå=å€
ã®åœ¢åŒã§æå®ããåŒæ°ããããããŒã¯ãŒãåŒæ°ãšåŒã°ããŠããŸããsep=
ã§åŒæ°ãæå®ããããšã§ãåºåãæåãåè§ã¹ããŒã¹ããåŒæ°ã§æå®ããæååã«å€ããããšãã§ããŸããããèªäœã¯ä»åã®ã³ã³ãã³ãã§ã¯äœ¿çšããŸãããããŒã¯ãŒãåŒæ°ãšèšãæžãæ¹ããããšããããšãé ã«å
¥ããŠäžããã
#æ»ãå€ãªããåŒæ°ïŒå + ããŒã¯ãŒãåŒæ°ïŒå
print('Hello', 'Python', 'Programming', sep='---')
以äžãPythonåºç€ããã°ã©ãã³ã°ã§ããã
ãã¬ããããŒãã¯ãã³ãä»ãäžèŠã§åè·¯ãçµãããšãã§ãã䟿å©ãªããŒãã§ãã
ããŒãã«è€æ°ã®ç©ŽãéããŠãããããã«é»åéšåããžã£ã³ããŒç·ãæ¿ããŠåè·¯ãçµã¿ãŸãã
å称ã®ç±æ¥ã¯ãã³ãåãæã«ãã³ãã®ããæ¿ãbreadboardãã§ãã
é·ææ¹åã«å¯ŸããŠãåçŽã®åïŒç©Žã¯èŠããªããšããã§é»æ°çã«ç¹ãã£ãŠããŸãã
é»æ°çã«ç¹ãã£ãŠããããšããå°éããŠããããšãããŸãã
äžèšã®ç»åã§ãåããã³ã¯è²ã®é·æ¹åœ¢ã®äžã«ããç©Žå士ã¯å°éããŠããŸãããã以å€ã¯äžåå°éããŠããŸããã
ãªããä»åã®ãã¬ããããŒãã¯ç·å¯Ÿç§°ã§ããããã180°å転ããŠãåãæ§é ã§ãã
ãžã£ã³ããŒç·ã¯ãã¬ããããŒããã©ãºãã€ã«æ¿ããŠå°éãããããšãã§ããã¯ã€ã€ãŒã§ãã
ä»å䜿çšãããžã£ã³ããŒç·ã¯ããªã¹âã¡ã¹ãã¿ã€ãã§ãããä»ã«ãããªã¹ïŒãªã¹ããã¡ã¹âã¡ã¹ããååšããŸãã
ãªã¹åŽããã¬ããããŒããã¡ã¹åŽãã©ãºãã€ã«æ¿ããŠäœ¿ããŸãã
çœä»¥å€ã«ãæ§ã ãªè²ã®ãžã£ã³ããŒç·ããããŸãããæ©èœã¯æ§èœã«ã¯äžåé¢ä¿ãããŸãããäœã®ä¿¡å·ããã±ã£ãšèŠãŠåããããã«ããããã«è²ã®äœ¿ãåããããå ŽåããããŸããä»åã¯çœäžè²ãã䜿ããªããããç¹ã«æ°ã«ããå¿ èŠã¯ãããŸããã
ä»åã¯15cmã®é·ãã§ããããžã£ã³ããŒç·ã®é·ãã¯äœçš®é¡ããããŸããã¡ãªã¿ã«ä»åã®ããªã¹âã¡ã¹ãã¿ã€ãã§ããã°ããžã£ã³ããŒç·ã«ãžã£ã³ããŒç·ãé£çµãããŠå»¶é·ãé·ãããããšãå¯èœã§ãã
ã§ã¯ãã©ãºãã€ãšãã¬ããããŒãã«ãžã£ã³ããŒç·ãçµç·ããŠãã ããã
ã©ãºãã€åŽã¯äžã®ç»åãšåãåãã«ããæã«ãé»ããã³äžåŽã®å·ŠäžããïŒçªç®ãšïŒçªç®ã«ç¹ããŸãã
ãã¬ããããŒãåŽã¯äžå€®ã®äœçœ®ã«æ¿ããŠããŸãããå°éããåãåã«çœ®ããªããã°å¥ã®äœçœ®ã§ãæ§ããŸããã
å®éã®åçã ãšãããªæãã«ãªããŸãã
ãã®çµç·ã¯é»æ°çã«ç¹ããããšã¯ãªãããããã®æç¹ã§ã¯åè·¯ãšããŠã®æå³ã¯ãããŸããã
ã©ãºãã€ã®äžç«¯ã®é»ã端åéšãGPIO
ãšããæ©èœããã£ããã³ã®éãŸãã§ããGPIO
ã¯General Purpose Input/Outputã®ç¥ã§ãæ¥æ¬èªã ãšãæ±çšå
¥åºåããšããèšèã§ãã
ããã«ã»ã³ãµãŒãLEDãã¢ãŒã¿ãŒãªã©ãç¹ãã§é»ååè·¯ãçµãã§ãããŸããGPIOããœãããŠã§ã¢ããé»æ°çã«å¶åŸ¡ããããšã§æ§ã
ãªé»åå¶åŸ¡ãå¯èœãšãªããŸããGPIO
ã¯ããããã端åçªå·ããšãGPIOçªå·ããšããïŒçš®é¡ã®çªå·ãæã£ãŠãããããééããªãããã«æ³šæããŸãããã
å®ã¯ãã®é»ã端åéšã«ã¯ãGPIO
ã ãã§ãªãããã®ä»ïŒçš®é¡ã®ç«¯åãååšããŸãã
å ·äœçã«ã¯ãã¡ãã®ç»åãã確èªã§ããŸãã
ç»ååŒçšå ïŒraspberrypi.org
å°ããªé»äžžã®äžã«æžããŠãã1ïœ40ã®çªå·ãã端åçªå·ãã§ãã
ãã®ç«¯åãGPIOã®å Žåã¯GPIO 17
ã®ããã«æšªã«çªå·ãæžããŠãããŸããããããGPIOçªå·ãã§ããGPIO 2(SDA)
ã®ããã«ã«ãã³()
ãšæååãæžããŠãã端åããããŸãããããã¯GPIO以å€ã®çšéãšããŠã䜿ããä¿¡å·ç·ïŒäŸãã°ã·ãªã¢ã«ä¿¡å·ãªã©ïŒã§ãã
5V power
ã¯5Vé»æºãšããæå³ã§ããäŸãã°å°ããªãµãŒãã¢ãŒã¿ãŒã®é»æºäŸçµŠã«äœ¿ããŸããåæ§ã«3V3 power
ã¯3.3Vé»æºãšããæå³ã§ããäŸãã°ICã®é»æºäŸçµŠã«äœ¿ããŸããGround
ã¯é»å§0Vã®åºæºãšãªã端åã§ãé»æ± ã§ãããšããã®ãã€ãã¹ç«¯åã«ããããŸããGND
ãšãèšãããããšããããŸãã
é»æºãGNDã¯æµããé»æµã«äžéããããŸãã®ã§æ³šæãå¿
èŠã§ãã
ä»åã®ã³ã³ãã³ãã§æ±ãã®ã¯éåžžã®ãGPIOä¿¡å·ããšãGNDãã®ã¿ã§ãã
ã©ãºãã€ã®ç«¯åã¯æèšããå¿
èŠã¯ãããŸãããäžèšã®ãµã€ãã«ã¢ã¯ã»ã¹ããŠç¢ºèªããã°OKã§ããããã£ãšçŽ æ©ã確èªããæ¹æ³ããããŸãã
Raspberry Pi OSã«ã¯ãã³é
ã確èªããã®ã«éåžžã«äŸ¿å©ãã³ãã³ããæåããå
¥ã£ãŠããŸããã©ãºãã€ã®ã¿ãŒããã«ãç«ã¡äžããŠä»¥äžã®ã³ãã³ããæã¡èŸŒãã§ã¿ãŠäžããã
pinout
ãã®ãããªå³ã衚瀺ãããããšæããŸãã
äžã®ç»åã§1
ãšæ°Žè²ã§è¡šç€ºãããŠããã®ã¯ç«¯åçªå·ã1çªã§ããããšã瀺ããŠããŸãã
以äžã®ããã«è²åããããŠããã®ã§åããããããšæããŸãã
éåžžã«ã·ã³ãã«ã§ææ©ã確èªã§ããŸãã®ã§ããã²æŽ»çšããŠãã ããã
ä»åã¯ã©ãºãã€ã®GND
ãšGPIO18
ã«ãžã£ã³ããŒç·ãæ¥ç¶ããŸããã
次ã®ãã£ãã¿ãŒã§ã¯ããã°ã©ã ã§GPIOãå¶åŸ¡ããŸãã
ã©ãºãã€ã§GPIOå¶åŸ¡ãè¡ãæ¹æ³ã¯ããã€ããããŸãããä»åã¯Pythonã§ç°¡åã«æ±ããRPi.GPIO
ãšããã¢ãžã¥ãŒã«ã掻çšããŸãã
RPi.GPIO
ã¯Raspberry Pi OSã«æåããå
¥ã£ãŠãããimportããã ãã§äœ¿ããŸãã
import RPi.GPIO as GPIO
äžèšã¯import A as B
ãšããæžãæ¹ã§ãã
ããã¯Aãšããã¢ãžã¥ãŒã«ãimportããŠããã®åŸã¯Bãšãã圢ã§äœ¿ããšããæå³ã§ããäŸãã°RPi.GPIO.input(18)
ãšæžãã¹ããšãããGPIO.input(18)
ãšçã圢ã§æžãããšãã§ããŸãã
GPIOã®ã¢ãŒããsetmode
ã䜿ã£ãŠèšå®ããŸãã
åã®ãã£ãã¿ãŒã§ç¢ºèªããéããGPIOã«ã¯ã端åçªå·ããšãGPIOçªå·ãã®ïŒçš®é¡ãååšããŸãããããã°ã©ã äžã§ã端åçªå·ãã䜿ãå Žåã¯GPIO.BOARD
ãããGPIOçªå·ãã䜿ãå Žåã¯GPIO.BCM
ãæå®ããŸãã
ä»åã¯ãGPIOçªå·ãã䜿ããããGPIO.BCM
ãæå®ããŸãã
import RPi.GPIO as GPIO
#GPIOåæèšå®
GPIO.setmode(GPIO.BCM)
å®è¡ããŠãäœãèµ·ããŸããã
GPIOã¯å
¥å(IN)ãšããŠäœ¿çšããããåºå(OUT)ãšããŠäœ¿çšããããæåã«æ±ºå®ããå¿
èŠããããŸãã
LEDãªã©ãç¹ç¯ãããå Žåã¯GPIO.OUT
ã䜿ããŸãããä»åã¯ã»ã³ãµãŒå
¥åãæ±ãããGPIO.IN
ãæå®ããŸãã
ããã«å
¥åã®å Žåã¯ãå
èµãã«ã¢ããèšå®ããå
èµãã«ããŠã³èšå®ããèšå®ãªããã®ã©ãããæå®ããå¿
èŠããããŸãã
å
¥å端åã«äœãå
¥åããªããšãã¯äžå®å®ãªç¶æ
ã«ãªããŸãã®ã§ãå
¥å端åã«ã¯å¿
ããã«ã¢ããããã«ããŠã³ãšåŒã°ããåŠçãè¡ãã®ãéåã§ãããã®åŠçãè¡ãããšã§é»æ°çã«äžå®å®ãªç¶æ
ããªããªããŸããå€ä»ãã®åè·¯ã§ãã«ã¢ããïŒãã«ããŠã³åŠçãè¡ãå Žåã¯ãèšå®ãªããéžã³ãŸãã
ä»åã¯å€ä»ãã®åè·¯ã§ã¯åŠçã¯è¡ããªããããå
èµèšå®ãå¿
èŠã§ãã
ãã«ã¢ãããªã®ããã«ããŠã³ãªã®ãã¯å
¥åä¿¡å·ã®ç¶æ
ã«ãã£ãŠæ±ºãŸããŸãããGND
å°éããšãå°éãªãããæ³å®ãããåè·¯ã®å Žåã¯ãã«ã¢ãããã3V3
å°éããšãå°éãªãããæ³å®ãããåè·¯ã®å Žåã¯ãã«ããŠã³ã§ãã
ä»åã¯åè
ã§ãããããã«ã¢ããèšå®ã§ãã
ããããèžãŸãããšä»¥äžã®ããã«ãªããŸãã
import RPi.GPIO as GPIO
#GPIOåæèšå®
GPIO.setmode(GPIO.BCM)
#GPIO18pinãå
¥åã»ãã«ã¢ããèšå®
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
å®è¡ããŠãäœãèµ·ããŸããã
ãããŸã§ã¯GPIOã䜿ãéã¯ãå¿
ãæžããã³ãã¬ãŒãã ãšæã£ãŠãã ããã
ãã«ã¢ããïŒãã«ããŠã³åŠçã«ã€ããŠåããŠèãæ¹ã¯ç解ãé£ããã£ãããšæããŸããä»åã¯ãã®åŠçãå¿
èŠã§ãããšããããšã ããæããŠäžãã
å®éã«GPIO18ã®ä¿¡å·ãèªã¿åã£ãŠè¡šç€ºããŸãã
import RPi.GPIO as GPIO
import time
#GPIOåæèšå®
GPIO.setmode(GPIO.BCM)
#GPIO18pinãå
¥åã»ãã«ã¢ããèšå®
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#ã¡ã€ã³ã«ãŒã
while True:
value = GPIO.input(18)
print(value)
time.sleep(0.03)
time.sleep
ã§å°ãã ãäžæåæ¢ãè¡ã£ãŠããçç±ã¯ãããã£ã¿ãªã³ã°ããšåŒã°ãããã€ãºã®å¯Ÿçã®ããã§ãããã£ã¿ãªã³ã°ãšã¯ç©ççãªæŒããã¿ã³ãªã©ã§ããããçŸè±¡ã§ãïŒåæŒããã ããªã®ã«æ¯åã®èª€å·®ã«ããè€æ°åæŒãããããšã«ãªã£ãŠããç¶æ
ã§ããå°ãåŸ
ã€ããšã«ããå®å®ããå
¥åãåŸãããŸãã
å®è¡ãããš1
ããã£ãšè¡šç€ºãããŸãã
ã§ã¯ãå®è¡ãããŸãŸãäžæ¹ã®ãžã£ã³ããŒç·ãæããŠãä»æ¹ã®ãžã£ã³ããŒç·ãšåãåã«æ¿å ¥ããããæãããããŠã¿ãŠäžããã 以äžã®çµç·å³ã¯åãåã«æ¿ããŠããæ§åã§ãã
åçã ãšãããªæãã§ãã
åãåã«æ¿ãããšããã€ãŸãGPIO18
ãšGND
ãå°éãããšãã¯0
ãå°éããŠããªããšãã¯1
ã«ãªãããšã確èªã§ããããšæããŸããå
¥åãGND
ã®ãšãã¯0
ã§ãå
¥åãæªå
¥åã®ãšãã¯ãã«ã¢ããèšå®ãå¹ããŠ1
ã«ãªããŸãã
è©Šãã«ä»¥äžã®ããã«ãã«ã¢ããèšå®ãç¡ãã«ããŠã¿ããšãäžå®å®ãªç¶æ ãšã¯ã©ããªæããªã®ããäœéšããããšãåºæ¥ãŸãã
#GPIO18pinãå
¥åã»ãã«ã¢ããèšå®
GPIO.setup(18, GPIO.IN) #ãã«ã¢ããïŒãã«ããŠã³èšå®ãªã
ãã¢ã»ã³ãµãŒã®å
éšã«ã¯äžã®åçã®ãããªããªãŒãã¹ã€ããããšåŒã°ããéšåãå
¥ã£ãŠããŸãã
ç£ç³ãè¿ãã«ãããšã¹ã€ããONãé¢ãããšã¹ã€ããOFFããŸãããªããã»ã³ãµãŒãåäœãããããã®é»æºã¯äžèŠã§ãã
ç»ååŒçšå ïŒç§æé»åéå
ä»åã®ãã¢ã»ã³ãµãŒã§ã¯ãç·ãã€ããŠããæ¹ã«ãªãŒãã¹ã€ãããç·ãä»ããŠãªãæ¹ã«ç£ç³ãå ¥ã£ãŠããŸããäž¡è ãè¿ã¥ãããšïŒæ¬ã®çœãä¿¡å·ç·ãå°éãé¢ãããšéå°éã®ç¶æ ã«ãªãä»çµã¿ã§ãã
ãã¢ã»ã³ãµãŒã䜿ãã°ãååã®ãã£ãã¿ãŒã§è¡ã£ããžã£ã³ããŒç·ã®æ¿ãïŒæãããç£ç³ãè¿ã¥ããïŒé ããããšããã¢ã¯ã·ã§ã³ã§ä»£çšã§ãããšããããšã§ãã
ãã¢ã»ã³ãµãŒã®ç·ã¯çŽæ¥ã©ãºãã€ã«æ¿ãããšããã¬ããããŒãã«æ¿ãããšãã§ããŸãããããã§ãã¿ãŒããã«ãããã¯ãçµç±ããŠæ¥ç¶ããŸãã
ãã®ç»åã®ããã«çµç·ããŸãããŸããã¿ãŒããã«ãããã¯ã®ïŒæ¬ã®è¶³ããžã£ã³ããŒç·ãšåãåã«ãªãããã«ãã¬ããããŒãã«æ¿ããŠãã ãããã¿ãŒããã«ãããã¯ã¯ç©Žã空ããŠããæ¹ãå€åŽã®åãã§ãã
ã¿ãŒããã«ãããã¯ã«ãã¢ã»ã³ãµãŒã®ç·ãç¹ãã«ã¯ã以äžã®åçã®ããã«ãã©ã¹ãã©ã€ããŒã䜿ã£ãŠç· ããå¿
èŠããããŸããç©Žã«ç·ã®ç«¯åéšãå
¥ããŠãããžãç· ããããšã«ããæãã§åºå®ããŸãã
ãã¢ã»ã³ãµãŒã®ïŒæ¬ã®ç·ã«æ¥µæ§ïŒãã©ã¹ããã€ãã¹ãªã©ã®åºå¥ïŒã¯ãããŸããã®ã§ãã©ã£ã¡ãã©ã£ã¡ã«ç¹ãã§ãåé¡ãããŸããã
ãããåè·¯ã®å®æåçã§ãã
ã§ã¯ååã®ãã£ãã¿ãŒãšåãã³ãŒããå®è¡ããŸãããã
import RPi.GPIO as GPIO
import time
#GPIOåæèšå®
GPIO.setmode(GPIO.BCM)
#GPIO18pinãå
¥åã»ãã«ã¢ããèšå®
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#ã¡ã€ã³ã«ãŒã
while True:
value = GPIO.input(18)
print(value)
time.sleep(0.03)
ç£ç³ãè¿ã¥ããããé ããããããŠã¿ãŠäžããã
è¿ã¥ãããšãã®è·é¢ã¯ä»¥äžã®åçãããã®è·é¢ã§ååã§ãã
ã¿ãŒããã«ç»é¢ã«ãè¿ã¥ããŠãããšãã¯0
ãé ããã£ãŠãããšãã¯1
ãé£ç¶ã§è¡šç€ºãããŠããã°æåã§ãïŒ
ååã®ãã£ãã¿ãŒã§ã¯ã0
ã1
ãé£ç¶çã«ãã£ãšè¡šç€ºãããŠããŸããããå€åããã£ããšãã ã衚瀺ããããã°ã©ã ãäœæããŠäžããã
å ·äœçã«ã¯ä»¥äžã®ïŒã€ã®æ©èœã§ãã
Close
ãšïŒåã ã衚瀺Open!
ãšïŒåã ã衚瀺åè·¯çµç·ã¯ååã®ãã£ãã¿ãŒãšåãã§ãã
ç°¡åãªã¢ã³ã±ãŒãã«çããŠè§£çäŸãå
¥æãããïŒ
解çãèŠãåã«èªåã§äœã£ãŠæ©ãããšãããã°ã©ãã³ã°åäžã«ç¹ãããŸãã
Pygameãšã¯ãããªã²ãŒã ã補äœããããã«èšèšãããã¯ãã¹ãã©ãããã©ãŒã ã®Pythonã¢ãžã¥ãŒã«éã§ãPythonã§ã³ã³ãã¥ãŒã¿ã°ã©ãã£ã¯ã¹ãšé³å£°ãæ±ãããã®ã©ã€ãã©ãªãå«ãã§ããŸãã
æååãç»åã衚瀺ããããå
šç»é¢ã§è¡šç€ºããããBGMãå¹æé³ã鳎ããããšãæ軜ã«ã§ããŸãã
ç»ååŒçšå ïŒPygame Github Page
Pygameã掻çšããŠããã®ããã«ç»é¢ã«å€§ããäžå€®ã«æååã衚瀺ãããããšæããŸãã
ãã¡ãã®ç»åãPygameã®ãŠã£ã³ããŠã«è¡šç€ºããŸãã
ãããŸã§ã¯ã©ãºãã€ç»é¢ã®äžã®ãŠã£ã³ããŠè¡šç€ºã§ããããã©ãºãã€ã®å šç»é¢ã®è¡šç€ºã«ãã£ã¬ã³ãžããŸããã¡ãªã¿ã«ãå šç»é¢è¡šç€ºã¯ããã«ã¹ã¯ãªãŒã³è¡šç€ºããšãåŒã°ããŠããŸãã
Pygameã䜿ã£ãŠã以äžã®å 容ã§å šç»é¢ïŒãã«ã¹ã¯ãªãŒã³ïŒè¡šç€ºãè¡ã£ãŠãã ããã
FullScreen!
ãšããæåå衚瀺Q
ããŒæŒãã§çµäºãããã£ããããŸãïŒããšãããããšãããããŸããããšããé³å£°ãåºåããŸãã
ãããŸã§åŠãã å
容ãçµåããŠãåç»ã®ããã«ç©ççãªééãè¡ããšãç¶æ
å€åã«åãããŠé³å£°åºåãšè¡šç€ºãå€æŽããã·ã¹ãã ãå®æãããŠãã ããã
åç»ã§ã¯åŒåºãã±ãŒã¹ã䜿ã£ãŠããŸãããäœã§ãæ§ããŸããããã£ã¹ãã¬ã€ãçŽæ¥æ¥ç¶ã§ã¯ãªããPCã®VNC Viewerãã¹ããã®VNC Viewerãªã©ã§ãOKã§ãã
ä»åã®ã·ã¹ãã ã¯ä»¥äžã®ç¹åŸŽããããããæ§ã ãªæã§å¿çšå¯èœã§ãã
家ã«ã¯ãã¢ãå¿ ããããšæããŸããä»åã®ã»ã³ãµãŒã¯éãã¿ã€ãã®ãã¢ãåŒãæžã¿ã€ãã®ãã¢ãã©ã¡ãã«ãé©å¿å¯èœãªãããæ§ã ã«å¿çšã§ããŸãã
ããã®ãã®ããã¯QAæ²ç€ºæ¿ããšãã³ã³ãã³ãã®å€æŽå±¥æŽãã§ãã