#include <ezButton.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define PIN_TOGGLE(port, pin) (PORT ## port) ^= (1 << (pin)) //makro na toggle led
//PIN_TOGGLE(B, 5); // Toggle Pin13 (B5) State PB5 ak a13
const int SHORT_PRESS_TIME = 2000; // 1000 milliseconds
const int LONG_PRESS_TIME = 2000; // 1000 milliseconds
ezButton button1(A0, INPUT_PULLUP); // create ezButton object that attach to pin 7;
ezButton button2(A1, INPUT_PULLUP);
ezButton button3(A2, INPUT_PULLUP);
ezButton button4(A3, INPUT_PULLUP);
ezButton button5(A4, INPUT_PULLUP);
ezButton button6(A5, INPUT_PULLUP);
ezButton button7(3, INPUT_PULLUP);
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
const int LED_pin = 13; //interní led
const int sekce1 = 12;
const int sekce2 = 11;
const int sekce3 = 10;
const int sekce4 = 9;
const int sekce5 = 8;
const int sekce6 = 7;
const int sekce7 = 6;
const int MV = 5;
int sec;
int RS;
// ------------------------ SETUP -----------------------------------------------------------------
void setup()
{
// --------- eZbutton debounce time --------------------------------
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);
button6.setDebounceTime(50);
button7.setDebounceTime(50);
// ----- IO periferie -------------------------------------------------------
pinMode(LED_pin, OUTPUT);
pinMode(sekce1, OUTPUT);
pinMode(sekce2, OUTPUT);
pinMode(sekce3, OUTPUT);
pinMode(sekce4, OUTPUT);
pinMode(sekce5, OUTPUT);
pinMode(sekce6, OUTPUT);
pinMode(sekce7, OUTPUT);
pinMode(MV, OUTPUT);
// ------- MPU TIMER 1 ----------------------------------------------
cli(); // zákaz vsech prerusení
TCCR1A = 0; // reset vitace A
TCCR1B = 0; // reset citace B
OCR1A = 0xF424; // porovnavaci registr - zadani hodnoty 62 500
TCCR1B = (1<<WGM12) | (1<<CS12); // delicka hodin
TIMSK1 = (1<<OCIE1A); // povoleni interrupt preteceni citace
sei();
// -------------------------------------------------------------------------
}
void loop()
{
butt_init(); //inicializace ezbuttons
if(RS == 0)
{
sek1();
sek2();
sek3();
sek4();
sek5();
sek6();
sek7();
}
if(RS == 1)
{
sek1();
}
if(RS == 2){
sek2();
}
if(RS == 3){
sek3();
}
if(RS == 4){
sek4();
}
if(RS == 5){
sek5();
}
if(RS == 6){
sek5();
}
if(RS == 7){
sek7();
}
}
// ----------------OBSLUHA ISR T1 COMPARE A-----------------------
ISR(TIMER1_COMPA_vect) //obsluha vektoru přerušeni od čítače 1
{
sec--; // pricte impuls do citace vterin
flash(); //neguje led
if(sec <=0) //
{
RS = 0;
sec = 0;
}
}
// ---------- VOIDS ----------------------------------
void sek1()
{
if(button1.isPressed())
pressedTime = millis();
if(button1.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{sec = sec + 10;
digitalWrite(sekce1, HIGH);
digitalWrite(MV, HIGH);
RS = 1; //pomocná promenná že beží sekce 1
}
if( pressDuration > LONG_PRESS_TIME )
{Serial.println("A long press is detected");
digitalWrite(sekce1, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce1,LOW);
digitalWrite(MV, LOW);
}
}
// -------------------------------------------------------------------------------------------------
void sek2()
{
if(button2.isPressed())
pressedTime = millis();
if(button2.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce2, HIGH);
digitalWrite(MV, HIGH);
RS = 2; //pomocná promenná že beží sekce 2
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce2, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce2,LOW);
digitalWrite(MV, LOW);
}
}
//------------------------------------------------------------
void sek3()
{
if(button3.isPressed())
pressedTime = millis();
if(button3.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce3, HIGH);
digitalWrite(MV, HIGH);
RS = 3; //pomocná promenná že beží sekce 3
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce3, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce3,LOW);
digitalWrite(MV, LOW);
}
}
// ---------------------------------------------------------------------------------
void sek4()
{
if(button4.isPressed())
pressedTime = millis();
if(button4.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce4, HIGH);
digitalWrite(MV, HIGH);
RS = 4; //pomocná promenná že beží sekce 4
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce4, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce4,LOW);
digitalWrite(MV, LOW);
}
}
// -----------------------------------------------------------------------------------------
void sek5()
{
if(button5.isPressed())
pressedTime = millis();
if(button5.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce5, HIGH);
digitalWrite(MV, HIGH);
RS = 5; //pomocná promenná že beží sekce 5
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce5, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce5,LOW);
digitalWrite(MV, LOW);
}
}
// -------------------------------------------------------------------------------------
void sek6()
{
if(button6.isPressed())
pressedTime = millis();
if(button6.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce6, HIGH);
digitalWrite(MV, HIGH);
RS = 6; //pomocná promenná že beží sekce 5
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce6, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce6,LOW);
digitalWrite(MV, LOW);
}
}
void sek7()
{
if(button7.isPressed())
pressedTime = millis();
if(button7.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{
sec = sec + 10;
digitalWrite(sekce7, HIGH);
digitalWrite(MV, HIGH);
RS = 7; //pomocná promenná že beží sekce 7
}
if( pressDuration > LONG_PRESS_TIME )
{
digitalWrite(sekce7, LOW);
digitalWrite(MV, LOW);
sec = 0;
RS = 0;
}
}
if (RS == 0)
{
digitalWrite(sekce7,LOW);
digitalWrite(MV, LOW);
}
}
// ----------------------------------------------------------------------------------------------
void butt_init()
{
button1.loop(); // MUST call the loop() function first
button2.loop();
button3.loop();
button4.loop();
button5.loop();
button6.loop();
button7.loop();
}
// -----------------------------------------------------------
void flash() // funkce bliknuti_negace led
{
static boolean output = HIGH;
digitalWrite(LED_pin, output);
output = !output;
}