Code problem

void setup() {
pinMode(9,OUTPUT);
pinMode(2,INPUT);
}
int pulsante=0;
void loop() {
pulsante=digitalRead(2);
if(pulsante==LOW)
{
digitalWrite(9,HIGH);
}
else{
digitalWrite(9,LOW);
}
}
It shows no errors and it seems correct but it doesn't work when i upload it on my arduino. See any errors?

it doesn't work

What does it do ?
What should it do ?

What is connected to pin 9 ?
How is the input wired ?

A couple of things:
First, welcome to the Forum. I think you'll find it helpful.
Second, please read the post at the top of this Forum by Nick Gammon on how to post to this Forum, especially the use of code tags when posting source code
Third, use Ctrl-T in the IDE before you posting using code tags. It formats your code into a common C style.
Finally, what happens if you do this:

#define LED     13
#define MYKEY    2

void setup() {
  pinMode(LED,OUTPUT);
  pinMode(MYKEY, INPUT);
}
int pulsante=0;
void loop() {
  pulsante = digitalRead(MYKEY);
  if(pulsante == LOW)
  {
    digitalWrite(LED, HIGH);
  }
  else{
    digitalWrite(LED, LOW);
  }

}

This will affect the LED that is one your Arduino so you can see what's happening.

It should turn on a LED when the switch is turned on

(deleted)

circuit:

The switch is on pin 2 and the led on pin 3, this is the new code:
void setup() {
pinMode(3, OUTPUT);
pinMode(2, INPUT);
}
int pulsante = 0;
void loop() {
pulsante = digitalRead(2);
if (pulsante == LOW)
{
digitalWrite(3, HIGH);
}
else {
digitalWrite(3, LOW);
}
}

econjack:
Second, please read the post at the top of this Forum by Nick Gammon on how to post to this Forum, especially the use of code tags when posting source code

Third, use Ctrl-T in the IDE before you posting using code tags. It formats your code into a common C style.

Start doing that now.

mode INPUT_PULLUP recommended for pin 2, especially if you’re still having problems.

Hi,
I see you are pulling high with the tactile button and are using a pull down resistor.
So with the button NOT PRESSED you should have the LED ON.

Have you got a DMM to measure voltages?

Is the button wired correctly as the pins of those tactile switches need to be configured correctly.
Lift the button out and use a piece of wire instead of your button to see if it works.

Do you have the LED around the correct way?
Check it by lifting the wire out of pin3 and connecting it to 5V, the LED should come ON.

Tom.... :slight_smile:

Hi tom thanks for the reply. Now i literally copied the circuit and the code on Arduino projects book. It is named “Spaceship interface”, and it still doesn’t work: pressing the button is the same of not pressing it. I really can’t understand, it never happened. I tried to change pins and places on the breadboard severely times but the result is always the same, i even tried to change the button 4 times, and the LEDs seem to work when i connect them to 5V.

Hi,
Did you try this?

Lift the button out and use a piece of wire instead of your button to see if it works.
Do you have the LED around the correct way?
Check it by lifting the wire out of pin3 and connecting it to 5V, the LED should come ON.

Tom... :slight_smile:

You need to connect the pin 2 of Arduino to other side of tactile switch and add this

pinMode (2,INPUT_PULLUP);

in setup() and also remove the jumper from 5v to the tactile switch.

Hi Baroch13600 (I get Baroch - it's nearly the name of our outgoing leader, but what does 13600 represent?)

I believe your switch is implemented incorrectly. mudassir9999p has it right. Your LED may be backward. Hook up your switch like this:

and change your code to this:

void setup() {
  pinMode(9,OUTPUT);
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  boolean pulsante = digitalRead(2);
  if(!pulsante)  // the button is grounded (ie: a tactile pushbutton is pressed)
  {
    digitalWrite(9, HIGH);
  }
  else{
    digitalWrite(9, LOW);
  }
}

There is one more thing: See how the code above is in a scrollable box? That box also protects the code from corruption by the forum software, so that we see the same code as you do. Use it. Read econjack's posts (above) and do that. It's really worth the extra effort, and it's not that much effort to begin with.

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks… Tom. :slight_smile:

Hi guys, thank you all for the replies (13600 is my date of birth).
I found out what is the problem: the button. If i connect the button like the 2 picture it works, but if i connect the button like the 1 picture it doesn’t. I tried to swap it and try again, but it won’t turn on the LED. Here is the sketch: void setup() {
pinMode(3, OUTPUT);
pinMode(2, INPUT);
}
int pulsante = 0;
void loop() {
pulsante = digitalRead(2);
if (pulsante == LOW)
{
digitalWrite(3, LOW);
}
else {
digitalWrite(3, HIGH);
}
}

Hi,
Didn't you try this back at post #9

Is the button wired correctly as the pins of those tactile switches need to be configured correctly.
Lift the button out and use a piece of wire instead of your button to see if it works.

Tom... :slight_smile:

Tactile switches always get me, first time I try to wire them up. Hadn't thought of using diagonally opposite corners before. Nice one.

TomGeorge:
Hi,
Didn't you try this back at post #9Tom... :slight_smile:

Yes, or at least i tried to light up a LED with just the breadboard, to check if the button was the problem.

Baroch130600:
Yes, or at least i tried to light up a LED with just the breadboard, to check if the button was the problem.

Did it work?