#include #define RX_PIN 44 #define TX_PIN 43 #define BAUD 115200 #define NUM_LEDS 16 #define LED_PIN 43 CRGB leds[NUM_LEDS]; int sensorPins[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // Sensor pins for receiving MCU int sensorValues[18]; // Sensor readings (thresholded) for ALL sensors // Correct ordering of sensor pins from left to right, top to bottom int sensorPinsOrdered[] = {0, 1, 2, 3, 4, 5, 15, 16, 17, 6, 7, 8, 14, 13, 12, 11, 10, 9}; int sensorValuesOrdered[18]; void setup() { Serial1.begin(BAUD, SERIAL_8N1, RX_PIN, TX_PIN); for (int i = 0; i < 9; i++) { pinMode(sensorPins[i], INPUT); } FastLED.addLeds(leds, NUM_LEDS); for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB::White; } FastLED.show(); // For debugging Serial.begin(115200); } void loop() { // If data is available from transmitter if (Serial1.available()) { String receivedData = Serial1.readStringUntil('\n'); parseReceivedData(receivedData); // Read values from our own sensors for (int i = 0; i < 9; i++) { int sensorValue = analogRead(sensorPins[i]); if (sensorValue < 100) { sensorValues[i+9] = 1; } else if (sensorValue > 4000) { sensorValues[i+9] = 2; } else { sensorValues[i+9] = 0; } } orderSensorValues(sensorValues); // // Debugging: print sensor data // for (int i = 0; i < 18; i++) { // Serial.print("Sensor "); // Serial.print(i + 1); // Serial.print(" "); // Serial.println(sensorValuesOrdered[i]); // } changeColor(sensorValuesOrdered); delay(1000); } } // Parse received data into sensorValues array void parseReceivedData(String data) { int startIndex = 0; int endIndex = data.indexOf(','); int i = 0; while (endIndex != -1) { sensorValues[i] = data.substring(startIndex, endIndex).toInt(); startIndex = endIndex + 1; endIndex = data.indexOf(',', startIndex); i++; } // The last value (after the final comma) sensorValues[i] = data.substring(startIndex).toInt(); } void orderSensorValues(int sensorValues[]) { for (int i = 0; i < 18; i++) { sensorValuesOrdered[i] = sensorValues[sensorPinsOrdered[i]]; } } #include // Mapping pairs of numbers to words (the indices correspond to the magnet orientation/presence of the two sensors) String tileMap[3][3] = { {"BABA", "IS", "RED"}, {"ORANGE", "YELLOW", "GREEN"}, {"BLUE", "PURPLE", "WHITE"} }; // Color map from indices to color names CRGB colorMap[3][3] = { {CRGB::White, CRGB::White, CRGB::Red}, {CRGB::Orange, CRGB::Yellow, CRGB::Green}, {CRGB::Blue, CRGB::Purple, CRGB::White} }; // Function to check if the phrase "BABA IS" appears in any row or column void changeColor(int sensorValuesOrdered[]) { int colorsFound = 0; CRGB colors[] = {NULL, NULL}; // Check if any row forms "BABA IS" for (int row = 0; row < 3; row++) { int startIdx = row * 6; if (tileMap[sensorValuesOrdered[startIdx]][sensorValuesOrdered[startIdx + 1]] == "BABA" && tileMap[sensorValuesOrdered[startIdx + 2]][sensorValuesOrdered[startIdx + 3]] == "IS" && !(tileMap[sensorValuesOrdered[startIdx + 4]][sensorValuesOrdered[startIdx + 5]] == "BABA" || tileMap[sensorValuesOrdered[startIdx + 4]][sensorValuesOrdered[startIdx + 5]] == "IS")) { Serial.println(tileMap[sensorValuesOrdered[startIdx + 4]][sensorValuesOrdered[startIdx + 5]]); colors[colorsFound] = colorMap[sensorValuesOrdered[startIdx + 4]][sensorValuesOrdered[startIdx + 5]]; colorsFound++; for (int i = 0; i < NUM_LEDS; i++) { leds[i] = colorMap[sensorValuesOrdered[startIdx + 4]][sensorValuesOrdered[startIdx + 5]]; } FastLED.show(); } } // Check if any column forms "BABA IS" for (int col = 0; col < 3; col++) { int startIdx = col * 2; if (tileMap[sensorValuesOrdered[startIdx]][sensorValuesOrdered[startIdx + 1]] == "BABA" && tileMap[sensorValuesOrdered[startIdx + 6]][sensorValuesOrdered[startIdx + 7]] == "IS" && !(tileMap[sensorValuesOrdered[startIdx + 12]][sensorValuesOrdered[startIdx + 13]] == "BABA" || tileMap[sensorValuesOrdered[startIdx + 12]][sensorValuesOrdered[startIdx + 13]] == "IS")) { Serial.println(tileMap[sensorValuesOrdered[startIdx + 12]][sensorValuesOrdered[startIdx + 13]]); colors[colorsFound] = colorMap[sensorValuesOrdered[startIdx + 12]][sensorValuesOrdered[startIdx + 13]]; colorsFound++; for (int i = 0; i < NUM_LEDS; i++) { leds[i] = colorMap[sensorValuesOrdered[startIdx + 12]][sensorValuesOrdered[startIdx + 13]]; } FastLED.show(); } } }