Reverse button state

Hello there ,

First of all , i am a complete beginner , just bought my arduino board yesterday.

I was trying to do a simple project with an led and a button .When you presss a button the 12 pin led should turn on .The thing is when i start the program the LED is allready on and when i press the button the LED turns off. i want it the other way around .

So what am i doing wrong ?

int led=12;
int button=2;

void setup (){
 pinMode(led,OUTPUT);
 pinMode(button,INPUT);
 
}

void loop() {
  
  digitalWrite(led,LOW);
  
 int state=digitalRead(button);

 if (state==LOW){
   digitalWrite(led,LOW);}
   else
    {digitalWrite(led,HIGH);
    }
}

How do you have the button wired? If it has a pullup resistor it will be high normally ie unpressed and low when pressed.

This first call to digitalWrite at the start of loop() doesn't achieve anything useful - it just makes the LED flicker when it's supposed to be on.

  digitalWrite(led,LOW);

Your problem is to reverse the logic in this block:

 if (state==LOW){
   digitalWrite(led,LOW);}
   else
    {digitalWrite(led,HIGH);
    }
}

There are lots of ways you could do that, all of them valid.

You could change it to:

 if (state==HIGH){

or

 if (state!= LOW){

Or you could swap these two statements over:

digitalWrite(led,LOW);
digitalWrite(led,HIGH);

Or you could physically rewire your switches to that they read HIGH when the switch is closed - this would involve swapping the switch and the pull-up resistor.

Or you could physically rewire the LEDs so that they illuminate when the output is LOW instead of HIGH.

Although they're all valid options, my preference would be to get rid of the external pull-up resistor and enable the internal pull-up instead, leave the LED wiring unchanged so that output HIGH means LED on, and put the right output state inside each branch of the IF statement.

Also, my preference is to lay your code out with each { and } on separate lines with matching pairs indented by the same amount and code between them indented one extra level. It makes it far easier to understand the control structure of your code, which will become more important as your code gets more complex. nIf you put the { and } on separate lines and use the tools / auto format command, it will take care of the indentation for you.

a tip, use CTRL-T in the IDE before copying code, it does automatic indentation making your code a bit more readable
also adding some spaces around operators makes the code more readable.

int led = 12;
int button = 2;

void setup ()
{
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void loop()
{
  int state = digitalRead(button);
  if (state == LOW)
  {
    digitalWrite(led, LOW);
  }
  else
  {
    digitalWrite(led, HIGH);
  }
}

notice the readability?

Thanks for reply ,

I am not sure if the external resistor is a pull up one , but i will try to find out how to use the internal resistor .

I was following a project from "Beginning Arduino " book , but i can't just make the bloody LED work as i want to .

Thanks !

Terminator2:
Thanks for reply ,

I am not sure if the external resistor is a pull up one

Well how's it connected? If it goes from the pin to ground (and the switch is on the 5v side) it's pull-down. If it goes from the pin to 5v (and the switch is on the ground side) it's a pull-up.

i've tryed both ways ...but no luck .

if there is no problem with code then i am doing something wrong here with the circuit.

Can you have a look at my circuit please ?

Hello and welcome :slight_smile:

Follow this tutorial:

http://arduino.cc/en/tutorial/button

guix:
Hello and welcome :slight_smile:

Follow this tutorial:

http://arduino.cc/en/tutorial/button

... and you'll see from the pic there that with a pull-up or pull-down resistor, the pin is connected between the resistor and the switch

i've copy pasted the code from the tutorial website , but still the same problem. Even when Arduino is not connected to the external circuit , the pin13 LED is allready on and it should be OFF .

Your code and circuit don't mention the LED on pin 13.... it often is on at startup.

I am sorry ,

Initially i was using pin12 LED OUTPUT for my circuit , but i was trying to follow the tutorial on the website you guys gave me and in the tutorial they use the internal pin13 LED .
But i get the same problem ; whenever i upload the program ( copy -paste from the tutorial ) the pin13 LED is allready on .

Yes, it's a design thing.... seems that pin 13's LED usually starts on. Mine does.....

Edit... This page explains the pull-up / pull-down resistor thing quite nicely, btw.