Language Switcher Fallback

AnalogDebounced (Arduino library)

I made a binary clock with ATtiny85. ATtiny85/45 has 5 pins for input and output. I used 4 for driving two shift registers and was left with just one for input. You have to set the clock somehow, you know, and that is much more convenient with two buttons rather than one. So I arranged two resistors and two buttons so that pushing one of them gives 5V and the other 2,5V.

After I had finished this library I found out, that I can't use millis() and this exact library while ATtiny85's timer0 is in use for interrupts that keep the time... Then I did a modification that instead of comparing millis() increase I count some number of loops that read same button value. You can see that version in my binary clock code.

This idea can be extended to more than two buttons also. I've illustrated possible schematics below. Note that for getting evenly distributed analogRead values you have to use resistors of different values.


Library zip file below. (How to install a library?)


#include <AnalogDebounced.h>

int buttonPin = A0;
int analogButtonReadingsMap[2] = {1023, 511};

AnalogDebounced buttons = AnalogDebounced(buttonPin, 100, 2, analogButtonReadingsMap);

void setup() {

void loop() {
  int buttonreading = buttons.dbRead();   



#include "Arduino.h"

class AnalogDebounced {

    AnalogDebounced(int pinNumber, unsigned long debounceDelay, int numberOfButtons, int *valueMappings);
        //numberOfButtons has to be declared as const
    int dbRead();

        int pin;
        int buttons;
    unsigned long dbDelay;
        int mappings[10];
    int lastDbState;
    unsigned long lastDbTime;
    int value;
        int i;

        int readMapped(int pin);



#include "Arduino.h"
#include "AnalogDebounced.h"

AnalogDebounced::AnalogDebounced(int pinNumber, unsigned long debounceDelay, int numberOfButtons, int *valueMappings) {

  pin = pinNumber;
    buttons = numberOfButtons;
  dbDelay = debounceDelay;
  for (i=0; i<buttons; i++) {
        mappings[i*2] = valueMappings[i]-25;
        mappings[i*2+1] = valueMappings[i]+25;
    lastDbState = readMapped(pin);
    value = lastDbState;
  lastDbTime = millis();

int AnalogDebounced::readMapped(int pin) {
    int reading = analogRead(pin);
    for (i=0; i<buttons; i++) {
        if (reading > mappings[i*2] && reading < mappings[i*2+1]) {
            return i+1;
    return 0;

int AnalogDebounced::dbRead() {
  int dbState = readMapped(pin);
  if (dbState != lastDbState) {// if reading changes
    lastDbTime = millis();     //      we reset timer
  lastDbState = dbState;       //save current reading for next loop
  if ((millis() - lastDbTime) > dbDelay) {  //if reading has been constant for long enough
    value = dbState;                        //    we assign reading as new value
  return value; //return value