diff options
author | Anthony Wang | 2022-09-29 15:58:09 -0400 |
---|---|---|
committer | Anthony Wang | 2022-09-29 15:58:09 -0400 |
commit | cf51e2732337879f61b900eb53cb37c5021326b8 (patch) | |
tree | a3560802cc8d3196d93365fd3766fa4b1b27585a | |
parent | e584c381d9c2efab9fb7a15ba6d5d0ea818488c0 (diff) |
Add more comments for week4.ino
-rw-r--r-- | week4/week4.ino | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/week4/week4.ino b/week4/week4.ino index f8ce7f9..073df39 100644 --- a/week4/week4.ino +++ b/week4/week4.ino @@ -6,28 +6,29 @@ void setup() { void loop() { int len = 0; int seq[1000]; + while (len < 3) seq[len++] = random(0, 4); // generate 3 random numbers while (1) { - seq[len++] = random(0, 4); - for (int i = 0; i < len; ++i) { - digitalWrite(seq[i]<<1|1, HIGH); + seq[len++] = random(0, 4); // generate another random number + for (int i = 0; i < len; ++i) { // flash the LEDs according to seq + digitalWrite(2*seq[i]+1, HIGH); delay(500); - digitalWrite(seq[i]<<1|1, LOW); + digitalWrite(2*seq[i]+1, LOW); delay(500); } for (int i = 0; i < len; ++i) { - while (analogRead(A0) < 240 && analogRead(A1) < 240 && analogRead(A2) < 240 && analogRead(A3) < 240); - for (int j = 0; j < 4; ++j) + while (analogRead(A0) < 240 && analogRead(A1) < 240 && analogRead(A2) < 240 && analogRead(A3) < 240); // wait for press + for (int j = 0; j < 4; ++j) // check which button was pressed and whether it matches seq if (analogRead(A0+j) > 240 && seq[i] != j) goto DONE; // Legit use case of goto to jump out of 3 loops - delay(500); + delay(250); } - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 4; ++j) digitalWrite(j<<1|1, i&1^1); + for (int i = 0; i < 8; ++i) { // flash green lights 4 times + for (int j = 0; j < 4; ++j) digitalWrite(2*j+1, (i+1)%2); delay(100); } } DONE: - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 4; ++j) digitalWrite(j<<1, i&1^1); + for (int i = 0; i < 8; ++i) { // flash red lights 4 times + for (int j = 0; j < 4; ++j) digitalWrite(2*j, (i+1)%2); delay(100); } } |