#include #include #include #include #include LiquidCrystal_I2C lcd(0x27, 16, 2); #define LED_PIN 6 #define NUM_LEDS 16 #define BRIGHTNESS 32 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; #define SENZOR_PIN1 5 #define BUTTON_PIN1 12 #define BUTTON_PIN2 13 #define BUTTON_PIN3 9 #define BUTTON_PIN4 8 #define CLK 3 #define DIO 4 #define BUZZER 11 CRGBPalette16 currentPalette; TBlendType currentBlending; TM1637Display display(CLK, DIO); int buzz_first = 0; int player1_score = 0; int player2_score = 0; int turn = 0; int game_mode = 0; int start = 0; int game_over = 0; int Minutes = 0; int Seconds = 0; int Set_Minutes = 0; int Set_Seconds = 40; uint8_t data[] = {0, 0, 0, 0}; unsigned long previousMillis = 0; unsigned long interval = 1000; void setup() { // led ring delay( 3000 ); FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; Serial.begin(115200); // senzor pinMode(SENZOR_PIN1, INPUT); // button pinMode(BUTTON_PIN1, INPUT_PULLUP); pinMode(BUTTON_PIN2, INPUT_PULLUP); pinMode(BUTTON_PIN3, INPUT_PULLUP); pinMode(BUTTON_PIN4, INPUT_PULLUP); // buzzer pinMode(BUZZER, OUTPUT); // lcd lcd.begin(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("Time->black button"); lcd.setCursor(0, 1); lcd.print("Yellow to start"); lcd.setCursor(0, 2); lcd.print("Time "); lcd.print(Set_Minutes); lcd.print(":");lcd.print(Set_Seconds); } void loop() { bool currentState1 = digitalRead(BUTTON_PIN1); bool currentState2 = digitalRead(BUTTON_PIN2); // setare timp + start if (start == 0){ bool currentState3 = digitalRead(BUTTON_PIN3); bool currentState4 = digitalRead(BUTTON_PIN4); if (currentState3 == false){ start = 1; lcd.clear(); lcd.setCursor(3, 0); lcd.print("Press to start"); lcd.setCursor(0, 1); lcd.print("Blue: 2 players"); lcd.setCursor(0, 2); lcd.print("Red: 1 player"); while(digitalRead(BUTTON_PIN3) == false) { } } if (currentState4 == false){ if (Set_Seconds == 40){ Set_Seconds = 0; Set_Minutes = Set_Minutes + 1; }else{ Set_Seconds = Set_Seconds + 20; } if (Set_Minutes >= 10){ Set_Minutes = 0; Set_Seconds = 40; } lcd.clear(); lcd.setCursor(0, 0); lcd.print("Time->black button"); lcd.setCursor(0, 1); lcd.print("Yellow to start"); lcd.setCursor(0, 2); lcd.print("Time "); lcd.print(Set_Minutes); lcd.print(":");lcd.print(Set_Seconds); delay(100); while(digitalRead(BUTTON_PIN4) == false) { } } } // setare mod de joc if (game_mode == 0 && start == 1){ if (currentState1 == false){ lcd.clear(); lcd.setCursor(2, 1); lcd.print("Multiplayer start"); lcd.setCursor(0, 2); lcd.print("blue - player1"); lcd.setCursor(0, 3); lcd.print("red -player2"); game_mode = 1; Minutes =0; Seconds =0; } if (currentState2 == false){ game_mode = 2; lcd.clear(); lcd.setCursor(2, 1); lcd.print("Singleplayer start"); Minutes = Set_Minutes; Seconds = Set_Seconds; } currentPalette = PartyColors_p; static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.show(); } // multiplayer if (game_mode == 1){ multiplayer_game_mode(currentState1,currentState2); } // singleplayer if (game_mode == 2){ singleplayer_game_mode(currentState1,currentState2); } } void multiplayer_game_mode(bool currentState1, bool currentState2){ if (currentState1 == false && game_over == 0) { turn = 1; lcd.clear(); lcd.setCursor(3, 0); lcd.print("Player1's turn"); lcd.setCursor(0, 1); lcd.print("Player1 score:"); lcd.print(player1_score); lcd.setCursor(0, 2); lcd.print("Player2 score:");lcd.print(player2_score); while(digitalRead(BUTTON_PIN1) == false) { } } if (currentState2 == false && game_over == 0) { turn = 2; lcd.clear(); lcd.setCursor(3, 0); lcd.print("Player2's turn"); lcd.setCursor(0, 1); lcd.print("Player1 score:"); lcd.print(player1_score); lcd.setCursor(0, 2); lcd.print("Player2 score:");lcd.print(player2_score); while(digitalRead(BUTTON_PIN2) == false) { } } if (digitalRead(SENZOR_PIN1) == 0 && turn == 1 && game_over == 0){ player1_score ++; turn = 2; digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); lcd.clear(); lcd.setCursor(3, 0); lcd.print("Player2's turn"); lcd.setCursor(0, 1); lcd.print("Player1 score:"); lcd.print(player1_score); lcd.setCursor(0, 2); lcd.print("Player2 score:");lcd.print(player2_score); delay(1500); } if (digitalRead(SENZOR_PIN1) == 0 && turn == 2 && game_over == 0){ player2_score ++; turn = 1; digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); lcd.clear(); lcd.setCursor(3, 0); lcd.print("Player1's turn"); lcd.setCursor(0, 1); lcd.print("Player1 score:"); lcd.print(player1_score); lcd.setCursor(0, 2); lcd.print("Player2 score:");lcd.print(player2_score); delay(1500); } static uint8_t startIndex = 0; if (turn == 1 && game_over == 0){ SetupBluePalette(); } else if (turn == 2 && game_over == 0){ SetupRedPalette(); } else if (player1_score < player2_score && game_over== 1){ SetupRedPalette(); } else if (player1_score > player2_score && game_over== 1){ SetupBluePalette(); } else { startIndex = startIndex + 1; SetupPurpleAndGreenPalette(); } FillLEDsFromPaletteColors(startIndex); FastLED.show(); unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval && game_over == 0) { previousMillis = currentMillis; display.setBrightness(0x0f); data[3] = display.encodeDigit(Seconds % 10); data[2] = display.encodeDigit(Seconds / 10); data[1] = display.encodeDigit(Minutes % 10); data[0] = display.encodeDigit(Minutes / 10); display.setSegments(data); uint8_t segto; segto = 0x80 | display.encodeDigit(Minutes % 10); display.setSegments(&segto, 1, 1); stepUp(); } } void singleplayer_game_mode(bool currentState1, bool currentState2){ if (game_over == 1){ currentPalette = RainbowColors_p; static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.show(); }else{ SetupBluePalette(); FillLEDsFromPaletteColors(0); FastLED.show(); } if (digitalRead(SENZOR_PIN1) == 0 && game_over == 0){ player1_score ++; digitalWrite(BUZZER,HIGH); delay(100); digitalWrite(BUZZER,LOW); lcd.clear(); lcd.setCursor(0, 1); lcd.print("Current score:"); lcd.print(player1_score); delay(1000); } unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; display.setBrightness(0x0f); data[3] = display.encodeDigit(Seconds % 10); data[2] = display.encodeDigit(Seconds / 10); data[1] = display.encodeDigit(Minutes % 10); data[0] = display.encodeDigit(Minutes / 10); display.setSegments(data); uint8_t segto; segto = 0x80 | display.encodeDigit(Minutes % 10); display.setSegments(&segto, 1, 1); stepDown(); } } void stepDown() { if (Seconds > 0) { Seconds -= 1; } else { if (Minutes > 0) { Seconds = 59; Minutes -= 1; } else { game_over = 1; if (buzz_first == 0){ buzz_first = 1; digitalWrite(BUZZER,HIGH); delay(300); digitalWrite(BUZZER,LOW); } const uint8_t SEG_GAME[] = { SEG_A | SEG_C | SEG_D | SEG_F | SEG_G | SEG_E , // G SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // A SEG_A | SEG_B | SEG_C | SEG_E | SEG_F, // M SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E }; const uint8_t SEG_OVER[] = { SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O SEG_F | SEG_E | SEG_D | SEG_C | SEG_B, // V SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E SEG_E | SEG_F | SEG_A | SEG_B | SEG_C | SEG_G // R }; display.setSegments(SEG_GAME); delay (500); display.setSegments(SEG_OVER); } } } void stepUp() { Seconds += 1; if (Minutes >= Set_Minutes && Seconds >=Set_Seconds ) { Minutes = Set_Minutes; Seconds = Set_Seconds; game_over = 1; if (buzz_first == 0){ buzz_first = 1; digitalWrite(BUZZER,HIGH); delay(300); digitalWrite(BUZZER,LOW); } const uint8_t SEG_GAME[] = { SEG_A | SEG_C | SEG_D | SEG_F | SEG_G | SEG_E , // G SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, // A SEG_A | SEG_B | SEG_C | SEG_E | SEG_F, // M SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E }; const uint8_t SEG_OVER[] = { SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O SEG_F | SEG_E | SEG_D | SEG_C | SEG_B, // V SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E SEG_E | SEG_F | SEG_A | SEG_B | SEG_C | SEG_G // R }; display.setSegments(SEG_GAME); delay (500); display.setSegments(SEG_OVER); lcd.clear(); if (player1_score == player2_score){ lcd.setCursor(1, 1); lcd.print("IT'S A TIE !"); }else if (player1_score > player2_score){ lcd.setCursor(1, 1); lcd.print("PLAYER 1 WINS !"); }else { lcd.setCursor(1, 1); lcd.print("PLAYER 2 WINS !"); } } else if (Seconds >= 60) { Seconds = 0; Minutes += 1; } } void SetupPurpleAndGreenPalette() { CRGB purple = CHSV( HUE_PURPLE, 255, 255); CRGB green = CHSV( HUE_GREEN, 255, 255); CRGB black = CRGB::Black; currentPalette = CRGBPalette16( green, green, black, black, purple, purple, black, black, green, green, black, black, purple, purple, black, black ); } void SetupRedPalette() { CRGB red = CHSV( HUE_RED, 255, 255); CRGB black = CRGB::Black; currentPalette = CRGBPalette16( red, black, red, black, red, black, red, black, red, black, red, black, red, black, red, black ); } void SetupBluePalette() { CRGB black = CRGB::Black; CRGB blue = CHSV( HUE_BLUE, 255, 255); currentPalette = CRGBPalette16( black, blue, black,blue, black, blue, black,blue, black, blue, black,blue, black, blue, black,blue); } void FillLEDsFromPaletteColors( uint8_t colorIndex) { uint8_t brightness = 255; for( int i = 0; i < NUM_LEDS; i++) { leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); colorIndex += 3; } }