how to handle multiple switches

Hi themotorman,

I have an Arduino UNO reading up to 16 track sensors (momentary buttons) on my model railroad layout. The module is working with the following sketch and reports position back to my PC which is running a quite large FreeBasic program I call ABKON. You can adapt it to read your 4 buttons. It uses the pin's internal pullup resistors so I didn't have to use external resistors, except for pin13. It also uses the Button.h library so, you will have to download it from:
http://www.arduino.cc/playground/Code/Button

Here's my sketch:

/*
 * serialAbkonSensorButton.ino
 * ABKON GleisKontaktStatus - Powered by Arduino UNO
 * Created by Bibre: August 2011
 * 
 * Sensors 1-16: handled by digital input Pins 2-17 (14-17 are A0-A3).
 * Analog inputs A4 & A5 are not used.
 *
 * Upon receiving an 'I' or an 'i' command from the ABKON Program,
 * return all input states triggered since previous 'I' command,
 * to the program via COM2 serial port in a 2 byte word.
 */

word abkonInputStates;
unsigned int  pin, value;
char chr;

#include <Button.h>

#ifndef PULLDOWN    // define constants used in previous version of Button.h for Arudino 022
#define PULLDOWN BUTTON_PULLDOWN
#define PULLUP BUTTON_PULLUP
#endif

Button sensor[16] =
  {
    Button(2, PULLDOWN), Button(3, PULLDOWN), Button(4, PULLDOWN), Button(5, PULLDOWN),
    Button(6, PULLDOWN), Button(7, PULLDOWN), Button(8, PULLDOWN), Button(9, PULLDOWN),
    Button(10, PULLDOWN), Button(11, PULLDOWN), Button(12, PULLDOWN), Button(13, PULLDOWN),
    Button(14, PULLDOWN), Button(15, PULLDOWN), Button(16, PULLDOWN), Button(17, PULLDOWN)
  };

void setup() {
  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  for (pin=2; pin<18; pin++) {
    pinMode(pin, INPUT); 
    digitalWrite(pin, HIGH);  // activate pull-up internal 20 Kohm resistor,
  }                           // but Pin13 must additionally have an external 1K ohm pull-up resistor. 
}

void loop() {
  receiveIcommand();   // waits for an 'I' or 'i' command from ABKON Program,
                       // and upon return, all triggered inputs are in abkonInputStates.
  Serial.println(abkonInputStates, HEX);
}  

void receiveIcommand() {
  abkonInputStates = 0;    // reset all input states        

  while (true) {           // gather input states while waiting for an 'I' or 'i' command
    
    for (pin=2; pin<18; pin++) {  // gather input states

      //detect sensor triggering only once
      value = sensor[pin-2].uniquePress();      // arrays start at 0 index so, use pin - 2

      abkonInputStates = abkonInputStates | ( value << (pin - 2) );  // set correspondin input pin bit
    }

    chr = Serial.read();
    if (chr =='I' || chr =='i') {         // command received?
      Serial.println(chr);   // yes, echo command back to ABKON Program
      break;                 // back to main loop
    }
  }                          // loop again until 'I' or 'i' received
}

With this, you do not need any capacitors or latches.

I hope it helps. Feel free to ask for any doubts or additional info. :slight_smile: