how to handle multiple switches

I have 4 pushbutton momentary switches that put out either a 5 volt or a 0 depending if I want any of the four lamps to flash or not. This is to be controlled by a MEGA. The flashing rate is to determined in the prgm. If a button is pressed then the corresponding lamp should flash. It should stay flashing until the button is pressed again. I am new to prgming C++ but have some experience in Basic. Can any one give me some idea of the code, or tell me where to look, for this. Thank you.

Look for debouncing a switch to begin with. Any switch is likeley to have the contacts close more that 1 time for and given closure and you need to "debounce" it so you only see it as one button press. I had one switch on the job that was capable of giving 15 bounces for 1 press.

Like KF2QD check out http://arduino.cc/it/Tutorial/Debounce

THanks for heads up on debouncing. I think I understand that part. The part I am missing is how to have complete prgm that goes through all the states and then keeps looping. I think that I need the prgming format info but not sure as I don't know what to ask for? If a pushbutton is pressed momentarilly and there is a long prgm how can I be sure that the status of the button was noticed since the port is not constantly monitored, and Arduino might be looking elsewhere at that critical time.
I could do the debouncing and latching externally, with discrete logic chips, to the Arduino but I am trying to avoid all that complications. More ideas and help please

Another option is to use a capacitor as a hardware denounce.

if you don't want/need to interrupt the code that is run when a button is pressed, you can simply run your long code in the loop.

If you do want to be able to interrupt your lengthy code while it is running, you could use interrupts on the button. A interrupt triggers a bit of code when, for example, you press the button and that chnages the digital port status.

~~Have you an example of using a capacitor for debouncing? ~~
What rules are there when selecting a capacitor for this?

Woot google. http://www.elexp.com/t_bounc.htm

I have a project that will use 16 switches and hardware denouncing would be great.
I will be using a mega so no need for a matrix :slight_smile: but it would be the same concept as this.

Yep, google helps a ton :slight_smile:

I was wondering about this too, as I have mainly used software debounce. I didn't find any specific values for the cap, but I found 100nf mentioned a few times.

After more googling I found these two tutorials for hardware debouncing

http://www.ikalogic.com/debouncing.php
http://www.labbookpages.co.uk/electronics/debounce.html

The ikalogic site says to use R1 = 10 K and C1 = 100nF ceramic capacitor.

But if software debouncing is your chosen solution then Debounce Code – One Post To Rule Them All | Hackaday has a heap of routines to look at.

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: