Components and supplies
Tactile Switch, Top Actuated
Breadboard (generic)
Jumper wires (generic)
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
Serial logger
arduino
1const int commonPin = 2; 2const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13}; 3 4unsigned long lastFire = 0; 5 6void setup() { 7 configureCommon(); // Setup pins for interrupt 8 9 attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING); 10 11 Serial.begin(9600); 12} 13 14void loop() { 15 // Empty! 16} 17 18void pressInterrupt() { // ISR 19 if (millis() - lastFire < 200) { // Debounce 20 return; 21 } 22 lastFire = millis(); 23 24 configureDistinct(); // Setup pins for testing individual buttons 25 26 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { // Test each button for press 27 if (!digitalRead(buttonPins[i])) { 28 press(i); 29 } 30 } 31 32 configureCommon(); // Return to original state 33} 34 35void configureCommon() { 36 pinMode(commonPin, INPUT_PULLUP); 37 38 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { 39 pinMode(buttonPins[i], OUTPUT); 40 digitalWrite(buttonPins[i], LOW); 41 } 42} 43 44void configureDistinct() { 45 pinMode(commonPin, OUTPUT); 46 digitalWrite(commonPin, LOW); 47 48 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { 49 pinMode(buttonPins[i], INPUT_PULLUP); 50 } 51} 52 53void press(int button) { // Our handler 54 Serial.println(button + 1); 55}
Servo with sleep mode
arduino
1#include <Servo.h> 2#include <avr/sleep.h> 3#include <avr/power.h> 4 5const int commonPin = 2; 6const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13}; 7 8const int servoEnablePin = A1; 9const int servoPin = A0; 10Servo servo; 11 12unsigned long lastFire = 0; 13int status = 0; 14 15void setup() { 16 pinMode(commonPin, INPUT_PULLUP); 17 18 configureCommon(); 19 20 attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING); 21 22 servo.attach(servoPin); 23 pinMode(servoEnablePin, OUTPUT); 24} 25 26void loop() { 27 if (millis()-lastFire > 5000) { 28 digitalWrite(servoEnablePin, LOW); 29 30 set_sleep_mode(SLEEP_MODE_PWR_DOWN); 31 sleep_enable(); 32 sleep_mode(); 33 } 34 35 delay(10); 36} 37 38void pressInterrupt() { 39 sleep_disable(); 40 power_all_enable(); 41 42 if (millis()-lastFire < 200) { 43 return; 44 } 45 lastFire = millis(); 46 47 configureDistinct(); 48 49 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 50 if (!digitalRead(buttonPins[i])) { 51 press(i); 52 } 53 } 54 55 configureCommon(); 56} 57 58void configureCommon() { 59 pinMode(commonPin, INPUT_PULLUP); 60 61 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 62 pinMode(buttonPins[i], OUTPUT); 63 digitalWrite(buttonPins[i], LOW); 64 } 65} 66 67void configureDistinct() { 68 pinMode(commonPin, OUTPUT); 69 digitalWrite(commonPin, LOW); 70 71 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 72 pinMode(buttonPins[i], INPUT_PULLUP); 73 } 74} 75 76void press(int button) { 77 digitalWrite(servoEnablePin, HIGH); 78 servo.write(map(button,0,9,0,180)); 79}
Serial logger
arduino
1const int commonPin = 2; 2const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13}; 3 4unsigned long lastFire = 0; 5 6void setup() { 7 configureCommon(); // Setup pins for interrupt 8 9 attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING); 10 11 Serial.begin(9600); 12} 13 14void loop() { 15 // Empty! 16} 17 18void pressInterrupt() { // ISR 19 if (millis() - lastFire < 200) { // Debounce 20 return; 21 } 22 lastFire = millis(); 23 24 configureDistinct(); // Setup pins for testing individual buttons 25 26 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { // Test each button for press 27 if (!digitalRead(buttonPins[i])) { 28 press(i); 29 } 30 } 31 32 configureCommon(); // Return to original state 33} 34 35void configureCommon() { 36 pinMode(commonPin, INPUT_PULLUP); 37 38 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { 39 pinMode(buttonPins[i], OUTPUT); 40 digitalWrite(buttonPins[i], LOW); 41 } 42} 43 44void configureDistinct() { 45 pinMode(commonPin, OUTPUT); 46 digitalWrite(commonPin, LOW); 47 48 for (int i = 0; i < sizeof(buttonPins) / sizeof(int); i++) { 49 pinMode(buttonPins[i], INPUT_PULLUP); 50 } 51} 52 53void press(int button) { // Our handler 54 Serial.println(button + 1); 55}
Servo with sleep mode
arduino
1#include <Servo.h> 2#include <avr/sleep.h> 3#include <avr/power.h> 4 5const int commonPin = 2; 6const int buttonPins[] = {4,5,6,7,8,9,10,11,12,13}; 7 8const int servoEnablePin = A1; 9const int servoPin = A0; 10Servo servo; 11 12unsigned long lastFire = 0; 13int status = 0; 14 15void setup() { 16 pinMode(commonPin, INPUT_PULLUP); 17 18 configureCommon(); 19 20 attachInterrupt(digitalPinToInterrupt(commonPin), pressInterrupt, FALLING); 21 22 servo.attach(servoPin); 23 pinMode(servoEnablePin, OUTPUT); 24} 25 26void loop() { 27 if (millis()-lastFire > 5000) { 28 digitalWrite(servoEnablePin, LOW); 29 30 set_sleep_mode(SLEEP_MODE_PWR_DOWN); 31 sleep_enable(); 32 sleep_mode(); 33 } 34 35 delay(10); 36} 37 38void pressInterrupt() { 39 sleep_disable(); 40 power_all_enable(); 41 42 if (millis()-lastFire < 200) { 43 return; 44 } 45 lastFire = millis(); 46 47 configureDistinct(); 48 49 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 50 if (!digitalRead(buttonPins[i])) { 51 press(i); 52 } 53 } 54 55 configureCommon(); 56} 57 58void configureCommon() { 59 pinMode(commonPin, INPUT_PULLUP); 60 61 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 62 pinMode(buttonPins[i], OUTPUT); 63 digitalWrite(buttonPins[i], LOW); 64 } 65} 66 67void configureDistinct() { 68 pinMode(commonPin, OUTPUT); 69 digitalWrite(commonPin, LOW); 70 71 for (int i=0;i<sizeof(buttonPins)/sizeof(int);i++) { 72 pinMode(buttonPins[i], INPUT_PULLUP); 73 } 74} 75 76void press(int button) { 77 digitalWrite(servoEnablePin, HIGH); 78 servo.write(map(button,0,9,0,180)); 79}
Downloadable files
Serial logger - Fritzing file
Serial logger - Fritzing file
Servo with sleep - Fritzing file
Servo with sleep - Fritzing file
Serial logger
Serial logger
Servo with sleep - Fritzing file
Servo with sleep - Fritzing file
Servo with sleep
Servo with sleep
Serial logger
Serial logger
Serial logger - Fritzing file
Serial logger - Fritzing file
Comments
Only logged in users can leave comments