First Ardunio Project

Hi, i am an engineering student and have just started to use ardunio as i thought that it would be quite fun and i would also learn a few things along the way.

I have just started my first project, it was from a tutorial i found on YouTube. It is a simple project where you push a button and it turns a LED on and off. I have connected up the circuit as shown on the tutorial video i will attach an image of the circuit and also my code. But when there is a wire plugged into pin 8 (the pin that sends the message to the computer regards to weather the button is pushed down or not) it seems to detect when something is close? Everytime a part of my body or an object goes within 200mm of the pin 8 jumper wire the led turns on and it wont turn off until i either press the push button or make direct contact with the pin 8 jumper wire. i don’t understand why it is doing this or how it is able to detect when an object is close to it or not?

Any help would be appreciated as if i am having problems at this early stage i would hate to see what it is going to be like when i go into more advanced projects.

Thanks, Ciaran

int switchPin = 8;
int ledPin = 13;

void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop()
{
if(digitalRead(switchPin) == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}

Post a picture of YOUR setup.
It seems to me that this is a wiring problem. Pin 8 seems to be "floating". Pullup resistor not connected.
Some breadboards have the long rails on the sides split up in two segments. That might be it.
After you have figuered it out, try removing that 10k resistor, and use "pullup" on pin 8 in your code to use the already inbuild pullup resistor inside the chip.
Then you just need the switch between pin 8 and ground. Nothing else.
This inverts the LED behaviour, so also swap the HIGH/LOW code for the LED.
Leo..

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Weedpharma

This:

{
 if(digitalRead(switchPin) == HIGH)
 {
   digitalWrite(ledPin, HIGH);
 }
 else
 {
   digitalWrite(ledPin, LOW);
 }
}

can be contracted to this:

{
digitalWrite(ledPin,digitalRead(switchPin))
}

But the long way is easier for non proficient to understand! .???

Weedpharma

weedpharma:
But the long way is easier for non proficient to understand! .???

Weedpharma

Agreed, but why not introduce them to efficient coding from the start?
If you start with bad habits, they're harder to shake off.

Henry_Best:
Agreed, but why not introduce them to efficient coding from the start?
If you start with bad habits, they're harder to shake off.

Sorry, but I actually think that short version is the bad habit.

It is much less easy to understand with a quick read through and it affords no opportunity to print the values for debugging purposes. I suspect both styles produce the same assembler code when the compiler has finished its magic so I don't think there is any efficiency.

...R