#include <TimerFive.h>
/// Código biblioteca Button para Tinkercad
class Button
{
public:
Button(uint8_t pin);
void begin();
bool read();
bool toggled();
bool pressed();
bool released();
bool has_changed();
const static bool PRESSED = LOW;
const static bool RELEASED = HIGH;
private:
uint8_t _pin;
uint16_t _delay;
bool _state;
bool _has_changed;
uint32_t _ignore_until;
};
Button::Button(uint8_t pin)
: _pin(pin)
, _delay(100)
, _state(HIGH)
, _ignore_until(0)
, _has_changed(false)
{
}
void Button::begin()
{
pinMode(_pin, INPUT_PULLUP);
}
//
// public methods
//
bool Button::read()
{
// ignore pin changes until after this delay time
if (_ignore_until > millis())
{
// ignore any changes during this period
}
// pin has changed
else if (digitalRead(_pin) != _state)
{
_ignore_until = millis() + _delay;
_state = !_state;
_has_changed = true;
}
return _state;
}
// has the button been toggled from on -> off, or vice versa
bool Button::toggled()
{
read();
return has_changed();
}
// mostly internal, tells you if a button has changed after calling the read() function
bool Button::has_changed()
{
if (_has_changed == true)
{
_has_changed = false;
return true;
}
return false;
}
// has the button gone from off -> on
bool Button::pressed()
{
if (read() == PRESSED && has_changed() == true)
return true;
else
return false;
}
// has the button gone from on -> off
bool Button::released()
{
if (read() == RELEASED && has_changed() == true)
return true;
else
return false;
}
//----------------
byte tabla7seg[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
byte pines_display[7] = {3, 4, 5, 6, 7, 8, 9};
byte pines_pantalla[4] = {14, 15, 16, 17};
unsigned int contador = 0;
#define BOTON_STOP 11
Button boton_parar = Button(BOTON_STOP);
volatile bool actualizar = false;
void saca7seg(int n) {
for (int i = 0; i < 7; i++) {
digitalWrite(pines_display[i], bitRead(tabla7seg[n], i));
}
}
void segundero(int numero) {
int unidades = numero % 10;
int decenas = numero / 10;
digitalWrite(pines_pantalla[0], HIGH);
saca7seg(decenas);
digitalWrite(pines_pantalla[1], LOW);
delay(10);
digitalWrite(pines_pantalla[1], HIGH);
digitalWrite(pines_pantalla[1], HIGH);
saca7seg(unidades);
digitalWrite(pines_pantalla[0], LOW);
delay(10);
digitalWrite(pines_pantalla[0], HIGH);
}
void ISR_pantalla() {
actualizar = true;
}
void setup() {
boton_parar.begin();
for (int i = 0; i < 7; i++) {
pinMode(pines_display[i], OUTPUT);
digitalWrite(pines_display[i], LOW);
}
for (int i = 0; i < 2; i++) {
pinMode(pines_pantalla[i], OUTPUT);
digitalWrite(pines_pantalla[i], HIGH);
}
Timer5.initialize(1000000);
Timer5.attachInterrupt(ISR_pantalla);
}
void parar(){
if (boton_parar.pressed()) {
saca7seg(contador=0);
}}
void loop() {
if (actualizar) {
contador++;
if (contador >= 60) contador = 0;
actualizar = false;
}
parar();
segundero(contador);
}