/*Primer uporabe knjižnice Bounce.2 */
#include <Bounce2.h>
#define btnGreen 3
#define btnRed 4
int counter;
Bounce2::Button tipka1, tipka2 = Bounce2::Button();
void setup(){
tipka1.attach(btnGreen, INPUT_PULLUP);
tipka2.attach(btnRed, INPUT_PULLUP);
tipka1.interval(10);
tipka2.interval(10);
tipka1.setPressedState(LOW);
tipka2.setPressedState(LOW);
Serial.begin(115200);
}
void loop(){
tipka1.update();
tipka2.update();
if(tipka1.pressed()){
counter++;
Serial.println(counter);
}
if(tipka2.pressed()){
counter--;
Serial.println(counter);
}
}
/* Tipka kot zunanji interupt; eliminiran Bounce efekt
#define ledPin 8
#define tipka 2
volatile unsigned long lastBounce = 0;
int delayBounce = 300;
int counter;
volatile bool stanje = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(tipka, INPUT_PULLUP);
// napovem funkcijo zunanjega interupta
attachInterrupt(digitalPinToInterrupt(tipka), ISR_prekinitev, FALLING);
}
void loop(){
digitalWrite(ledPin, stanje);
}
void ISR_prekinitev(){
if((millis() - lastBounce) > delayBounce){
stanje = !stanje;
lastBounce = millis();
counter++;
Serial.println(counter);
}
}
*/
/* Problem odboja kontakta je rešen
#define tipka 4
bool lastState= HIGH;
int counter;
unsigned long lastBounce = 0;
int delayBounce = 300;
void setup(){
Serial.begin(115200);
pinMode(tipka, INPUT_PULLUP);
}
void loop(){
int state = digitalRead(tipka);
if(state != lastState && (millis() - lastBounce) > delayBounce){
lastBounce = millis();
lastState = state;
if (lastState == HIGH ){
counter++;
Serial.println(counter);
}
}
}
*/
/* problem odboj kontakta ni rešen
#define tipka 4
int counter;
bool lastState = HIGH;
void setup() {
Serial.begin(115200);
pinMode(tipka, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(tipka);
if(lastState != state){
counter++;
lastState = state;
Serial.println(counter);
}
}
*/