Arduino simple coding (if, else) statement

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
guccyi
 
Posts: 9
Joined: Mon May 13, 2013 3:50 pm

Arduino simple coding (if, else) statement

Post by guccyi »

Hello,

I'm just trying to get this if, else statement working.

Basically, I have led lights connected to pin 2 (led 1) and pin 3 (led 2),

and I want led 2 to light up every time led 1 lights up.

My coding is,

Code: Select all

int led1=2;
int led2=3;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop()
{
  if (digitalRead(led1 == HIGH))
  {
    digitalWrite(led2, HIGH);
  }
  else
 {
  digitalWrite(led2, LOW); //This line won't work.
 } 
  digitalWrite(led1, HIGH);
  delay(5000);
  digitalWrite(led1, LOW);
  delay(3000);
}
So after I upload this code, led 2 lights up whenever led 1 lights up,

but led 2 wont turn off when led 1 turns off.

PLEASE HELPPP
Last edited by adafruit_support_bill on Tue Jun 04, 2013 2:57 pm, edited 2 times in total.
Reason: Please use the 'code' button when submitting code

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino simple coding (if, else) statement

Post by adafruit_support_bill »

This line is not doing what you expect:

Code: Select all

  if (digitalRead(led1 == HIGH))
You need:

Code: Select all

  if (digitalRead(led1) == HIGH)
A simpler way is to write the whole thing in one line:

Code: Select all

   digitalWrite(led2, digitalRead(led1));

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”