teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを修正いたしました。

2021/09/26 09:51

投稿

wataru.py
wataru.py

スコア7

title CHANGED
File without changes
body CHANGED
@@ -58,4 +58,61 @@
58
58
 
59
59
  if score == 2:
60
60
  break
61
+ ```
62
+
63
+ y_waiwai様、thkana様回答、アドバイス本当にありがとうございました!!!
64
+ 以下、ご指摘を参考に修正したコードです。
65
+ 現時点では思ったように動作しております。
66
+ 改良点などあればお願い致します。
67
+ ```import machine
68
+ import utime
69
+ import random
70
+
71
+ led1 = machine.Pin(0,machine.Pin.OUT)
72
+ led2 = machine.Pin(1,machine.Pin.OUT)
73
+ led3 = machine.Pin(2,machine.Pin.OUT)
74
+
75
+ switch1 = machine.Pin(3,machine.Pin.IN,machine.Pin.PULL_UP)
76
+ switch2 = machine.Pin(4,machine.Pin.IN,machine.Pin.PULL_UP)
77
+ switch3 = machine.Pin(5,machine.Pin.IN,machine.Pin.PULL_UP)
78
+
79
+ score = 0
80
+
81
+ flag = False
82
+
83
+ ledlist = [led1,led2,led3]
84
+
85
+
86
+
87
+
88
+ while True:
89
+ i = random.randint(0,2)
90
+ x = ledlist[i]
91
+
92
+ x.value(1)
93
+ for n in range(100):
94
+ utime.sleep(0.01)
95
+
96
+ if (led1.value() == 1):
97
+ if (switch1.value() == 0 ):
98
+ flag = True
99
+
100
+ if (led2.value() == 1):
101
+ if (switch2.value() == 0 ):
102
+ flag = True
103
+
104
+ if (led3.value() == 1):
105
+ if (switch3.value() == 0 ):
106
+ flag = True
107
+
108
+ x.value(0)
109
+ utime.sleep(1)
110
+ if (flag == True):
111
+ score += 1
112
+ flag = False
113
+ print(score)
114
+
115
+ if (score == 3):
116
+ break
117
+
61
118
  ```