Svizel_pritula
Published © CC BY

10 Buttons Using 1 Interrupt

Connect up to 10 buttons using a single interrupt.

IntermediateFull instructions provided57,512
10 Buttons Using 1 Interrupt

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Or any other microcontroller
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×21
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
Any button will do, big or small
×10

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Serial logger

Servo with sleep

Serial logger - Fritzing file

Servo with sleep - Fritzing file

Code

Serial logger

Arduino
const int commonPin = 2;
const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13};

unsigned long lastFire = 0;

void setup() {
  configureCommon(); // Setup pins for interrupt

  attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING);

  Serial.begin(9600);
}

void loop() {
  // Empty!
}

void pressInterrupt() { // ISR
  if (millis() - lastFire < 200) { // Debounce
    return;
  }
  lastFire = millis();

  configureDistinct(); // Setup pins for testing individual buttons

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { // Test each button for press
    if (!digitalRead(buttonPins[i])) {
      press(i);
    }
  }

  configureCommon(); // Return to original state
}

void configureCommon() {
  pinMode(commonPin, INPUT_PULLUP);

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) {
    pinMode(buttonPins[i], OUTPUT);
    digitalWrite(buttonPins[i], LOW);
  }
}

void configureDistinct() {
  pinMode(commonPin, OUTPUT);
  digitalWrite(commonPin, LOW);

  for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
}

void press(int button) { // Our handler
  Serial.println(button + 1);
}

Servo with sleep mode

Arduino
#include <Servo.h>
#include <avr/sleep.h>
#include <avr/power.h>

const int commonPin = 2;
const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13};

const int servoEnablePin = A1;
const int servoPin = A0;
Servo servo;

unsigned long lastFire = 0;
int status = 0;

void setup() {
  pinMode(commonPin, INPUT_PULLUP);
  
  configureCommon();
  
  attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING);

  servo.attach(servoPin);
  pinMode(servoEnablePin, OUTPUT);
}

void loop() {
  if (millis()-lastFire > 5000) {
    digitalWrite(servoEnablePin, LOW);
    
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();  
    sleep_mode();
  }

  delay(10);
}

void pressInterrupt() {
  sleep_disable();
  power_all_enable();
  
  if (millis()-lastFire < 200) {
    return;
  }
  lastFire = millis();
  
  configureDistinct();

  for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) {
    if (!digitalRead(buttonPins[i])) {
      press(i);
    }
  }

  configureCommon();
}

void configureCommon() {
  pinMode(commonPin, INPUT_PULLUP);
  
  for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) {
    pinMode(buttonPins[i], OUTPUT);
    digitalWrite(buttonPins[i], LOW);
  }
}

void configureDistinct() {
  pinMode(commonPin, OUTPUT);
  digitalWrite(commonPin, LOW);
  
  for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
}

void press(int button) {
  digitalWrite(servoEnablePin, HIGH);
  servo.write(map(button,0,9,0,180));
}

Credits

Svizel_pritula

Svizel_pritula

0 projects • 19 followers

Comments