//https://forum.arduino.cc/t/duvidas-em-codigo-projecto-placar-desportivo/1233497
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 5
LCD D5 pin to digital pin 4
LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
LCD R/W pin to ground
LCD VSS pin to ground
LCD VCC pin to 5V
10K resistor:
ends to +5V and ground
wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#define LEDPIN 13
#define HOME_SENSORPIN 7
#define AWAY_SENSORPIN 4
// LEDS
#define BRIGHTNESS 80
// lado do moedeiro
#define FIRST_STRIP_NUM_LEDS 35
#define FIRST_STRIP_PIN 10
CRGB first_leds[FIRST_STRIP_NUM_LEDS];
// lado oposto moedeiro, tem mais 1 led
#define SECOND_STRIP_NUM_LEDS 36
#define SECOND_STRIP_PIN 11
CRGB second_leds[SECOND_STRIP_NUM_LEDS];
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char HOME_TEAM[] = "BEIRA MAR ";
const char AWAY_TEAM[] = "ACADEMICA ";
int home_goals = 0;
int away_goals = 0;
int home_wins = 0;
int away_wins = 0;
// variables will change:
int homeSensorState = 0, homeLastState = 0; // variable for reading the pushbutton status
int awaySensorState = 0, awayLastState = 0; // variable for reading the pushbutton status
//-------------------------------------------------------------
void flashYellow() {
int i = 0;
for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
first_leds[i] = CRGB::Yellow;
}
for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
second_leds[i] = CRGB::Yellow;
}
FastLED.show();
}
//-------------------------------------------------------------
void flashGray() {
int i = 0;
for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
first_leds[i] = CRGB::Gray;
}
for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
second_leds[i] = CRGB::Gray;
}
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 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(" ");
}
// separate between info
lcd.print(" ");
lcd.print(home_wins);
// 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(" ");
}
// separate between info
lcd.print(" ");
lcd.print(away_wins);
}
//-------------------------------------------------------------
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);
flashYellow();
delay(150);
flashBlack();
}
//-------------------------------------------------------------
void blinkAway() {
Serial.println("blinkAway");
flashBlack();
delay(150);
flashGray();
delay(150);
flashBlack();
}
//-------------------------------------------------------------
void printGoal() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" GOLO GOLO GOLO!!");
lcd.setCursor(0, 1);
lcd.print("GOLO GOLO GOLO !");
delay(200);
// lcd.clear();
// delay(100);
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("GOLO GOLO GOLO!!");
// lcd.setCursor(0, 1);
// lcd.print(" GOLO GOLO GOLO !");
// delay(200);
}
//-------------------------------------------------------------
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
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);
flashGray();
Serial.begin(9600);
}
void loop() {
// Turn the LED on, then pause
// first_leds[0] = CRGB::Gray;
// first_leds[1] = CRGB::Yellow;
// first_leds[2] = CRGB::Green;
// FastLED.show();
// delay(500);
// // Now turn the LED off, then pause
// first_leds[0] = CRGB::Black;
// first_leds[1] = CRGB::Black;
// first_leds[2] = CRGB::Black;
// FastLED.show();
// delay(500);
//delay(2000);
//home_goals++;
//printScore();
// read the state of the pushbutton value:
homeSensorState = digitalRead(HOME_SENSORPIN);
awaySensorState = digitalRead(AWAY_SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (homeSensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}
if (awaySensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}
if (homeSensorState && !homeLastState) {
Serial.println("home Unbroken");
}
if (!homeSensorState && homeLastState) {
Serial.println("home Broken");
printGoal();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
blinkHome();
flashGray();
home_goals++;
printScore();
if (home_goals + away_goals == 9) {
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");
printGoal();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
blinkAway();
flashGray();
away_goals++;
printScore();
if (home_goals + away_goals == 9) {
if (away_goals > home_goals) {
away_wins++;
}
else {
home_wins++;
}
away_goals = 0;
home_goals = 0;
delay(2000);
printScore();
}
}
homeLastState = homeSensorState;
awayLastState = awaySensorState;
}