/*
https://forum.arduino.cc/t/attiny10-button-input-wired-with-external-pull-down/1264601
Test setup:
Button Side 1 --> 4.7k Resistor --> PB1 --> Ground
Button Side 2 --> VCC (3,3V or 5V)
LED (+) Color 1 --> 1K resistor --> Pin 1 (PB0)
LED (+) Color 2 --> 1K resistor --> Pin 1 (PB2)
LED (-) --> (GND)
*/
#define LED1_PIN PB0
#define LED2_PIN PB2
#define BTN_PIN PB1
unsigned long lastCheck;
//needs a bit more time than 500 ms
unsigned long intervalCheck = 1000;// 1000 ms to press buttons..
unsigned long lastPress;
unsigned long intervalDebounce = 20;
byte lastState = 0;
bool timerStarted = false;
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT);
}
void loop() {
unsigned long now = millis();
static uint8_t multiclick = 0;
if (now - lastPress >= intervalDebounce) {
byte state = digitalRead(BTN_PIN);
if (state != lastState) {
lastPress = now;
lastState = state;
//start timer to count press..
if (!timerStarted) {
timerStarted = true;
lastCheck = now;
}
if (state == HIGH) multiclick++;
}
}
if (timerStarted) {
if (now - lastCheck >= intervalCheck) {
lastCheck = now;
switch (multiclick) {
case 1: // it was a single long click
digitalWrite(LED1_PIN, !digitalRead(LED1_PIN));
digitalWrite(LED2_PIN, !digitalRead(LED2_PIN));
multiclick = 0;
break;
case 2: // doubleclick
digitalWrite(LED1_PIN, !digitalRead(LED1_PIN)); // toggle PB0
multiclick = 0;
break;
case 3: // tripleclick
digitalWrite(LED2_PIN, !digitalRead(LED2_PIN)); // toggle PB2
multiclick = 0;
break;
default:
multiclick = 0; // undefined # of clicks
break;
}
//reset for next shot
timerStarted = false;
multiclick = 0;
}
}
}