quickest way to read 8 switches

i was wondering what the quickest way to read 8 switches would be, i want to make my design modular so i was going to use a shift register but then i attached it and it takes about 15 ms to transfer the data,

about how long would reading 8 switches connected directly to the arduino take?

Try it. You're going to have a delay no matter what. You can offload switch detection to another IC such as a keypad decoder with interrupt.

Bypass the friendly Arduino digital read functions and read the pins directly. Hook all 8 of 'em to a single port (check the ATmega datasheet). Grab the value in a single instruction, i.e. mvar = PORTC;.

-j

15 ms seems like a long time to read 8 switches. I can read 16 slide pots on two 4051 multiplexer IC's in less than 10 ms. This includes adressing the mux IC's ( two of them), reading the data, formatting the data and transmitting the data over the serial line to a Pc program.

Is it the 595 shift register that slows tings down ?

Im using the shiftin tutorial on the website and now im getting about 8 ms reading them and 9ms reading the values and then comparing them

//*************************************************************
//  Name    : shiftIn Example 1.4                             //
//  Author  : Carlyn Maw                                        //
//  Date    : 25 Jan, 2007                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a CD4021B Shift Register          //
//          :                                                   //
//****************************************************************

//define where your pins are
int latchPin = 7;
int dataPin = 9;
int clockPin = 6;

//Define variables to hold the data 
//for shift register.
//starting with a non-zero numbers can help
//troubleshoot
byte switchVar1 = 72;  //01001000


void setup() {
  //start serial
  Serial.begin(38400);

  //define pin modes
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);

}

void loop() {
  int time1;
  int time2;
  int test0,test1,test2,test3,test4,test5,test6,test7;
 
 
 // TIME TEST ONE
 
  time1 = millis();                              // SET TIME 
   digitalWrite(latchPin,1);
  delayMicroseconds(20);
  digitalWrite(latchPin,0);
  switchVar1= shiftIn(dataPin, clockPin);
  time2 = millis();                              // END TIME
  time1 = time2-time1;                          // DELTA TIME
  
  Serial.print("It took ");                      // PRINT DIFFERENCE
  Serial.print(time1);
  Serial.println(" ms to read the pins");
  Serial.println(switchVar1, BIN);
  delay(1000);
  
  // TIME TEST TWO 
  // READING VALUE OF THE SHIFT REGISTER AND THEN COMPARING THEM AND THEN ADDING. 
  time1= millis();
   digitalWrite(latchPin,1);
  //set it to 1 to collect parallel data, wait
  delayMicroseconds(20);
  //set it to 0 to transmit data serially  
  digitalWrite(latchPin,0);
  switchVar1= shiftIn(dataPin, clockPin);
  if(getBit(switchVar1,0))test0++;
  if(getBit(switchVar1,1))test1++;
  if(getBit(switchVar1,2))test2++;  
  if(getBit(switchVar1,3))test3++;  
  if(getBit(switchVar1,4))test4++;  
  if(getBit(switchVar1,5))test5++;
  if(getBit(switchVar1,6))test6++;
  if(getBit(switchVar1,7))test7++;
  time2= millis();
  time1= time2-time1;
  Serial.print("It took ");
  Serial.print(time1);
  Serial.println(" ms to read the pins and check");
  Serial.println(switchVar1,BIN);
  delay(1000);
  
  
}

//------------------------------------------------end main loop

////// ----------------------------------------shiftIn function
///// just needs the location of the data pin and the clock pin
///// it returns a byte with each bit in the byte corresponding
///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0

byte shiftIn(int myDataPin, int myClockPin) { 


  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);

  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0); 
    delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      myDataIn = myDataIn | (1 << i);
    }
    else {
    
      pinState = 0;
    }
    digitalWrite(myClockPin, 1);

  }
  return myDataIn;
}

////// ----------------------------------------getBit
boolean getBit(byte myVarIn, byte whatBit) {
  boolean bitState;
  bitState = myVarIn & (1 << whatBit);
  return bitState;
}

As kg4wsv posted forget about all that digitalRead() and digitalWrite() and use use direct PORT Mainpulation instead. There is an article in the reference. Hooking them all up on a single PORT won't really work as the only PORT with 8 accessable PINs on an ATMEL168 would be PORTB. Unfortunately 2 of that PORTs are used by the crystel. So you can just get 6 Inputs parallel. Analog Input 0 - Analog Input 5 are All PortC (Bit 0 - 5).

You will quickly notice that 15ms is a huge amount of time for reading 8 Inputs.

Hooking them all up on a single PORT won't really work as the only PORT with 8 accessable PINs on an ATMEL168 would be PORTB. Unfortunately 2 of that PORTs are used by the crystel. So you can just get 6 Inputs parallel. Analog Input 0 - Analog Input 5 are All PortC (Bit 0 - 5).

you can read the data in two instructions as two 4bit nibbles

data = (lowPIN & 0x0F) | (PIND & 0xF0);

On a standard arduino, the high port has to be PIND for the reason given above, but the lowPIN can be PINB (pins 8-11) or PINC (the analog pins)

don't forget to set the data direction

… and with a shift or three thrown in, any set of 4 can be combined with any other 4, using the expression mem posted above.

Shifts and logical ANDs are quite inexpensive, in terms of CPU time.

-j