/*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
*/
#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 = 3;
int sec;
byte RS; // bezici sekce 0b0000 0000 0bit MV, 1 - prvni sekce atd
byte STOPALL; //vypnutí všech sekci
// ------------------------ SETUP -----------------------------------------------------------------
void setup()
{
Serial.begin(9600);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);
button6.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(MV, OUTPUT);
// ------- CPU -------------------------------------------------------------
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()
{
button1.loop(); // MUST call the loop() function first
button2.loop();
button3.loop();
button4.loop();
button5.loop();
button6.loop();
if(button1.isPressed())
pressedTime = millis();
if(button1.isReleased())
{
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < SHORT_PRESS_TIME )
{Serial.println("A short press is detected");
sec = sec + 20;
digitalWrite(sekce1, HIGH);
digitalWrite(MV, HIGH);
RS = 0b00000011; //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 = 0b00000000;
}
}
if (RS == 0b00000000)
{
digitalWrite(sekce1,LOW);
digitalWrite(MV, LOW);
}
}
void flash() // funkce bliknuti_negace led ------------------------------------------------
{
static boolean output = HIGH;
digitalWrite(LED_pin, output);
output = !output;
}
// ----------------OBSLUHA ISR -----------------------------------------
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 = 0b00000000;
sec = 0;
}
}