// assign pin to LED (2 digital pins and 2 analog pin
// because I've run out of digital pins on my arduino for my whole project)
const int aled = 4; // RED (top-left)
const int bled = 2; // GREEN (top-right)
const int cled = A3; // BLUE (bottom-left)
const int dled = A5; // YELLOW (bottom-right)
// array of LED pins in the order A B C D
int leds[4] = { aled, bled, cled, dled };
// KF note: LEDs go 5V to led/resistor to pin so LOW is on and HIGH is OFF
// function to turn on LED connected to pin number it recieves
void ledOn(int pin) {
digitalWrite(pin, LOW); // pin LOW = LED on
}
// function to turn off LED connected to pin number it receives
void ledOff(int pin) {
digitalWrite(pin, HIGH); // pin HIGH = LED off
}
// function that allows serial monitor to print the desired pattern in terms of 1's and 0's
// KF note: 1 = on and 0 = off
// example: AC on and BD off = ABCD = 1010
void printPattern(int value) {
for (int bit = 3; bit >= 0; bit--) {
Serial.print((value >> bit) & 1);
}
Serial.println();
}
// Light LEDs according to a 4-bit pattern (ABCD)
void showPattern(int pattern) {
for (int i = 0; i < 4; i++) {
int bit = (pattern >> (3 - i)) & 1; // get bit 3→0
if (bit == 1)
ledOn(leds[i]);
else
ledOff(leds[i]);
}
}
void setup() {
Serial.begin(9600);
// Set LED pins as OUTPUT and turn them off
for (int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
ledOff(leds[i]);
}
randomSeed(analogRead(A0)); // randomness
}
void loop() {
// Example: generate 4 actions
const int ACTION_COUNT = 4;
int actions[ACTION_COUNT];
Serial.println("Generated pattern:");
for (int i = 0; i < ACTION_COUNT; i++) {
// Allowed patterns (no diagonals, no singles)
int validActions[5] = {
0b1100, // A + B
0b0011, // C + D
0b1111, // all 4
0b1010, // A + C (vertical left)
0b0101 // B + D (vertical right)
};
actions[i] = validActions[random(0, 5)];
printPattern(actions[i]); // show in Serial
}
Serial.println("Showing pattern...");
for (int i = 0; i < ACTION_COUNT; i++) {
showPattern(actions[i]); // turn LEDs on
delay(500);
// turn them all off between flashes
for (int j = 0; j < 4; j++) ledOff(leds[j]);
delay(300);
}
Serial.println("---- NEW ROUND ----\n");
delay(1500);
}