//Bandiera Elettronica SoftAir Opposit Force ASD by Gianluca Ricci
#include <LiquidCrystal.h> //Include la libreria Display lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // inizializza la libreria con questi pin di riferimento
/* (RS, E, d4,d5,d6,d7)
pin lcd
1. VSS 5. R/W
2. VDD 6.E
3. V0 7--14. D0--D7
4- RS 15. Retro Illu+ 16. Retro Illu-
uscita 12 = pin lcd 5
uscita 11 = pin lcd 6
uscita 5 = pin lcd 11
uscita 4 = pin lcd 12
uscita 3 = pin lcd 13
uscita 2 = pin lcd 14
*/
#define ButtonPin0 18 //Modalità Bandiera e/o Modalità Punteggio switch
#define ButtonPin1 6 //Pulsante 1 pin 6
#define ButtonPin2 7 //Pulsante 2 pin 7
#define ButtonPin3 8 //Pulsante 3 pin 8
#define ButtonPin4 9 //Pulsante 4 pin 9
#define ButtonPin5 13 //Pulsante cambio pagina display
#define LedPin1 14 //Led 1 pin 14
#define LedPin2 15 //Led 2 pin 15
#define LedPin3 16 //Led 3 pin 16
//
#define LedPin4 17 //Led 4 pin 14 + pin 16
#define BuzzerPin 10 //Buzzer pin 10
int Button0 = 0;
int Button1 = 0;
int Button2 = 0;
int Button3 = 0;
int Button4 = 0;
int Button5 = 0;
int Clear = 0;
int Incremento1 = 1;
int Incremento2 = 1;
int Incremento3 = 1;
int Incremento4 = 1;
int Count1 = 0;
int Count2 = 0;
int Count3 = 0;
int Count4 = 0;
int Led1 = 0;
int Led2 = 0;
int Led3 = 0;
int Led4 = 0;
int inizio = 0;
int Blocco = 0;
int second = 0;
int a = 1;
byte Barra[8] = { // crea ■ nel display
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
byte Lucchetto[8] = { // crea il lucchetto nel display
B00100,
B01010,
B01010,
B11111,
B11011,
B11011,
B11111,
B01110,
};
void setup () {
lcd.begin(16, 2); //inizializza il display a 16x2
lcd.createChar(0, Barra); //inizializza la barra di avanzamento
lcd.createChar(1, Lucchetto); //inizializza il lucchetto per il blocco
lcd.clear(); //pulisci il display
pinMode(ButtonPin0, INPUT); //imposta il Buttone0 in input
pinMode(ButtonPin1, INPUT); //imposta il Buttone1 in input
pinMode(ButtonPin2, INPUT); //imposta il Buttone2 in input
pinMode(ButtonPin3, INPUT); //imposta il Buttone3 in input
pinMode(ButtonPin4, INPUT); //imposta il Buttone4 in input
pinMode(ButtonPin5, INPUT); //imposta il Buttone5 in input
pinMode(LedPin1, OUTPUT); //imposta L'uscita led in output
pinMode(LedPin2, OUTPUT); //imposta L'uscita led in output
pinMode(LedPin3, OUTPUT); //imposta L'uscita led in output
pinMode(LedPin4, OUTPUT); //imposta L'uscita led in output
pinMode(BuzzerPin, OUTPUT); //imposta L'uscita del buzzer in output
Incremento1 = 1; //imposta il valore della variabile
Incremento2 = 1; //imposta il valore della variabile
Incremento3 = 1; //imposta il valore della variabile
Incremento4 = 1; //imposta il valore della variabile
Led1 = 0; //imposta il valore della variabile
Led2 = 0; //imposta il valore della variabile
Led3 = 0; //imposta il valore della variabile
Led4 = 0; //imposta il valore della variabile
Blocco = 0; //imposta il valore della variabile
second = 120; //imposta il valore della variabile
Button0 = digitalRead(ButtonPin0); //legge il dato dall'ingresso
if (Button0 == LOW){
lcd.setCursor(0,0);
lcd.print("Modalita'");
lcd.setCursor(0,1);
lcd.print("Bandiera");
} else {
lcd.setCursor(0,0);
lcd.print("Modalita'");
lcd.setCursor(0,1);
lcd.print("Punteggio");
}
delay(5000); //blocca il programma per 5 sec
lcd.clear();
}
void loop () {
Button0 = digitalRead(ButtonPin0); //Legge lo stato del Pulsante 0
Button1 = digitalRead(ButtonPin1); //Legge lo stato del Pulsante 1
Button2 = digitalRead(ButtonPin2); //Legge lo stato del Pulsante 2
Button3 = digitalRead(ButtonPin3); //Legge lo stato del Pulsante 3
Button4 = digitalRead(ButtonPin4); //Legge lo stato del Pulsante 4
Button5 = digitalRead(ButtonPin5); //Legge lo stato del Pulsante 5
if (Button0 == LOW) {
Bandiera();
} else {
Punteggio();
}
if ((Button1 == HIGH) && (Button4 == HIGH)) {
lcd.clear();
lcd.print("Reset Totale");
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
Incremento1 = 1;
Incremento2 = 1;
Incremento3 = 1;
Incremento4 = 1;
Count1 = 0;
Count2 = 0;
Count3 = 0;
Count4 = 0;
inizio = 0;
Clear = 0;
a = 1;
lcd.begin(16, 2); //inizializza il display a 16x2
lcd.print("SQ.1 SQ.2 SQ.3"); //Scrivi nel display
lcd.setCursor(0,1);
lcd.print(Count1);
lcd.setCursor(6,1);
lcd.print(Count2);
lcd.setCursor(12,1);
lcd.print(Count3);
}
}
void Punteggio() {
if (Blocco == 0){
if (Button1 == HIGH) {
if (Led1 == 0) {
digitalWrite(LedPin1, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000); //Pausa di 2 Secondi
digitalWrite(BuzzerPin, LOW);
Count1++;
Led1 = 1;
Led2 = 0;
Led3 = 0;
Led4 = 0;
lcd.clear();
}
}
}
if (Blocco == 0){
if (Button2 == HIGH) {
if (Led2 == 0) {
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, LOW);
digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Count2++;
Led1 = 0;
Led2 = 1;
Led3 = 0;
Led4 = 0;
lcd.clear();
}
}
}
if (Blocco == 0){
if (Button3 == HIGH) {
if (Led3 == 0) {
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, HIGH);
digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Count3++;
Led1 = 0;
Led2 = 0;
Led3 = 1;
Led4 = 0;
lcd.clear();
}
}
}
if (Blocco == 0){
if (Button4 == HIGH) {
if (Led4 == 0) {
digitalWrite(LedPin1, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, HIGH);
digitalWrite(LedPin4, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Count4++;
Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 1;
lcd.clear();
}
}
}
if (second == 120) {
if (Button5 == LOW) {
lcd.setCursor(0,0);
lcd.print("SQ.1 SQ.2 SQ.3"); //Scrivi nel display
lcd.setCursor(0,1);
lcd.print(Count1);
lcd.setCursor(6,1);
lcd.print(Count2);
lcd.setCursor(12,1);
lcd.print(Count3);
Clear = 0;
} else {
if (Clear == 0) {
lcd.clear();
Clear = 1;
}
lcd.setCursor(0,0);
lcd.print("SQ.4"); //Scrivi nel display
lcd.setCursor(0,1);
lcd.print(Count4);
}
}
static unsigned long lastTick = 0; // set up a local variable to hold the last time we decremented one second
if ((Led1 != 0) || (Led2 != 0) || (Led3 != 0) || (Led4 != 0)) {
if (second > 0) {
if (millis() - lastTick >= 1000) {
lastTick = millis();
second--;
lcd.setCursor(0,0);
lcd.print("Ripristino tra: ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(second);
lcd.setCursor(4,1);
lcd.print("Sec.");
Blocco = 1;
lcd.setCursor(9,1);
lcd.write(1);
}
if (second == 0) {
lcd.clear();
second = 120;
digitalWrite(BuzzerPin, HIGH);
delay (500);
digitalWrite(BuzzerPin, LOW);
Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 0;
Blocco = 0;
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
//digitalWrite(LedPin4, LOW);
}
}
}
}
void Bandiera() {
Serial.print("Pulsici: "),
Serial.println(Clear);
if ((Button1 != LOW) || (Button2 != LOW) || (Button3 != LOW) || (Button4 != LOW)) {
if (Clear == 0){
lcd.clear();
Clear = 1;
}
Blocco = 1;
lcd.setCursor(0,0);
lcd.print("In Acquisizione..");
lcd.setCursor(0,1);
lcd.print("[");
lcd.setCursor(11,1);
lcd.print("]");
lcd.setCursor(a++,1);
//lcd.write(0);
}
if ((Button1 == LOW) && (Button2 == LOW) && (Button3 == LOW) && (Button4 == LOW)) {
a = 1 ;
Clear = 0;
Blocco = 0;
if ((Led1 == LOW) && (Led2 == LOW) && (Led3 == LOW) && (Led4 == LOW)){
lcd.setCursor(0,0);
lcd.print("Band. Libera ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
if (Button1 == HIGH) {
if (Led1 == 0) {
if (Incremento1 == 10) {
digitalWrite(LedPin1, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
//digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000); //Pausa di 2 Secondi
digitalWrite(BuzzerPin, LOW);
Incremento1 = 1;
lcd.clear();
Led1 = 1;
Led2 = 0;
Led3 = 0;
Led4 = 0;
a = 1;
Clear = 0;
}
else {
Incremento1++; //Incrementa la variabile
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(500); //Pausa di 0.5 secondi
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(BuzzerPin, LOW);
delay(500);
}
}
} else {
Incremento1 = 1;
digitalWrite(BuzzerPin, LOW);
}
if (Button2 == HIGH) {
if (Led2 == 0) {
if (Incremento2 == 10) {
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, LOW);
//digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Incremento2 = 1;
lcd.clear();
Led1 = 0;
Led2 = 1;
Led3 = 0;
Led4 = 0;
a = 1;
Clear = 0;
}
else {
Incremento2++;
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(500); //Pausa di 0.5 secondi
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(BuzzerPin, LOW);
delay(500);
}
}
} else {
Incremento2 = 1;
digitalWrite(BuzzerPin, LOW);
}
if (Button3 == HIGH) {
if (Led3 == 0) {
if (Incremento3 == 10) {
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, HIGH);
//digitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Incremento3 = 1;
lcd.clear();
Led1 = 0;
Led2 = 0;
Led3 = 1;
Led4 = 0;
a = 1;
Clear = 0;
}
else {
Incremento3++;
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(500); //Pausa di 0.5 secondi
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
digitalWrite(BuzzerPin, LOW);
delay(500);
}
}
} else {
Incremento3 = 1;
digitalWrite(BuzzerPin, LOW);
}
if (Button4 == HIGH) {
if (Led4 == 0) {
if (Incremento4 == 10) {
digitalWrite(LedPin1, HIGH);
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, HIGH);
//digitalWrite(LedPin4, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(2000);
digitalWrite(BuzzerPin, LOW);
Incremento4 = 1;
lcd.clear();
Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 1;
a = 1;
Clear = 0;
}
else {
Incremento4++;
digitalWrite(LedPin2, HIGH);
digitalWrite(LedPin3, HIGH);
//digitalWrite(LedPin4, HIGH);
digitalWrite(BuzzerPin, HIGH);
delay(500); //Pausa di 0.5 secondi
digitalWrite(LedPin2, LOW);
digitalWrite(LedPin3, LOW);
//igitalWrite(LedPin4, LOW);
digitalWrite(BuzzerPin, LOW);
delay(500);
}
}
} else {
Incremento4 = 1;
digitalWrite(BuzzerPin, LOW);
}
if (Blocco == 0) {
if (Led1 == 1) {
lcd.setCursor(0,0);
lcd.print("Band. Acqusita ");
lcd.setCursor(0,1);
lcd.print("Squadra 1 ");
}
if (Led2 == 1) {
lcd.setCursor(0,0);
lcd.print("Band. Acqusita ");
lcd.setCursor(0,1);
lcd.print("Squadra 2 ");
}
if (Led3 == 1) {
lcd.setCursor(0,0);
lcd.print("Band. Acqusita ");
lcd.setCursor(0,1);
lcd.print("Squadra 3 ");
}
if (Led4 == 1) {
lcd.setCursor(0,0);
lcd.print("Band. Acqusita ");
lcd.setCursor(0,1);
lcd.print("Squadra 4 ");
}
}
}