#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#define LEDPIN 13
#define HOME_SENSORPIN 7
#define AWAY_SENSORPIN 4
#define dcasa 8
#define dfora 2
#define BRIGHTNESS 80
#define FIRST_STRIP_NUM_LEDS 35
#define FIRST_STRIP_PIN 10
CRGB first_leds[FIRST_STRIP_NUM_LEDS];
#define SECOND_STRIP_NUM_LEDS 36
#define SECOND_STRIP_PIN 11
CRGB second_leds[SECOND_STRIP_NUM_LEDS];
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char HOME_TEAM[] = "SPORTING ";
const char AWAY_TEAM[] = "BENFICA ";
int home_goals = 0;
int away_goals = 0;
int home_wins = 0;
int away_wins = 0;
int homeSensorState = 0, homeLastState = 0; // variable for reading the pushbutton status
int awaySensorState = 0, awayLastState = 0; // variable for reading the pushbutton status
int dcasaState = 0, dcasaLastState = 0;
int dforaState = 0, dforaLastState = 0;
//-------------------------------------------------------------
void flashGreen() {
int i = 0;
for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
first_leds[i] = CRGB::Green;
}
for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
second_leds[i] = CRGB::Green;
}
FastLED.show();
}
//-------------------------------------------------------------
void flashBlack() {
int i = 0;
for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
first_leds[i] = CRGB::Black;
}
for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
second_leds[i] = CRGB::Black;
}
FastLED.show();
}
//-------------------------------------------------------------
void flashRed() {
int i = 0;
for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
first_leds[i] = CRGB::Red;
}
for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
second_leds[i] = CRGB::Red;
}
FastLED.show();
}
//-------------------------------------------------------------
void printScore() {
// Write home info
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(HOME_TEAM);
if (home_goals < 10) {
lcd.print(" ");
}
lcd.print(home_goals);
if (home_wins < 10) {
lcd.print(" ");
}
// Write away info
lcd.setCursor(0, 1);
lcd.print(AWAY_TEAM);
if (away_goals < 10) {
lcd.print(" ");
}
lcd.print(away_goals);
if (away_wins < 10) {
lcd.print(" ");
}
}
//-------------------------------------------------------------
void splashScreen() {
int i = 0;
int j = 0;
for (i = 0; i < 2; i++) {
for (j = 0; j < 16; j++) {
//lcd.clear();
lcd.setCursor(j, i);
lcd.print("-");
delay(25);
}
}
}
//-------------------------------------------------------------
void blinkHome() {
Serial.println("blinkHome");
flashBlack();
delay(150);
flashGreen();
delay(150);
flashBlack();
}
//-------------------------------------------------------------
void blinkAway() {
Serial.println("blinkAway");
flashRed();
delay(150);
flashBlack();
delay(150);
flashRed();
}
//-------------------------------------------------------------
void printGoalHOME() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GOLO GOLO GOLO");
lcd.setCursor(0, 1);
lcd.print(" SPORTING ");
delay(1100);
}
void printGoalAWAY() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GOLO GOLO GOLO");
lcd.setCursor(0, 1);
lcd.print(" BENFICA ");
delay(1100);
}
void printGoalmenos() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" INVALIDO ");
lcd.setCursor(0, 1);
lcd.print(" ANULADO ");
delay(500);
}
//-------------------------------------------------------------
void setup() {
// set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
// Print a message to the LCD.
//lcd.print("hello, world!");
splashScreen();
printScore();
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(HOME_SENSORPIN, INPUT);
digitalWrite(HOME_SENSORPIN, HIGH); // turn on the pullup
pinMode(AWAY_SENSORPIN, INPUT);
digitalWrite(AWAY_SENSORPIN, HIGH); // turn on the pullup
pinMode(dcasa, INPUT);
digitalWrite(dcasa, HIGH);
pinMode(dfora, INPUT);
digitalWrite(dfora, HIGH);
FastLED.addLeds<NEOPIXEL, FIRST_STRIP_PIN>(first_leds, FIRST_STRIP_NUM_LEDS);
FastLED.addLeds<NEOPIXEL, SECOND_STRIP_PIN>(second_leds, SECOND_STRIP_NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
flashBlack();
Serial.begin(9600);
}
void loop() {
homeSensorState = digitalRead(HOME_SENSORPIN);
awaySensorState = digitalRead(AWAY_SENSORPIN);
dcasaState = digitalRead(dcasa);
dforaState = digitalRead(dfora);
if (homeSensorState == LOW) {
digitalWrite(LEDPIN, HIGH);
}
else {
digitalWrite(LEDPIN, LOW);
}
if (awaySensorState == LOW) {
digitalWrite(LEDPIN, HIGH);
}
else {
digitalWrite(LEDPIN, LOW);
}
if (homeSensorState && !homeLastState) {
Serial.println("home Unbroken");
}
if (!homeSensorState && homeLastState) {
Serial.println("home Broken");
printGoalHOME();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
flashBlack();
home_goals++;
printScore();
if (home_goals == 5) {
if (home_goals > away_goals) {
home_wins++;
}
else {
away_wins++;
}
home_goals = 0;
away_goals = 0;
delay(2000);
printScore();
}
}
if (dcasaState == LOW) {
digitalWrite(LEDPIN, HIGH);
}
else {
digitalWrite(LEDPIN, LOW);
}
if (dforaState == LOW) {
digitalWrite(LEDPIN, HIGH);
}
else {
digitalWrite(LEDPIN, LOW);
}
if (dcasaState && !dcasaLastState) {
Serial.println("home Unbroken");
}
if (!dcasaState && dcasaLastState) {
Serial.println("home Broken");
printGoalmenos();
home_goals--;
printScore();
if (home_goals == 5) {
if (home_goals > away_goals) {
home_wins++;
}
else {
away_wins++;
}
home_goals = 0;
away_goals = 0;
delay(2000);
printScore();
}
}
if (awaySensorState && !awayLastState) {
Serial.println("away Unbroken");
}
if (!awaySensorState && awayLastState) {
Serial.println("away Broken");
printGoalAWAY();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
flashBlack();
away_goals++;
printScore();
if (away_goals == 5) {
if (away_goals > home_goals) {
away_wins++;
}
else {
home_wins++;
}
away_goals = 0;
home_goals = 0;
delay(2000);
printScore();
}
}
if (dforaState && !dforaLastState) {
Serial.println("away Unbroken");
}
if (!dforaState && dforaLastState) {
Serial.println("away Broken");
printGoalmenos();
away_goals--;
printScore();
if (away_goals == 5) {
if (away_goals > home_goals) {
away_wins++;
}
else {
home_wins++;
}
away_goals = 0;
home_goals = 0;
delay(2000);
printScore();
}
}
homeLastState = homeSensorState;
awayLastState = awaySensorState;
dcasaLastState = dcasaState;
dforaLastState = dforaState;
}