/******************************************************************
// Project Title: Counter from 0 to 9 with 7-Segments Display
// Date: 07.03.2024
// Author: Jose Baptista
// Parts List:
// - Arduino
//
//
//
// Descrition:
*/
/*/++++++ Set ESP32 Pins++++++/*/
#define clockpin 17 //Cuando hay que leer los bit SH
#define data 4 //Envio datos DS
#define latch 16 //indica pin de salida en el chip ST
#define clockpin1 0 //Cuando hay que leer los bit SH
#define data1 15 //Envio datos DS
#define latch1 2 //indica pin de salida en el chip ST
// no cambiar el const int
#define coin_sensorpin 19 // pin 4 como entrada para el coin
//#define relay 21 // pin 12 como salida para el relay
#define red_sensorpin 5 // pin 2 como entrada para el home_sensor
#define blue_sensorpin 18 // pin 3 como entrada para el visitor_sensor
//red = home
//blue = visitor
//+++++++++ variables will change:
int count = 0, count1 = 0;
boolean go = false, sw = false;
const int debouncetime = 10; // time in ms to avoid bouncing
// Variable for reading the pushbutton status
//+++++++++ variables will change:
int coinSensorState = 0, lastCoinState = 0;
int redSensorState = 0, redLastState = 0;
int blueSensorState = 0, blueLastState = 0;
//+++++++++++++++++++++++++++++++
//Números enteros en Binario - 7 segmentos Catodo
/*
AAAAA
F B
F B
F B
GGGGG
E D
E D
E D
CCCCC X
*/
// X = Decimal Point
// ABCDEFGX
// 11111111 = 8.
// Codificación hexadecimal de los dígitos decimales en el display de 7 segmentos Catodo
//0xEE, 0x82, 0xDC, 0xD6, 0xB2, 0x76, 0x7E, 0xC2, 0xFE, 0xF6};
byte number[]= {
B11111100, //0
B01100000, //1
B11011010, //2
B11110010, //3
B01100110, //4
B10110110, //5
B10111110, //6
B11100000, //7
B11111110, //8
B11110110}; //9
//Números enteros en Binario - 7 segmentos Anodo
/*
AAAAA
F B
F B
F B
GGGGG
E D
E D
E D
CCCCC X
*/
// X = Decimal Point
// ABCDEFGX
// 00000000 = 8.
// Codificación hexadecimal de los dígitos decimales en el display de 7 segmentos Anodo
//0xEE, 0x82, 0xDC, 0xD6, 0xB2, 0x76, 0x7E, 0xC2, 0xFE, 0xF6};
/*
byte number[]= {
B00000010, //0
B10011110, //1
B00100100, //2
B00001100, //3
B10011000, //4
B01001000, //5
B01000000, //6
B00011110, //7
B00000000, //8
B00001000}; //9
*/
void setup() {
Serial.begin(9600); // Inicializamos el puerto serie
pinMode(coin_sensorpin, INPUT);
pinMode(red_sensorpin, INPUT);
pinMode(blue_sensorpin, INPUT);
//pinMode (relay, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(data, OUTPUT);
pinMode(latch1, OUTPUT);
pinMode(clockpin1, OUTPUT);
pinMode(data1, OUTPUT);
showred();
showblue();
}
void loop() {
redSensorState = digitalRead(red_sensorpin);
blueSensorState = digitalRead(blue_sensorpin);
coinSensorState = digitalRead(coin_sensorpin);
//********************************************************
if (go == false) {
if (coinSensorState == HIGH && coinSensorState != lastCoinState) {
if (anti_bounce(coin_sensorpin)) {
Serial.println("Coin inserted");
go = true;
sw = false;
//delay(50); // Delay a little bit to avoid bouncing
if (go == true) {
// Release Balls
//digitalWrite(relay, 1);
delay(3000);
//digitalWrite(relay, 0);
}
}
}
}
if (go == true) {
// compare the state of the button to its previous state
if (redSensorState == HIGH && redSensorState != redLastState) {
if (anti_bounce(red_sensorpin)) {
Serial.println("Red broken");
// increases the display counter of the red team
count++;
showred();
}
}
//Compare the state of the button to its previous state
if (blueSensorState == HIGH && blueSensorState != blueLastState) {
if (anti_bounce(blue_sensorpin)) {
Serial.println("Blue broken");
// increases the display counter of the blue team
count1++;
showblue();
}
}
}
//save the last current state as the last state
//for the next loop
lastCoinState = coinSensorState;
redLastState = redSensorState;
blueLastState = blueSensorState;
//** Limit the goal counter **//
if (count + count1 == 9) {
go = false;
delay(1000);
count = 0;
count1 = 0;
showred();
showblue();
}
}
void showred() {
//guarda el último estado actual como el ultimo estado
//para el proximo bucle
//+++++++++++++++++++++++++++++++++++++
digitalWrite(latch, LOW);
shiftOut(data, clockpin, LSBFIRST, number[count]); // lee el arreglo y pasa cada numero a lectura binaria
digitalWrite(latch, HIGH);
}
void showblue() {
//guarda el último estado actual como el ultimo estado
//para el proximo bucle
//+++++++++++++++++++++++++++++++++++++
digitalWrite(latch1, LOW);
shiftOut(data1, clockpin1, LSBFIRST, number[count1]); // lee el arreglo y pasa cada numero a lectura binaria
digitalWrite(latch1, HIGH);
}
boolean anti_bounce(int pin) {
int counter = 0;
boolean State;
boolean previousState;
do {
State = digitalRead(pin);
if (State != previousState) {
// Serial.print("IN: ");/
// Serial.println(State);
counter = 0;
previousState = State;
}
else {
counter++;
// Serial.print("OUT: ");
// Serial.println(State);
}
delay(1);
} while (counter < debouncetime);
// Serial.print("OUT: ");
// Serial.println(State);
return State;
}