#include #include // ---------- SENSOR ---------- #define SENSOR_PIN D2 int lastState = 1; // ---------- LED STRIP ---------- #define LED_PIN D1 #define TOTAL_LEDS 60 Adafruit_NeoPixel strip(TOTAL_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); // ---------- DISPLAY ---------- #define CLK D0 #define DIO D7 TM1637Display display(CLK, DIO); // ---------- RESET BUTTON ---------- #define RESET_BUTTON D4 int lastResetButtonState = HIGH; // ---------- CONTROL ---------- bool goalActive = false; unsigned long goalStartTime = 0; const int goalDuration = 2000; // LED blinking time const int goalTextDuration = 1000; // "GOAL" on display for 1 second unsigned long lastBlinkTime = 0; bool ledState = false; const int blinkInterval = 100; // ---------- SCORE ---------- int score = 0; // ---------- CUSTOM SEGMENTS FOR "GOAL" ---------- const uint8_t CHAR_G = 0x3D; const uint8_t CHAR_O = 0x3F; const uint8_t CHAR_A = 0x77; const uint8_t CHAR_L = 0x38; const uint8_t GOAL_WORD[] = { CHAR_G, CHAR_O, CHAR_A, CHAR_L }; void setup() { Serial.begin(115200); pinMode(SENSOR_PIN, INPUT); pinMode(RESET_BUTTON, INPUT_PULLUP); // LED strip strip.begin(); strip.clear(); strip.show(); // Display display.setBrightness(5); display.showNumberDec(score, true); // Start at 0 lastResetButtonState = digitalRead(RESET_BUTTON); Serial.println("Goal system with score and reset ready"); } void loop() { int currentState = digitalRead(SENSOR_PIN); int currentResetButtonState = digitalRead(RESET_BUTTON); // ---------- RESET SCORE ---------- if (lastResetButtonState == HIGH && currentResetButtonState == LOW) { score = 0; display.showNumberDec(score, true); Serial.println("Score reset to 0"); delay(250); // debounce } lastResetButtonState = currentResetButtonState; // ---------- GOAL DETECTION ---------- if (lastState == 1 && currentState == 0 && !goalActive) { Serial.println("GOAL!! ⚽"); // Increase score score++; // Start animation and display effect goalActive = true; goalStartTime = millis(); lastBlinkTime = millis(); ledState = false; // Show GOAL immediately display.setSegments(GOAL_WORD); } lastState = currentState; // ---------- GOAL EFFECT ---------- if (goalActive) { unsigned long currentTime = millis(); unsigned long elapsedTime = currentTime - goalStartTime; // LED blinking for 2 seconds if (elapsedTime <= goalDuration) { if (currentTime - lastBlinkTime > blinkInterval) { lastBlinkTime = currentTime; ledState = !ledState; if (ledState) { setAllGreen(); } else { strip.clear(); strip.show(); } } } // Display "GOAL" for 1 second, then show score if (elapsedTime <= goalTextDuration) { display.setSegments(GOAL_WORD); } else { display.showNumberDec(score, true); } // End animation after 2 seconds if (elapsedTime > goalDuration) { goalActive = false; strip.clear(); strip.show(); display.showNumberDec(score, true); } } } // ---------- FUNCTIONS ---------- void setAllGreen() { for (int i = 0; i < TOTAL_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 255, 0)); } strip.show(); }