[No.9]暗くなったら自動的に点灯!〜レインボーライトを作ろう
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/R5OqXcPWkPM9uQiakKkCG.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=eDaCVKNgHr4KzvNw81iSvTcRyyA%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
小さなものづくりコンピューター「SPACEBLOCK(スペースブロック)」は、いろいろなパーツを足してプログラムを工夫してさまざまな機能を作ることができます。今回は「明るさセンサモジュール(明るさセンサ)」を使って暗くなったら自動的に点灯するライトを作りましょう。前回[SPACEBLOCK-8]で覚えた「繰り返し」と、新たに「条件分岐」の技を使ってプログラミングしますよ。
必要なもの&準備
- SPACEBLOCK通常パッケージ[SPACEBLOCK本体、USBケーブル、電池ボックス]
- 8×8 RGB LEDマトリックスパネル(LEDパネル)
- ジャンパーワイヤ[ピンヘッダ-ピンソケット3本]
- 明るさセンサモジュール(明るさセンサ)
- プログラミングをするパソコン
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/XPoXIkOVTIHkkOiEbluAT.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=IHsQzrDaRZgrGlv9W7iXjrEScYU%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
SPACEBLOCKに拡張パーツを接続する
まずは、今回使用するLEDパネル、明るさセンサをSPACEBLOCKに接続してみましょう。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/9ykT8Ob45XMAAahKO3cIB.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=DlZbr7qNTNFTNnDxPrBgqrhBq2Y%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
細かい部分は次の図を参考にしてください。明るさセンサからは3本のワイヤが出ていますので、黄色ピンソケットの図の通りの位置に接続してください。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/83Mv5KHyTrlrHUONwbhhN.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=U%2Fo8aOrDOahpapCaKIWDjrvJyGU%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
明るさセンサって何ができるの?
明るさセンサは、明るさを測定して数値化することができます。一番暗い状態が0で、明るくなるほど数値は大きくなります。このように、明るさなどの程度を測定し数値にしてプログラムに渡すことをアナログ入力と呼びます。
明るさセンサは、測定した明るさに応じてSPACEBLOCKに挿したピンヘッダの電圧を上げたり下げたりします。電圧の程度に応じて0から1023までの数値に置き換えてプログラムに値を渡しています。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/mxAumyyOQt8IutkIiXe0s.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=Ybv5yWZNni%2FW9DpigR03W6w1ySI%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
明るさセンサの値は、[入力]カテゴリーの[《A0》の電圧をアナログ読取]というブロックで取得します。まずは明るさセンサが測定した値を表示するプログラムを作りましょう。次の図の通り[基本]カテゴリーの[数字《0》を表示]ブロックと組み合わせてください。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/Vb5Dkzh5jWu3PU8Yut69X.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=SXeCMfgTGDtXpi%2FGzTZEYixu%2FOY%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
[《A0》の電圧をアナログ読取]ブロックをクリックするとピンソケットの一覧が表示されるので、「B0」を選びます。同時に、明るさセンサを図の通り接続していることを確認してください。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/is8jogxqffXzEnkS1aYJN.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=VbMNKlXeGdmBrDPBNbViCib8qOs%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
明るさセンサの測定値を見てみよう!
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/r5rB-8Kt8uCnUvVeZk8GQ.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=b03sxOWpioHozC9EXvuOlUNuQ5Y%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
昼間の自然光が入る室内で実行したところ、値は300でした。明るさセンサの向きや場所を変えてプログラムを実行すると、また違う値が表示されます。
上の図のプログラムでは実行した瞬間の明るさセンサの値しかわかりません。常に値を表示させるようプログラムをアレンジしましょう。次の図の通り、[繰り返す]ブロックを追加してください。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/rpb8tsYHY7jcFODDyVNnL.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=UTgeAJl%2BzSsscycF7NdBIwbzHIc%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
上の図のプログラムを実行すると、値を繰り返し表示し続けるので、コンソールに値が次々に流れていきます。
明るさセンサに手をかざしたりライトをあてたりすると、リアルタイムで測定値が変化するのがわかるので試してみましょう。どのくらいの明るさでどのくらいの数値になるでしょうか? センサ部分を完全にふさぐと「0」になりますよ。
暗くなったらLEDパネルが点灯する装置
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/_uF1JfkrLL5fUAYuqETnl.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=piisx9U0zKcep7Z%2FYZYJwYcjIdY%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
プログラムでは「なんとなく暗くなったら」「夕方くらいの暗さになったら」「適当に常識的に判断して」という指示はできません。「明るさセンサの値が○よりも小さいなら」と具体的な数字で表現します。
明るさセンサの値がいくつより小さい状態を「暗い」ということにするのかは、プログラマーである皆さん次第。置きたい場所の環境によって適正な値は違うので、自分でいろいろな値を試して決めてください。今回は、「玄関の電気を消したら点灯する」ことを目的として試し、50としました。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/uZjPRjx3PMlIkQo47ShQ0.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=16edWriH1PGKSqqff2xLQalUS1U%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
「論理」カテゴリーのブロックを使うと、設定した条件が「はい」の状態のときと「いいえ」の状態のときで実行する命令を分けることができます。これを「条件分岐」と呼びます。何を条件にするかによって、さまざまな仕組みを作ることができます。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/YIVTAg_rh7mAR6R-kqtOt.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=M1WAK9deKPh1IcDaI83tD6%2B%2B4Dk%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/5QOkJnAcCu6lvZDb_qhFH.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=bSxpFZgor1kvIkwOZf7U%2F%2FRlC7w%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/8GJxam3xx1KgAMlIYCI26.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=aNfqM1NznJjXxWDq6QK3CI%2FIAFY%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/8FDTlkCIYBe5Hud38mKTl.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=bKfDExpuAXFYi72K81eS75c8NQ0%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
動画でご覧ください。
![](https://s3.ap-northeast-1.amazonaws.com/spaceblock.jp-storage/inquiries/2024-03-26/CWRjngqakJ2nva19iqxG7.png?AWSAccessKeyId=ASIAZZVIWFLRYPYCASPF&Expires=1725769075&Signature=J6gsbLsBeKhdCKApQAuMNR%2FStOU%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEBIaDmFwLW5vcnRoZWFzdC0xIkgwRgIhAMKKk4aPUJn0e3SMDa3%2FAteardhsO4C2QmZ%2FkpDJXcREAiEA5h1%2BhXMSAVKKhjvcAZ3JU%2FoECFek1D9LtUesx0G6ZkcqlAMIOxABGgw2NzM1ODk4OTc5NTUiDLP6gTNK2iBRQ0QEgCrxAre5ARThtFYiP5HewoKJlDsZ%2Fym9MJOratwTQ9mb3bhJubMItXViaFiSPI8BowsTLnGCxorQFqzSDW9PG05Z4D1jSUBH9Mhn0XE3SM%2BH66V86Tnci%2BdakfPPKnmEzbccndH80Z6m4jfOFQKHWXUuWNb0lBa6WFR9MmesdFj88%2FRRBgooF8chJBXQ9uBZgq%2FAuaY%2Bbyly3iIlMWXpgXUQy1tLuD5Sq7HEF8%2FB5ziU40hTprO%2B2jFPcRPNIhJEFNy%2BJpj%2Fq9ftmp2wYSuTwDF30wnZdZsiVW1pxwjadXMPUEecpeKukqEhtKcyl1IOIlojPFJphRJZZ%2BY0vgBgKhD%2BJZiKG3Kx9FSKT9Dz1sYdfWMQElc6yFFOnZ6R5hpdSHVWxLLdEW8d8S85H5xlSb26Q0JiCNupA4u9jENLdP%2BS02ry0q19vEzYrD%2B%2Fmo6gjXXZa6U66dUlVLSmhl0ZV%2FD17G4DrbZyTZIPv8okvFZpqYvvUTCpivS2BjqcATPuLjpWxhuwUWl1HwkKq2WH3PK4sfLQGRY%2Fmc8xZ0IzU6IDaMosEV%2B4LFr33F1IadIBT9rZVJZ2BBeI6vAQnKMWSeEnjHIXZANw8%2Fk0Kw7DNj46HKVTWfWkDGwH5a3MvBjxo18yawLeYwNSfetGVpS6TMMz8BArcKp7OTbcaLeyXC4Qr%2F0Hoevcuipf9syVdc74eLpb6gIDf9r4Qg%3D%3D)
同じ場所に置いていても、明るさセンサをどんな位置や向きに設置するかによって、測定の値は変わりますから、設置の仕方に応じて条件の値を変えたり、センサの配置を変えたりして工夫してみましょう。
動画でご覧ください。