From f69bfee67cde8b0416cc39975b9fe11924bd958a Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Thu, 6 Oct 2022 16:51:16 -0400 Subject: Add week5 clanker --- week5/week5.ino | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 week5/week5.ino diff --git a/week5/week5.ino b/week5/week5.ino new file mode 100644 index 0000000..52106f7 --- /dev/null +++ b/week5/week5.ino @@ -0,0 +1,41 @@ +void setup() { + for (int i = 0; i < 8; ++i) pinMode(i, OUTPUT); + analogReadResolution(8); +} + +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); // generate another random number + for (int i = 0; i < len; ++i) { // flash the LEDs according to seq + digitalWrite(2*seq[i], HIGH); + delay(500); + digitalWrite(2*seq[i], LOW); + delay(500); + } + for (int i = 0; i < len; ++i) { + 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(250); + } + for (int i = 0; i < 8; ++i) { // flash green lights 4 times + for (int j = 0; j < 4; ++j) digitalWrite(2*j, (i+1)%2); + delay(100); + } + } + DONE: + for (int i = 0; i < 2; ++i) { // flash red lights 4 times + for (int j = 0; j < 2; ++j) digitalWrite(2*j, 1); + delay(500); + for (int j = 0; j < 2; ++j) digitalWrite(2*j, 0); + delay(500); + for (int j = 2; j < 4; ++j) digitalWrite(2*j, 1); + delay(500); + for (int j = 2; j < 4; ++j) digitalWrite(2*j, 0); + delay(500); + } + delay(1000); +} -- cgit v1.2.3-70-g09d2