problem in getting the desired output

hey! this is the code is used

int switchpin=8;
int ledpin =13;

void setup()
{
  pinMode(switchpin,INPUT);
  pinMode(ledpin,OUTPUT);
}

void loop()
{  
  if(digitaRead(switchpin)==HIGH)
  {
      digitalWrite(ledpin,HIGH);
  }
  else
{
 digitalWrite(ledpin,LOW);
}
}

The problem i am facing is that as i approach the switch, before i reach it the led glows. As i move my hand away it stops glowing. Basically the set up works without me touching the switch.
Also at other times the setup works perfectly when i tap on the switch. But when i press it, it stops sensing change.
Why is this happening? Is it a problem with my board?

Your hand is making the pin pick up stray sugnals. Read about pinMode(x, INPUT_PULLUP) here.

Use that approach and change the wiring (which you didn't show) so that the switch connects the pin to ground not 5V. The pin will normally be high (via the pullup) and go low when you push it. Change the code to reflect that pushed is low and un-pushed is high.

Yes classic you have a floating input. It comes of not wiring your switch up correctly.
See also:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

In addition to Mike's excellent guide... I have a write-up with videos that show what's happening, on an oscilloscope.

http://www.baldengineer.com/tutorials/arduino-pull-ups/

I used a pull up resistor and this code:

int switchpin=8;
int led=13;


void setup()
{
  pinMode(switchpin,INPUT);
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH); 
}

void loop()
{
  boolean button=digitalRead(switchpin);
  digitalWrite(led, !button);
}

and i have attached an image of my circuit.
i am still facing the same problem. it works continuously 3 times , then doesnt work the next 3 times.

It's very difficult (ie impossible) to figure out your circuit from those pics- can you do a sketch of the circuit and post a photo of it for us?

But as far as I can see, you don't have a pullup resistor: you have a resistor in series with the switch and the pin. I'm not sure though, from the pix.

That said, it's MUCH easier to use the internal pullup, and have the pin high normally and to ground when pressed.

But if you post a pic of a circuit schematic, we'll see more clearly.

You should be aiming for this

You seem to have only connected one side of the switch.
Tip - wire push buttons like this across a diagonal, then you always get the right two pins.

i am still facing the same problem. it works continuously 3 times , then doesnt work the next 3 times.

That was not the problem you posted originally you said:-

As i move my hand away it stops glowing. Basically the set up works without me touching the switch.
Also at other times the setup works perfectly when i tap on the switch. But when i press it, it stops sensing change.

Anyway you still have not wired it up right.

Grumpy_Mike:
Tip - wire push buttons like this across a diagonal, then you always get the right two pins.

That IS a good tip... they always confuse me.

The diagram attached shows how those 4-pin switches work internally so you can see what Mike says makes sense. Pins on a diagonal are always across the switch; pins on one side might or might not be. Awesome tip....

switch schematic.jpg

i am very new to this, so from whatever i could understand, i changed my wiring to this.
It doesnt work at all.

i changed my wiring to this.
It doesnt work at all.

Yes it will not.

Why will you not wire it up like we are saying? You need a pull up resistor.

Now click on those photos you posted. See how big they are so big they are useless. Sio size them before posting no more that 1000 pixels wide.

i am very new to this,

And you will stay new as long as you ignore advice.

the resistor that i connected is a pull up resistor. value 10k ohms.

Nice video James...

Maybe worth pointing out that the video shows the "old" way of using the internal pullup, by setting an input pin high:

pinMode(myPin, INPUT);
digitalWrite(myPin, HIGH);

The later IDE supports a simpler one line "new" way of doing this:

pinMode(myPin, INPUT_PULLUP);

fishfry:
the resistor that i connected is a pull up resistor. value 10k ohms.

How do we know? That diagram has just unnamed terminals connected to wires. It does not show you have a pull up resistor.

When you have a pull up resistor the switch goes between the input pin and ground. I can't make that out of what you posted.

fishfry:
the resistor that i connected is a pull up resistor. value 10k ohms.

I think you may the switch wired wrong: looks to me like 5V is connected to GND permanently through the resistor and you're not pulling the i/o pin to 5V.

Please do all of us- including yourself- a favour, and lose the external resistor. Use the INPUT_PULLUP approach and then just simply pull the i/o pin to ground through the switch as I suggested earlier, like this

fishfry:
the resistor that i connected is a pull up resistor. value 10k ohms.

A pull up resistor goes directly from the input pin to 5V. You do not have a resistor connected directly to the input pin you have it going through the switch.

The switch should be wired from the input and one end of the pull up resistor with the other end of the switch to ground.

Make your life simple and use the internal pullup.

Connect your push button like this picture:

And then change your code to:

int switchpin=8;
int led=13;

void setup()
{
  pinMode(switchpin,INPUT_PULLUP);  //enables internal pull-up resistor
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH); 
}

void loop()
{
  boolean button=digitalRead(switchpin);
  digitalWrite(led, !button);
}