//************ Set Library ***********
#include <Wire.h>
//#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
//************************************
// Set the LCD address to 32 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);
//*****************************************************************
//********************** Set Arduino Nano Pins **********************************************************
#define LEDPIN 13 // Nano - The pin that the LED Integrated into Arduino is attached to
#define BLUE_SENSORPIN 2 // Nano - The pin that the pushbutton is attached to
//#define RED_SENSORPIN 3 // Nano - The pin that the pushbutton is attached to
#define RED_SENSORPIN 0 // ESP32 - The pin that the pushbutton is attached to
#define LED_STRIP_PIN 4 // Nano - The pin that the LED Strip is attached to
// Nano - Set PIN A5 for comunication between SCL (serial clock) and the LiquidCrystal_I2C SCL
// Nano - Set PIN A4 for comunication between SDA (serial data) and the LiquidCrystal_I2C SAD
// ESP32 - Set PIN GPIO22 for comunication between SCL (serial clock) and the LiquidCrystal_I2C SCL
// ESP32 - Set PIN GPIO21 for comunication between SDA (serial data) and the LiquidCrystal_I2C SAD
//******************************************************************************************************
/*
// LEDS
#define BRIGHTNESS 100
// How many leds are in the strip?
//#define FIRST_STRIP_NUM_LEDS 6
#define FIRST_STRIP_PIN 4
CRGB first_leds[FIRST_STRIP_NUM_LEDS];
// lado oposto moedeiro
//#define SECOND_STRIP_NUM_LEDS 6
#define SECOND_STRIP_PIN 5
CRGB second_leds[SECOND_STRIP_NUM_LEDS];
*/
//************************Set LED STRIPS *************************************
#define NUMPIXELS 6 // 6 LEDs because they are connected in parallel
#define BRIGHTNESS 180
const int DELAYVAL = 100;
Adafruit_NeoPixel neopixels(NUMPIXELS, LED_STRIP_PIN, NEO_GRB + NEO_KHZ800);
//************************* Set Colours *******************************
uint32_t Purple_On = neopixels.Color(100, 0, 255);
uint32_t Gray_On = neopixels.Color(128, 128, 128);
uint32_t Red_On = neopixels.Color(255, 0, 0);
uint32_t Cyan_On = neopixels.Color(0, 255, 255);
uint32_t Blue_On = neopixels.Color(0, 0, 255);
uint32_t Green_On = neopixels.Color(0, 255, 0);
uint32_t Yellow_On = neopixels.Color(255, 255, 0);
uint32_t Magenta_On = neopixels.Color(255, 0, 255);
uint32_t Led_Off = neopixels.Color(0, 0, 0);
//****************************************************************************
//*************** Set Varibles ****************
String goal = "GOAL GOAL GOAL!!";
String message1 = "Welcome!!";
String message2 = "Are you ready?";
String message3 = ".....Let's......";
String message4 = "Start the Game!!";
int text_length = goal.length();
String RED_TEAM = "SL Benfica:"; // Set Team Red (Red LED Strip)
String BLUE_TEAM = "FC Porto :"; // Set Team Blue (Blue LED Strip)
//String RED_TEAM = "Red Team..:";
//String BLUE_TEAM = "Blue Team.:";
// Variables will change:
// Variable for goals counter
int red_goals = 0, blue_goals = 0;
int red_wins = 0, blue_wins = 0;
// Variable for reading the pushbutton state
int redSensorState = 0, redLastState = 0;
int blueSensorState = 0, blueLastState = 0;
//*******************************************
void setup() {
// Initialize brightness & neopixels:
neopixels.setBrightness(BRIGHTNESS);
neopixels.begin();
//Initialize serial comunication:
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
// Initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// Initialize the sensor pin as an input:
pinMode(RED_SENSORPIN, INPUT);
// digitalWrite(RED_SENSORPIN, LOW); // turn on the pullup
// Initialize the sensor pin as an input:
pinMode(BLUE_SENSORPIN, INPUT);
// digitalWrite(BLUE_SENSORPIN, LOW); // turn on the pullup
// Initialize the LED pin as an output:
pinMode(LED_STRIP_PIN, OUTPUT);
print_message();
blink();
splashScreen();
printScore();
Led_Strip_On();
}
void loop() {
// read the state of the pushbutton value:
redSensorState = digitalRead(RED_SENSORPIN);
blueSensorState = digitalRead(BLUE_SENSORPIN);
digitalWrite(LEDPIN, LOW);
//Compare the redSensorState to its previus state
if (redSensorState == HIGH && redSensorState != redLastState) {
Serial.println("Red Broken");
digitalWrite(LEDPIN, HIGH);
RedLed();
Led_Strip_On();
printGoal();
Scroll_Display();
// If the state has changed , increment the counter
red_goals++;
check_Goals();
delay(50); // Delay a little bit to avoid bouncing
}
//Compare the blueSensorState to its previus state
if (blueSensorState == HIGH && blueSensorState != blueLastState) {
Serial.println("Blue Broken");
digitalWrite(LEDPIN, HIGH);
BlueLed();
Led_Strip_On();
printGoal();
Scroll_Display();
// If the state has changed , increment the counter
blue_goals++;
check_Goals();
delay(50); // Delay a little bit to avoid bouncing
}
redLastState = redSensorState;
blueLastState = blueSensorState;
}
/****************** Funtions LCD ***********************/
void print_message() {
lcd.setCursor(4,0);
lcd.print(message1);
lcd.setCursor(1,1);
lcd.print(message2);
delay(2000);
}
void blink() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(message3);
lcd.setCursor(0,1);
lcd.print(message4);
delay(300);
for (int a = 0; a < 5; a++) {
lcd.noDisplay();
delay(300);
lcd.display();
delay(300);
delay(25); // Delay a little bit to improve simulation performance
}
}
void splashScreen() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 16; j++) {
// lcd.clear();
lcd.setCursor(j, i);
lcd.print("/");
delay(80);
}
}
}
void check_Goals() {
printScore();
if (red_goals + blue_goals == 9) {
delay(1000);
if (red_goals > blue_goals) {
red_wins++;
}
else {
blue_wins++;
}
red_goals = 0;
blue_goals = 0;
printScore();
}
if (red_wins == 5) {
lcd.clear();
lcd.setCursor(1,0);
lcd.print("The WINNER is:");
lcd.setCursor(1,1);
lcd.print("* SL BENFICA *");
red_wins = 0;
blue_wins = 0;
delay(1000);
Scroll_Display();
delay(1000);
printScore();
}
if (blue_wins == 5) {
lcd.clear();
lcd.setCursor(1,0);
lcd.print("The WINNER is:");
lcd.setCursor(1,1);
lcd.print("** FC PORTO **");
red_wins = 0;
blue_wins = 0;
delay(1000);
Scroll_Display();
delay(1000);
printScore();
}
}
void printScore() {
//*** Show RED TEAM info
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(RED_TEAM);
lcd.setCursor(12, 0);
lcd.print(red_goals);
lcd.setCursor(15, 0);
lcd.print(red_wins);
//*** Show BLUE TEAM info
lcd.setCursor(0, 1);
lcd.print(BLUE_TEAM);
lcd.setCursor(12, 1);
lcd.print(blue_goals);
lcd.setCursor(15, 1);
lcd.print(blue_wins);
}
void printGoal() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(goal);
lcd.setCursor(0, 1);
lcd.print(goal);
delay(1000);
}
void Scroll_Display() {
// Moves the text to the left as many times as its length
for (int position = 0; position < text_length; position++) {
lcd.scrollDisplayLeft();
delay(150);
// delay(300);
}
// Moves the text to the right as many times as its length
for (int position = 0; position < (16 + text_length); position++) {
lcd.scrollDisplayRight();
delay(150);
// delay(300);
}
// Move the text to the left until it is in its initial position
for (int position = 0; position < 16; position++) {
lcd.scrollDisplayLeft();
delay(150);
}
// delay(1000);
}
/****************** Funtions LED Strip ***********************/
void Led_Strip_On(){
for (int i = 0; i < NUMPIXELS; i++) {
neopixels.setPixelColor(i, Purple_On);
neopixels.show();
delay(DELAYVAL);
}
// neopixels.show();
/*
for (int i = 0; i < NUMPIXELS; i++) {
neopixels.setPixelColor(i, Led_Off);
neopixels.show();
delay(100);
}
*/
}
void RedLed() {
// neopixels.clear();
for (int j = 0; j < 3; j++){
for (int i = 0; i < NUMPIXELS; i++) {
neopixels.setPixelColor(i, Red_On);
neopixels.show();
delay(DELAYVAL);
}
for (int i = NUMPIXELS - 1; i >= 0; i--) {
neopixels.setPixelColor(i, Led_Off);
neopixels.show();
delay(DELAYVAL);
}
}
}
void BlueLed() {
// neopixels.clear();
for (int j = 0; j < 3; j++){
for (int i = 0; i < NUMPIXELS; i++) {
neopixels.setPixelColor(i, Blue_On);
neopixels.show();
delay(DELAYVAL);
}
delay(300);
for (int i = NUMPIXELS - 1; i >= 0; i--) {
neopixels.setPixelColor(i, Led_Off);
neopixels.show();
delay(DELAYVAL);
}
}
}