Debouncing and State Changing - basic basic

Hi all,
Just got my arduino yesterday. made a cool program (for a beginner) but it looks like i need to get debouncing and state changing handled properly.

so for this experiment i have one pushbutton switch hooked up to the arduino. no resistors. one lead goes to 12. the other to 8.

please see the following code. upon init "OINK" is printed once, or a few times. I desire to see OINK printed ONCE each time i press the button. please help!

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

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

int transInt = 50;
int lastButtonUpTime = 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)){
    if(millis() - lastButtonUpTime > transInt){
      Serial.println("OINK");
      lastButtonUpTime = millis();
    }  
  }
  buttonState1 = buttonValue1;
}

many thanks,
marco.

PS. I've found that if i move the system around with my hands, the switch fires (ie. the pin value becomes HIGH). i dont have a base plate or anything and was working on a sketch pad. any ideas?

You're multiposting. Most people don't like that.

Have fun

can i not test this debouncing by using the digital outs? not knowing much about arduinos, to me it looks neater to use the digital outs for power just to test pushbutton input. any ideas?

no.

You are making the easy stuff become complicated. There are many many examples of button code. I would recommend that you read more of the many free manuals available.