simple button issue...driving me insane!

Hey guys,

I'm somewhat of an Arduino newb so this one's really got me scratching my head.

I've got a circuit I'm working on and it requires a button for some of the actions. I've done buttons before, so that's not a problem, got my pull up resistor configuration all set on the breadboard, but for some reason I'm getting really weird responses. Voltage bounces between 4.3V and 4.2V and I'm printing the button value to serial for troubleshooting and it's bouncing between 0 and 1. But then, when I press the button, voltage drops to nearly 0V and never goes back to 4.2V. My serial monitor continues to print 0s at that point. I figured there was a hardware issue, so I loaded up the simple button example sketch (without making any changes to the breadboard) and it works flawlessly. My assumption at this point is it is programming...but there's not much to mess up on this. Nevertheless, here's my code:

int ledPin = 13;
int zPin = 0;
int led[] = {3, 6, 9, 10, 11};
int buttonPin = 7;
int buttonVal = 0;

void setup(){
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop(){

   buttonVal = digitalRead(buttonPin);
   Serial.println(buttonVal);
   
   some other code for accelerometer...

Board: Duemilanove OS: Windows XP Pro Com: 10 Micro: ATMEGA168

Here's to hoping someone smarter than me can explain this phenomenon.

Cheers! Dan

Just wondering if it might be your button. I work in the tech support field, not for micros, and sometimes the simple things that we overlook are the real cause of the problem. Have you tried getting rid of the button and just using a jumper wire? This is just in case the button has a bad contact inside. Just a thought.

Thanks for the reply. Unfortunately I've already tried 3 different buttons, and also, the button I'm using works for other button sketches, but not this one (I think I've tried 3 different button sketches).

I'm printing the button value to serial for troubleshooting and it's bouncing between 0 and 1.

In that case you simply haven't got a pull up resistor connected up propley.

Why not use the internal one. Enable it with a digitalWrite(pin, HIGH);

Thanks...I will give that a shot. However, why would all the other sketches work properly and not the one I posted? Perhaps because with this sketch I'm implementing a few other devices (LEDs, accelerometer)?

OK, tried that and got some really wacky results...I didn't have time to measure the voltage, but the serial output was 1s and 0s again, but when I pressed the button some higher numbers were being thrown in like 19, 20, etc. Now I'm REALLY confused...any ideas?

Are you sure your ground connection is OK?

some higher numbers were being thrown in like 19, 20,

Try changing to:- buttonVal = digitalRead(buttonPin);

Serial.println(buttonVal,DEC);

What you had was simply sending out a character 0 or 1 not the ASCII 0 (0x30) or ASCII 1 (0X31).

Also check your baud rate at the monitor end matches 9600.

OK, I will try this tonight. Thanks for your continued support! I'll let you know what happens.

Guys...thanks again for your help and suggestions, but I think I figured it out. There were some sections of code further down (which I didn't post because the program is still under works and sloppy) that was causing some weird output. So when I was thinking only button state was being posted to serial output, other information was being thrown in there as well (which would explain the 19s and 20s). I figured this out by commenting out all unnecessary code and working my way piece by piece through each section until I uncommented the section that caused the problem. Then I was able to zero in on the issue.

Again...thanks for your help!