trying to test pushbutton

hi there,
i just got my arduino today and i wrote this sketch

#define BUTTON1 13

int buttonValue1 = 0;
int buttonValue2 = 0;

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

void loop(){

buttonValue1 = digitalRead(BUTTON1);

if(buttonValue1 == HIGH){
Serial.println("OINK");
delay(10);
}
}

easy.

and ive wired it like this. in my broadboard i have a pushbutton. one lead is connected to "gnd" on the arduino. the other lead is connected to "13" on the arduino. i figured when i run this, and press the button, OINK should print in the serial log. but nothing happens.

why not? shouldnt this be bare bones?

thanks,
marco.

You should wire one lead to pin 13, the other to +5 volt not ground.

You should wire one lead to pin 13, the other to +5 volt not ground.

NO NO NO NO NO.

You should enable the internal pull up resistors by putting
pinMode(BUTTON1, INPUT);
digitalWrite(BUTTON1,HIGH);
in the setup()

ah yes! ive set that pin to be an input! silly. so do i need a resistor inline here when i do this? i guess the resistance is 0. how do i calculate the amps if resistance = 0?

mike,

thanks but im a little confused. how can i do a digitalWrite to a pin that ive set as an input?

im guess this pull up resister is what makes this possible. but how can i test the pushbutton this way?

how can i do a digitalWrite to a pin that ive set as an input?

You just do it and it enables the internal pull up resistor - that's how the chip works.
Just run your code to test the button.

same wiring. did this. not working

#define BUTTON1 13

int buttonValue1 = 0;
int buttonValue2 = 0;

void setup(){
pinMode(BUTTON1, INPUT);
digitalWrite(BUTTON1, HIGH);
Serial.begin(9600);
}

void loop(){

buttonValue1 = digitalRead(BUTTON1);

if(buttonValue1 == HIGH){
Serial.println("OINK");
delay(10);
}
}

EDIT

have this now. but it perpetually prints "OINK" without me pushing the button.

#define BUTTON1 13
#define BUTTON2 12

int buttonValue1 = 0;

void setup(){
pinMode(BUTTON2, INPUT);
digitalWrite(BUTTON1, HIGH);
Serial.begin(9600);
}

void loop(){

buttonValue1 = digitalRead(BUTTON2);

if(buttonValue1 == HIGH){
Serial.println("OINK");
delay(10);
}
}

OK try it on a pin other than 13, this is because pin 13 has a resistor and LED connected to it and might be too much for the internal pull up resistor.

indeed that helped. if you can help me a little further id appreciate it.

now it prints a few oinks on init. thats tolerable i guess. but when i press the button, it prints many many oinks. then it stopped. so i pressed it again. then it kept printing oinks. any ideas? how can i get one oink per print? (ideally)

Your code simply continually checks whether the button is pressed.
While it is pressed, it will keep printing oinks as fast as it can, because every time it loops, it is still pressed.

There's a tutorial for state change detection:

That may help.

[edit]Edit: Contact bounce may also be an issue. The contacts in the switch actually bounce and chatter for a short time, seeming like multiple presses. There is also a tutorial on debouncing, and a library, too.
http://arduino.cc/en/Tutorial/Debounce[/edit]

It does that because that is what you asked the code to do. To only get it to print once you have to remember what the button state was and then only print if it is now different from the last time you read it.
Use a variable called lastTime, the last thing in your loop() do:-
lastTime = BUTTON1;

then in the if statement :-
if ( ( buttonValue1 == HIGH) && (latsTime==LOW ) ) {

Note when ever you post code use the # button in the reply box.

ahhh yes. ok gonna try it out. thanks everybody.

ps. in my book it says that for simple code i can simple use a delay as i have.

also, in the state change tutorial you sent. there is a resistor in line with the switch. is this necessary? safer?

still getting strange behaviour. this is my test program. at the beginning, one OINK gets printed. if i push that button again. nothing happens. if i press the other button, OI gets printed once. then soon after it keeps printing. plz help!

//declare constants
#define BUTTON1 8
#define BUTTON2 12
#define POWER 7
#define LED 10

//declare variables
int LEDState = 0;
int LEDBrightness = 63;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonValue1 = 0;
int buttonValue2 = 0;

void setup(){
  //set pins
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(POWER, HIGH);
  Serial.begin(9600);
}

void loop(){
  //read button pins
  buttonValue1 = digitalRead(BUTTON1);
  buttonValue2 = digitalRead(BUTTON2);
  
  //handle button1 behaviour - turn LED off and on
  if((buttonValue1 == HIGH) && (buttonState1 != buttonValue1)){
    Serial.println("OINK");
    if(LEDState == 0){
     analogWrite(LED, LEDBrightness);
     LEDState = 1; 
    }
    
    if(LEDState == 1){
     analogWrite(LED, 0);
     LEDState = 0; 
    }
    
    delay(20);
    
  }
  
  //handle button2 behaviour - increase brightness of LED
  if((buttonValue2 == HIGH) && (buttonState2 != buttonValue2)){
    Serial.println("OI!");
    LEDBrightness += 63;
    if (LEDBrightness > 255)
      LEDBrightness = 63;
      
    analogWrite(LED, LEDBrightness);
  
    delay(20);
    
  }  
  
  buttonState1 = buttonValue1;
  buttonState2 = buttonValue2;
}

Your forgetting to do what you did earlier. That is initialise BOTH push buttons as inputs and do a digital write to BOTH of them to enable the internal pull up resistors.

i think my pushbutton switches are bad or something.

i reduced the problem back to the pushbutton test with the following code. even when i hold the two wires (while not touching the bare metal parts), it continually prints oink. if i wire it up to a switch, on one of mine, it prints oink upon press and once upon release. the other switch it just continues printing oinks. will run to the store and grab more switches. but why would it print oink if im holding the two wires apart!

//declare constants
#define BUTTON1 8
#define POWER1 7

//declare variables
int buttonState1 = LOW;
int buttonValue1 = 0;

void setup(){
  //set pins
  pinMode(BUTTON1, INPUT);
  pinMode(POWER1, OUTPUT);
  digitalWrite(POWER1, HIGH);
  Serial.begin(9600);
}

void loop(){
  //read button pins
  buttonValue1 = digitalRead(BUTTON1);
  
  //handle button1 behaviour - turn LED off and on
  if((buttonValue1 == HIGH) && (buttonState1 != buttonValue1)){
    Serial.println("OINK");
    delay(50);
  }
  buttonState1 = buttonValue1;
}

You could do an if else statement...