Code for button press detection

is this code allright?

int  button() 
{  
  int y=digitalRead(8);
  if(y==HIGH)
  {             
    y=LOW ;
    delay(1000); 

    y=digitalRead(8); 
    if(y==LOW)
      return 1; 
    else                   
      return 2 ;     
  }    

  else return 0;


}

it doesnt seem to work :confused:
it should return 2 if its a long press
1 if it is a short press
and 0 if it doesnt detect any button

Normally, button presses go from HIGH to LOW (switch contact to gnd) - Looks like you're running LOW to HIGH

Start by fixing your indentation: Put each curly brace on its own line and use the Tools > Auto Format function in the Arduino IDE. Then, manually copy and paste the code from the IDE to the forums (don't use the Copy to Forum function) surrounding it with CODE tags.

In what way does it not work ?
Have you tried putting some Serial.prints in there to see what the value of y is at various points ?

yea, it returns erratic value
when i put my hand close to the wire, it returns different values

int  button() 
{  
  int y=digitalRead(8);
  if(y==HIGH)
  {             
    y=LOW ;
    delay(1000); 

    y=digitalRead(8); 
    if(y==LOW)
      return 1; 
    else                   
      return 2 ;     
  }    

  else return 0;


}

abrookfield:
Normally, button presses go from HIGH to LOW (switch contact to gnd) - Looks like you're running LOW to HIGH

could u please ellaborate on that
and yes, my switch normally passes no current (atleast thats how i planned it to be), if i press, the circuit is completed

Your input pin is floating then. You need to enable the internal pull-up resistor so that when the switch is open, the input will read HIGH. You also need to modify your circuit so that pressing the button will connect the input pin to GND.

What is connected to pin 8? A complete diagram, please.

What you have sounds a lot like a floating pin condition, caused by not using proper pullup or pulldown resistors.