// KF LED setup
// A B
// C D
// define LED pins
// KF note: 2 digital pins and 2 analog pins because I've run out of digital pins on my arduino for my whole project
const int aled = 4; // RED (top left) = A
const int bled = 2; // GREEN (top right) = B
const int cled = A3; // BLUE (bottom left) = C
const int dled = A5; // YELLOW (bottom right) = D
// 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
}
// array to organize all possible LED forms/patterns
// each pattern will be a list of LED indexes that should turn on
// in LED array above, A = 0, B = 1, C = 2, D = 3 and -1 is a sentinel value
int patterns[5][4] = {
{0, 1, -1, -1}, // A B (top)
{2, 3, -1, -1}, // C D (bottom)
{0, 2, -1, -1}, // A C (left)
{1, 3, -1, -1}, // B D (right)
{0, 1, 2, 3} // A B C D (all)
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// set each LED pin as an output and turn off initially
for (int x = 0; x < 4; x++)
{
pinMode(leds[x], OUTPUT); // set LED pin as output device
ledOff(leds[x]); // turn LED off
}
// use random seed to pick a random pattern
// read from analog pin A2, which is not connected or used for any other purpose/component
randomSeed(analogRead(A2));
}
// function that prints the intended pattern on the serial monitor
void printPatternVisual(int patternIndex) // function receives index of the pattern to display
{
// flags for each LED, they are true if that specific LED is included in the pattern chosen
bool A = false;
bool B = false;
bool C = false;
bool D = false;
for (int x = 0; x < 4; x++) // for each of the 4 separate LEDs
{
// go through each entry of the pattern received through the function call
int ledIndex = patterns[patternIndex][x];
// if entry has sentinel value, break because there's no more LEDs in the pattern
if (ledIndex == -1)
{
break;
}
// otherwise, include the LEDs that are in the chosen pattern
if (ledIndex == 0)
{
A = true;
}
if (ledIndex == 1)
{
B = true;
}
if (ledIndex == 2)
{
C = true;
}
if (ledIndex == 3)
{
D = true;
}
}
Serial.println("-----------------");
Serial.print(A ? "A " : "_ "); // if A is in pattern, display A in its spot. otherwise, display _
Serial.println(B ? "B" : "_"); // if B is in pattern, display B in its spot. otherwise, display _
Serial.print(C ? "C " : "_ "); // if C is in pattern, display C in its spot. otherwise, display _
Serial.println(D ? "D" : "_"); // if D is in pattern, display D in its spot. otherwise, display _
Serial.println("-----------------");
Serial.println();
}
// function displays the LEDs in the correct pattern
void showPattern(int patternIndex)
{
for (int x = 0; x < 4; x++) // for each of the 4 separate LEDs
{
int ledIndex = patterns[patternIndex][x]; // index records that pattern's entry
if (ledIndex == -1) break; // if entry in specific pattern is -1, there are no more LEDs in the pattern
ledOn(leds[ledIndex]); // turn on that LED
}
//call function to print that pattern on serial monitor
printPatternVisual(patternIndex);
delay(1000); // leave LEDs on for 1 second
// turn off all LEDs before showing next pattern
for (int x = 0; x < 4; x++)
{
ledOff(leds[x]);
}
delay(300);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Generated pattern sequence:");
for (int round = 0; round < 4; round++) // 4 random pattern actions = 4 rounds
{
int randomPattern = random(0, 5); // picks one pattern out of the five possible
showPattern(randomPattern); // call function to show this random pattern on LEDs
}
Serial.println("---- NEW ROUND ----");
Serial.println();
delay(1500);
}