[SOLVED] Digital Switch Array Read and Bleed Errors

Hi Guys,

I'm trying to build the beginnings of a project and get seven switches to reliably send input that I'm printing to serial. I believe this setup is quite simple.

I've attached a picture of my setup in Fritzing, in real life and also the serial output format.

My problem is that only three of the switches are registering as 0 when I press them. There also seems to be an unreliability as to which switches they are, switch 1 and 2 always work, but sometimes switch 3-4 will work but not 5-7. There is also occasionally some switch bleed where one switch will trigger two 0s

I've tried a lot of hardware troubleshooting as far as swapping the switches around, trying some of the switches that aren't working on pins that are working and they work fine. This leads me to think it's a software thing. Maybe I'm saturating some sort of read bus??

The software things I've tried are:

  • Debouncing
  • Interrupts (I think...)
  • Slowing down the serial from 9600 to 4800
  • Building the whole thing without for loops and just manually putting 7 switches in
  • Using an Arduino Mega 2560 (had the same problems)
  • Using 10K pulldown resistors instead
  • Continuity checked the switches inline with the circuit with a multimeter, works fine

Things I will try:

  • Using a lower Ohm pull down resistor

Here's the code:

#define buttonFirst 2     // the first digital pin used by the switches
#define buttonLast 8      // the last digital pin used by the switches
#define buttonLEDFirst 11 // the first PWM channel used by the switch LED
#define buttonLEDLast 12  // the last PWM channel used by the switch LED

boolean switchState = 0;
byte switches[buttonLast-buttonFirst+1];

void setup() 
{
  Serial.begin(9600);
  for(byte button = buttonFirst; button < (buttonLast + 1); button++) 
  {
    pinMode(button,INPUT);
    digitalWrite(button,HIGH);
  }
  for(int led = buttonLEDFirst; led < (buttonLEDLast + 1); led++) 
  {
    pinMode(led,OUTPUT);
  }
}
void loop() 
{
  // fills the array
  for(byte switchnum = buttonFirst; switchnum < (buttonLast + 1); switchnum++) 
  {
    switches[switchnum-buttonFirst] = digitalRead(switchnum);
  }
  // prints the array
  for(byte arrayValue = 0; arrayValue < (buttonLast-buttonFirst+1); arrayValue++) 
  {
  Serial.print(arrayValue+1);
  Serial.print(":");
  Serial.print(switches[arrayValue]);
  Serial.print(" ");
  }
  Serial.println();
}

The bits to do with LEDs are unimplemented but are the beginnings of something.

Thanks in advance for any help you guys may have!

Chris

UPDATE:

I just tried shorting each of the switches with a piece of jumper wire and they all read fine then... makes me think it's an electrical thing to do with the switches maybe??.. even though they continuity check fine..

IMG_2159-1.JPG

Serial Capture.png

Just solved it!

There are bits of the breadboard that don't contact with the switches properly, so I just moved one of the switches and now it's all good.

It sure helps to write it out when problem solving!

You do have pullup resistors, don't you?

See: