// Include required libraries
// #include <MFRC522.h>
// #include <SPI.h>
#include <Adafruit_NeoPixel.h>
#define PIXELS_PER_SEGMENT 4 // Number of LEDs in each Segment
#define PIXELS_DIGITS 4 // Number of connected Digits
#define PIXELS_PIN 3 // GPIO Pin
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS_PER_SEGMENT * 7 * PIXELS_DIGITS + 3, PIXELS_PIN, NEO_GRB + NEO_KHZ800);
int prevDigit1 = 0; // variable to hold preious digit value
int prevDigit2 = 0; // variable to hold preious digit value
int prevDigit3 = 0; // variable to hold preious digit value
int prevDigit4 = 0; // variable to hold preious digit value
//Pixel Arrangement
/*
c
d b
a
e g
f
*/
// Total segments to be turned on in one digit we have 7 segments
byte oneDigitStrips[10] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 9 };
byte digitSegment1[10][7] = { { 4, 8, 12, 17, 21, 25 }, // Starting pixel number for digit 0 sem
{ 4, 25 }, // Starting pixel number for digit 1 sem
{ 0, 4, 8, 17, 21 }, // Starting pixel number for digit 2 sem
{ 0, 4, 8, 21, 25 }, // Starting pixel number for digit 3 sem
{ 0, 4, 12, 25 }, // Starting pixel number for digit 4 sem
{ 0, 8, 12, 21, 25 }, // Starting pixel number for digit 5 sem
{ 0, 8, 12, 17, 21, 25 }, // Starting pixel number for digit 6 sem
{ 4, 8, 25 }, // Starting pixel number for digit 7 sem
{ 0, 4, 8, 12, 17, 21, 25 }, // Starting pixel number for digit 8 sem
{ 0, 4, 8, 12, 25 } }; // Starting pixel number for digit 9 sem
void clearDisplay() { // Clear Full Display
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
void clearOneSegment(int startPixel, int endPixel) { // Clear One Segment
for (int i = startPixel; i < endPixel; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
const byte SS_PIN = 10; // MFRC522 Chip Select Pin
const byte RST_PIN = 9; // MFRT522 Reset Pin
// MFRC522 mfrc522(SS_PIN, RST_PIN); // Create an instance for RFID reader
const byte startButton = 5; // Start Button pin
const byte resetButton = 6; // Reset Button Pin
const byte cardBtn = 7;
unsigned long previousMillis = 0;
const long interval = 1000; // Update display every 1 second
int totalMinutes = 10; // Set total minutes here
int totalSeconds = 0; // totale seconds along with minute keep it zero if you just want to use minute
bool gameStarted = false; // Flag to check if game has been started or not
int minutes = totalMinutes; // momentary variable to store minute
int seconds = totalSeconds; // momentary variable to store second
void setup() {
Serial.begin(9600);
// SPI.begin();/
// mfrc522.PCD_Init();
pinMode(startButton, INPUT_PULLUP); // Set button pin as input with pullup
pinMode(resetButton, INPUT_PULLUP); // Set button pin as input with pullup
pinMode(cardBtn, INPUT_PULLUP); // Set button pin as input with pullup
strip.begin();
strip.setBrightness(255); // Set pixel brightness from 0-255
// Extract individual digits
int digit4 = seconds % 10; // Extract the least significant digit of seconds
int digit3 = seconds / 10; // Extract the tens place digit of seconds
int digit2 = minutes % 10; // Extract the least significant digit of minutes
int digit1 = minutes / 10; // Extract the tens place digit of minutes
prevDigit4 = digit4;
updateDigit(3, digit4);
prevDigit3 = digit3;
updateDigit(2, digit3);
prevDigit2 = digit2;
updateDigit(1, digit2);
prevDigit1 = digit1;
updateDigit(0, digit1);
}
void loop() {
unsigned long currentMillis = millis(); // Note down current time
if (digitalRead(startButton) == LOW && !gameStarted) { // Check if start button is pressed and previously the game was not started
gameStarted = true; // set the flag to start game
}
if (digitalRead(resetButton) == LOW) { // Check if reset button was pressed reset time
gameStarted = false; // Reset start flag
minutes = totalMinutes; // Reset the time back to original time
seconds = totalSeconds; // Reset the time back to original time
// display.showNumberDecEx(minutes * 100 + seconds, 0b11100000, true); // display time on segment
// Extract individual digits
// Extract individual digits
int digit4 = seconds % 10; // Extract the least significant digit of seconds
int digit3 = seconds / 10; // Extract the tens place digit of seconds
int digit2 = minutes % 10; // Extract the least significant digit of minutes
int digit1 = minutes / 10; // Extract the tens place digit of minutes
updateDigit(3, digit4);
updateDigit(2, digit3);
updateDigit(1, digit2);
updateDigit(0, digit1);
}
if (gameStarted) { // check if game was starte
if (currentMillis - previousMillis >= interval) { // if 1 second passed update display
updateDisplay();
previousMillis = currentMillis;
}
if (checkRFID()) { // call rfid function and check if any rfid is presented or not
gameStarted = false;
minutes = totalMinutes; // Reset the time back to original time
seconds = totalSeconds; // Reset the time back to original time
}
if (minutes == 0 && seconds == 0) {
gameStarted = false;
minutes = totalMinutes; // Reset the time back to original time
seconds = totalSeconds; // Reset the time back to original time
}
}
}
void updateDisplay() {
if (seconds == 0) {
minutes--;
seconds = 59;
} else {
seconds--;
}
// display.showNumberDecEx(minutes * 100 + seconds, 0b11100000, true);
// Extract individual digits
int digit4 = seconds % 10; // Extract the least significant digit of seconds
int digit3 = seconds / 10; // Extract the tens place digit of seconds
int digit2 = minutes % 10; // Extract the least significant digit of minutes
int digit1 = minutes / 10; // Extract the tens place digit of minutes
if (digit4 != prevDigit4) { // check if we need to update curent digit or not
prevDigit4 = digit4;
updateDigit(3, digit4);
}
if (digit3 != prevDigit3) { // check if we need to update curent digit or not
prevDigit3 = digit3;
updateDigit(2, digit3);
}
if (digit2 != prevDigit2) { // check if we need to update curent digit or not
prevDigit2 = digit2;
updateDigit(1, digit2);
}
if (digit1 != prevDigit1) { // check if we need to update curent digit or not
prevDigit1 = digit1;
updateDigit(0, digit1);
}
}
bool checkRFID() {
// if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
if (!digitalRead(cardBtn)) {
// RFID card detected, stop the timer
// display.showNumberDecEx(minutes * 100 + seconds, 0b11100000, true);
// Extract individual digits
// Extract individual digits
int digit4 = seconds % 10; // Extract the least significant digit of seconds
int digit3 = seconds / 10; // Extract the tens place digit of seconds
int digit2 = minutes % 10; // Extract the least significant digit of minutes
int digit1 = minutes / 10; // Extract the tens place digit of minutes
updateDigit(0, digit1);
updateDigit(1, digit2);
updateDigit(2, digit3);
updateDigit(3, digit4);
return true;
}
return false;
}
void updateDigit(int segmentNumber, int digitValue) {
Serial.print("Segment:");
Serial.print(segmentNumber);
Serial.print(", Digit Value:");
Serial.println(digitValue);
clearOneSegment(segmentNumber * 29, (segmentNumber + 1) * 29); // cleaer segment
for (int n = 0; n < oneDigitStrips[digitValue]; n++) {
strip.fill(strip.Color(255, 0, 0), digitSegment1[digitValue][n] + (segmentNumber * 29), 4);
}
strip.show();
}