Dip switch problem & shift register question

I have a block of 8 dip switches that are connected to pins 0-7 on my arduino uno and connected with 10k resistors to ground. When I do a digitalRead to the pins I get back seemingly random results. I expected to get digitalRead(pin) == HIGH when the switch was in the on position. The switches seem to be ok, is my setup wrong? I was using the button tutorial from this site as my guide.

Second, can I use the 74HC595 IC to read multiple switches or is it only for output? The ShiftIn tutorial mentions CD4021B is this my best option? If so is this the right thing? CD4021B: Major Brands : IC CD4021 8-Stage Static Shift Register : ICs & Semiconductors it doesn’t have the B on the end, but the description seems rightish.

You should show a sketch of your circuit. You probably have the resistor connected to the wrong spot.

595 is for output only. The best way to work out what chips do is to look for the datasheet (Google the chip name followed by 'datasheet')

Do you have the internal pullup resistors turned on?

pinMode (pin_number, INPUT);
digitalWrite (pin-number, HIGH);

You will only read random high/low when the switch is open otherwise.

'595 is for output. Need a part like 74HC165 http://www.ti.com/product/sn74hc165 for parallel in from dip switch to serial out to the arduino

digitalWrite (pin-number, HIGH);

^ This is the piece i was missing. If I am using a the internal pullup resistor do I need an additional resistor?

As far as the ICs I looked at the datasheet but I wasn't certain and I have the 595s so I thought it would be worth making sure. I am coming at this with a strong c++ background but I know very little about electronics. I got impatient and ordered the CD4021BE mentioned in the ShiftIn tutorial is there any reason to go with the 74HC165 instead?

what is the best way to show a sketch of what I am doing?

Create ascii graphics in notepad and paste it inside [ tt ] tags. ;)

With a pullup resistor, the other side of the switch goes to GND directly. With your external pulldown resistor, you need to supply Vcc to the other side of the switch, and connect Arduino input to the resistor side of the switch.

...BE suffix just means plastic case , I guess

my hardware ignorance taught me 74HC are faster, 4000 have a wider Vcc Range --> for our use ( 5V Arduino ) it does not matter. some complain , good old 4000 CMOS series are not available any more.

With internal pullup, no external resistor is needed.

To post your code click the # icon, it will add [ code ] & [ /code ] tags, copy & paste your code in the middle.

I never used 4000 series CMOS, was too slow in my line of design work. At 5V operation, max clock speed is 3 MHz. http://www.ti.com/lit/ds/symlink/cd4021b.pdf Has the added drawback of not having a Chip Select type of pin, so you cannot keep it from putting serial data out if you had multiple devices doing SPI operations.

" good old 4000 CMOS series are not available any more" Really? TI says they are active parts. http://www.ti.com/sitesearch/docs/universalsearch.tsp?searchTerm=CD4021&linkId=1

so here is a sketch of my understand as to how this should be using the pullup resistor

or should it be like this with one pin connected to power and the arduino?

Hmmm - draw a closed switch between pin 1 and pin 16 of the dip switch in your second diagram. Do you notice anything (like a direct short circuit between +5V and GND)?

doesnt the pullup resistor on the arduino pin take care of that? Do I need to connect the switch to the 5v and arduino pin and ground or just the arduino pin and ground? Also any tips and or corrections on my attempts to draw the circuits are welcome.

my plan is to build a step sequencer, so I want to have a matching pot for each switch. Originally I planned to use digital pins to read the switches and analog pins for the pots. My code would be something like this

int iSteps(8);
int iTemp(300);
void loop()
for(int iBeat = 0; iBeat < iSteps; ++iBeat) {
   iTempo = GetTempo(); // checks the tempo pot
   if(PlayBeat(iBeat)) { // checks the appropriate switch
     iTone = GetTone(iBeat); // checks the appropriate pot
    PlayTone(iTone, duration);
  }
  else { 
     Delay(iTempo);
  }
}

After thinking about it I should be able to do it all with just the analog pins right? It should be something like this

and code like this

int iSteps(8);
int iTemp(300);
void loop()
for(int iBeat = 0; iBeat < iSteps; ++iBeat) {
   iTempo = GetTempo(); // checks the tempo pot
     iTone = GetTone(iBeat); // checks the appropriate pot, returns 0 for off
 if( iTone ) { 
    PlayTone(iTone, duration);
  }
  else { 
     Delay(iTempo);
  }
}

and in this case i would not use the built in pullup resistor because i have the pot. is that right?

Pullup resistor is for digitals only.

You can of course do it that way. An open switch should give you 1023 with AnalogRead.

However you have more Digital Pins than Analog Inputs, in general. With this circuit, you need a seperate Analog Input per pot.

michael_x: Pullup resistor is for digitals only.

that question was referring to the second drawing, which is using the digital pins. How should the switch be hooked up to those?

However you have more Digital Pins than Analog Inputs, in general. With this circuit, you need a seperate Analog Input per pot.

These circuits are simpilfied for ease of drawing and to focus on my current problems and questions. In the end I will need at least 17 pots (1 per beat + 1 for the speed control) so I need to deal with the limited analog inputs anyway. I was planning to use the 74HC4052 http://www.nxp.com/documents/data_sheet/74HC_HCT4052.pdf, i think i should be able to use these to increase my number of analog inputs enough. I think hooking it up this way should save me digital inputs because I wont have to use 3 (?) for a shift register like the 74HC165 mentioned above. I think that in the end I will need 5 analog input pins (2x2 for the 74HC4052 to read the 16 tone pots + 1 for speed) and 7 digital pins (3 to control a pair of 74HC595s for LEDs + 2x2 for the 74HC4052s), This should leave me with some pins to deal with output and any additional stuff I can dream up. Of course i could also be totally wrong...

can anyone help me with how to set up this switch on the digital pins? all the tutorials i have found use buttons with 4 pins and I can't quite work out where in the circuit the arduino fits with only 2 pins

Is this a good start for you?

hooking it up with the pot makes alot more sense to me but I think I get the diagram you posted I need to experiment a bit to be sure. thanks

If you need that many analog inputs perhaps some extra helper ics will fill your needs, I took apart two dmx controller boards that had 24 analogsliders each(10k pots) and they had 3 8channel adc that handled all of those, tho in that case it was controlled via a 8 bit bus, but im sure they make them spi or even i2c if you don't need speed

and say you use spi then you can potentially control the in and out shift register as well and all you would need is a chip select line for each and cascade them the only problem with that is how fast you would need to poll them to get responsive results, you'll need to run the spi as fast as possible too because that's alot of info to pass long 2 bytes output, 1 byte input and then 17x10(?) Bit, so your talking like 40+ bytes transfer to get all the values you want, which isn't bad

And as for pins that's just miso, mosi,sck and then maybe 6 cs pins

Thanks for the help, I got it figured out. Although the switch returned HIGH when I expected low and vice versa, but that was easy enough to fix with the software. One more question, I was getting bad reads on the arduino's pin two, it was always returning LOW. When I moved it to pin 10 using the same switch and wire it worked properly? how common is it for one pin to go bad? is it likely something I did wrong? any ideas as to how to diagnose and fix the issue?