#include <TM1637Display.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
// opomba rfid varjanta nima aktivnih štirih tipk oz petih tipk ker so tam priklopljni
//stikala za izbor časa
//definiraj RGB strip
#define PIN_STRIP 9
#define NUM_PIXELS 16
Adafruit_NeoPixel STRIP(NUM_PIXELS, PIN_STRIP, NEO_GRB + NEO_KHZ800);
#define RST_PIN 9 // Konfigurirajte vaš RST pin (Reset)
#define SS_PIN 10 // Konfigurirajte vaš SDA (SS) pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Ustvarite MFRC522 instanco.
#define buzzer 7
// Define the common CLK pin connected to all displays
#define CLK_PIN 2
// Define the DIO pins for each display
#define DIO_PIN_1 3
#define DIO_PIN_2 4
#define DIO_PIN_3 5
#define DIO_PIN_4 6
const int UPTime = A1;
const int DOWNTime = A2;
const int STARTTime = A3;
const int PBRed = A5;
const int PBBlue = A4;
//const int PBYellow = A3;
//const int PBGreen = A2;
//const int PBBlack = A1;
const int mosfet = 8;
const int MOSFET_TIME = 500;
unsigned long PlayTime ;
unsigned long iTime;
unsigned long redTime;
unsigned long greenTime;
unsigned long blueTime;
unsigned long yellowTime;
byte team=0; // 0 = neutral,
int selectedTeam = 0;
int i, j;
int seconds;
int secondsr;
int secondsb;
int secondsy;
int secondsg;
int minutes;
int minutesr;
int minutesb;
int minutesy;
int minutesg;
int SPBRed;
int SPBBlue;
int SPBYellow;
int SPBGreen;
int SPBBlack;
// Create instances of the TM1637Display for each display
TM1637Display display1(CLK_PIN, DIO_PIN_1);
TM1637Display display2(CLK_PIN, DIO_PIN_2);
TM1637Display display3(CLK_PIN, DIO_PIN_3);
TM1637Display display4(CLK_PIN, DIO_PIN_4);
const byte all_on[] = { 0b11111111,
0b11111111,
0b11111111,
0b11111111 };
const uint8_t SEG_X[] = {
SEG_G, // -
SEG_G, // -
SEG_G, // -
SEG_G, // -
};
void setup() {
Serial.begin(9600);
Wire.begin();
STRIP.begin();
// Set the display brightness for all displays (0-7)
display1.setBrightness(7);
display2.setBrightness(7);
display3.setBrightness(7);
display4.setBrightness(7);
pinMode(UPTime, INPUT); // Minutes adjust
pinMode(DOWNTime, INPUT); // Seconds adjust
pinMode(STARTTime, INPUT);
pinMode(PBRed, INPUT);
pinMode(PBBlue, INPUT);
pinMode(PBYellow, INPUT);
pinMode(PBGreen, INPUT);
pinMode(PBBlack, INPUT);
pinMode(mosfet, OUTPUT);
SPI.begin(); // Začni SPI komunikacijo
mfrc522.PCD_Init(); // Inicializiraj MFRC522 čitalnik
Serial.println(F("Približajte kartico čitalniku..."));
Serial.println();
theaterChase(STRIP.Color(255, 0, 0), 20); //red
display1.setSegments(all_on);
tone(buzzer,1000,100);
delay(500);
theaterChase(STRIP.Color(0, 0, 255), 20); //blue
display2.setSegments(all_on);
tone(buzzer,1000,100);
delay(500);
theaterChase(STRIP.Color(255, 128, 0), 20); //yellow
display3.setSegments(all_on);
tone(buzzer,1000,100);
delay(500);
theaterChase(STRIP.Color(0, 255, 0), 20); // green
display4.setSegments(all_on);
tone(buzzer,1000,100);
delay(500);
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(255,255,255)); //white
STRIP.show();
}
delay(500);
tone(buzzer,1500,100);
display1.setSegments(SEG_X);
delay(500);
tone(buzzer,1500,100);
display2.setSegments(SEG_X);
delay(500);
tone(buzzer,1500,100);
display3.setSegments(SEG_X);
delay(500);
tone(buzzer,1500,100);
display4.setSegments(SEG_X);
delay(500);
while(1,2){
minutes = seconds / 60;
//Serial.print(seconds);
display1.showNumberDecEx(seconds - minutes * 60,0b01000000, true, 2, 2);
display1.showNumberDec(minutes, true, 2, 0);
if( digitalRead(UPTime) == HIGH ) {
seconds += 60;
//Serial.print(seconds);
tone (buzzer, 500, 20);
delay(500);
}
if( digitalRead(DOWNTime) == HIGH ) {
seconds -= 60;
//Serial.print(seconds);
tone (buzzer, 500, 20);
delay(500);
}
if (seconds <= 0) seconds = 60;
//if (seconds == 0 ) seconds = 15;
if( digitalRead(STARTTime) == HIGH ) {
PlayTime = seconds;
//Serial.println(PlayTime);
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(0,0,0)); // red
STRIP.show();
tone(buzzer,1500,20);
delay(150);
}
tone(buzzer,1500,100);
display1.setSegments(SEG_X);
delay(500);
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(255,255,255)); //white
STRIP.show();
}
break;
}
}
}
void loop() {
flag();
}
void flag(){
while(1){
// Preveri, če je nova kartica prisotna
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
Serial.print(F("Kartica zaznana: "));
// Izpiši UID prebrane kartice
String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println(content);
// Preveri UID zaznane kartice in aktiviraj funkcionalnost
// Primer: Če je UID kartice določena vrednost, aktiviraj funkcijo za rdečo ekipo
if (content.substring(1) == "50 26 E8 9F" || content.substring(1) == "77 65 EA 9F") {tone(buzzer,1500,200); activateMosfet();
Serial.println(F("Aktivirana rdeča ekipa!"));
SPBRed = 1; SPBBlue = 0; SPBYellow = 0; SPBGreen = 0; SPBBlack = 0;}
if (content.substring(1) == "5B 3A E9 9F" || content.substring(1) == "A4 04 EA 9F") {tone(buzzer,1500,200); activateMosfet();
Serial.println(F("Aktivirana modra ekipa!"));
SPBBlue = 1; SPBRed = 0; SPBYellow = 0; SPBGreen = 0; SPBBlack = 0;}
if (content.substring(1) == "04 58 E9 9F" || content.substring(1) == "5E 03 EA 9F") {tone(buzzer,1500,200); activateMosfet();
Serial.println(F("Aktivirana ORANŽNA ekipa!"));
SPBYellow = 1; SPBRed = 0; SPBBlue = 0; SPBGreen = 0; SPBBlack = 0;}
if (content.substring(1) == "3A F5 E9 9F" || content.substring(1) == "AC D7 E9 9F") {tone(buzzer,1500,200); activateMosfet();
Serial.println(F("Aktivirana zelena ekipa!"));
SPBGreen = 1; SPBRed = 0; SPBBlue = 0; SPBYellow = 0; SPBBlack = 0;}
if (content.substring(1) == "64 7B E9 9F") {tone(buzzer,1500,200); activateMosfet();
Serial.println(F("Aktivirana nevtralno!"));
SPBBlack = 1; SPBRed = 0; SPBBlue = 0; SPBYellow = 0; SPBGreen = 0;}
// Dodajte dodatne pogoje za preostale kartice/ekipe
mfrc522.PICC_HaltA(); // Ustavi branje kartice
}
//if (digitalRead(PBRed) == HIGH) {activateMosfet(); SPBRed = 1; SPBBlue = 0; SPBYellow = 0; SPBGreen = 0; SPBBlack = 0;}
//if (digitalRead(PBBlue) == HIGH) {activateMosfet(); SPBBlue = 1; SPBRed = 0; SPBYellow = 0; SPBGreen = 0; SPBBlack = 0;}
//if (digitalRead(PBYellow) == HIGH) {activateMosfet(); SPBYellow = 1; SPBRed = 0; SPBBlue = 0; SPBGreen = 0; SPBBlack = 0;}
//if (digitalRead(PBGreen) == HIGH) {activateMosfet(); SPBGreen = 1; SPBRed = 0; SPBBlue = 0; SPBYellow = 0; SPBBlack = 0;}
//if (digitalRead(PBBlack) == HIGH) {activateMosfet(); SPBBlack = 1; SPBRed = 0; SPBBlue = 0; SPBYellow = 0; SPBGreen = 0;}
// Affichage de l'état de la base
if (SPBRed == 1){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(255,0,0)); // red
STRIP.show();
}
if( millis() - redTime >= 1000 ) {
secondsr++;
redTime = millis();
tone(buzzer,1400,30);
}
minutesr = secondsr / 60;
display1.showNumberDecEx(secondsr - minutesr * 60,0b01000000, true, 2, 2);
display1.showNumberDecEx(minutesr,0b01000000, true, 2, 0);
}
if (SPBBlue == 1){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(0,0,255)); // blue
STRIP.show();
}
if( millis() - blueTime >= 1000 ) {
secondsb++;
blueTime = millis();
tone(buzzer,1400,30);
}
minutesb = secondsb / 60;
display2.showNumberDecEx(secondsb - minutesb * 60,0b01000000, true, 2, 2);
display2.showNumberDecEx(minutesb,0b01000000, true, 2, 0);
}
if (SPBYellow == 1){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(255, 128, 0)); // yellow
STRIP.show();
}
if( millis() - yellowTime >= 1000 ) {
secondsy++;
yellowTime = millis();
tone(buzzer,1400,30);
}
minutesy = secondsy / 60;
display3.showNumberDecEx(secondsy - minutesy * 60,0b01000000, true, 2, 2);
display3.showNumberDecEx(minutesy,0b01000000, true, 2, 0);
}
if (SPBGreen == 1){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(0,255,0)); // green
STRIP.show();
}
if( millis() - greenTime >= 1000 ) {
secondsg++;
greenTime = millis();
tone(buzzer,1400,30);
}
minutesg = secondsg / 60;
display4.showNumberDecEx(secondsg - minutesg * 60,0b01000000, true, 2, 2);
display4.showNumberDecEx(minutesg,0b01000000, true, 2, 0);
}
if (SPBBlack == 1){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
STRIP.setPixelColor(pixel,STRIP.Color(255,255,255)); //white
STRIP.show();
}
if( millis() - iTime >= 1000 ) {
seconds++;
iTime = millis();
//tone (buzzer, 500, 20);
minutes = seconds / 60;
}
}
else{
}
if (secondsr == PlayTime & SPBRed == 1 || secondsb == PlayTime & SPBBlue == 1 ||secondsy == PlayTime & SPBYellow == 1 || secondsg == PlayTime & SPBGreen == 1 ){
SPBBlack = 0; SPBRed = 0; SPBBlue = 0; SPBYellow = 0; SPBGreen = 0;
activateMosfet();
delay(500);
activateMosfet();
delay(500);
digitalWrite(mosfet, HIGH); // turn the MOSFET ON
delay(1000); // wait for 4 second
digitalWrite(mosfet, LOW); // turn the MOSFET ON
delay(300000);
}
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < NUM_PIXELS; i=i+3) {
STRIP.setPixelColor(i+q, c); //turn every third pixel on
}
STRIP.show();
delay(wait);
for (int i=0; i < NUM_PIXELS; i=i+3) {
STRIP.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
void activateMosfet(){
//lcd.print("Mosfet ON!");
digitalWrite(mosfet, HIGH);
// turn the MOSFET ON
delay(MOSFET_TIME); // wait for 4 second
//lcd.print("Mosfet OFF!");
digitalWrite(mosfet, LOW); // turn the MOSFET ON
}