# If Statement problem

**URL:** <https://forum.arduino.cc/t/if-statement-problem/1253835>\
**Category:** Programming\
**Created:** [April 30, 2024, 3:34am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835 "2024-04-30T03:34:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 3:34am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/1 "2024-04-30T03:34:23Z")

</div>

Trying to get one LED to copy what another LED does?

Attempting to learn coding, when I put the "if" statement in the led1 does not light up.

What am I doing wrong?

```arduino
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int delayH = 500;
int delayL = 500;
int delayBreak = 50;

void setup() {
  // put your setup code here, to run once:
if (led1 = HIGH) {
  digitalWrite (3, HIGH);
}

  pinMode(led1, OUTPUT); // initiates led pin as an output
  pinMode(led2, OUTPUT); // ""
  pinMode(led3, OUTPUT); // ""
  pinMode(led4, OUTPUT); // ""
}

void loop() {
  // put your main code here, to run repeatedly:

  //flash sequence left to right of LEDs
  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  delay(delayBreak); //provides a delay as per int delayBreak

  //flash sequence right to left of LEDs
  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL);

  delay(delayBreak); //provides a delay as per int delayBreak
}

```

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [April 30, 2024, 3:39am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/2 "2024-04-30T03:39:37Z")

</div>

> [@ardufvdh](#):
>
> `if (led1 = HIGH) {`

You need == in a compare. What you wrote was assign HIGH to led1

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 30, 2024, 3:52am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/3 "2024-04-30T03:52:18Z")

</div>

In the IDE, go to File, and turn all compiler warninngs on. Next time you leave out an =, you will get a warning.

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:08am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/4 "2024-04-30T04:08:36Z")

</div>

Cheers JimLee, that fixed the led1 not switching on but my pin 3 is not switching on (I have an LED in pin 3)

```arduino

int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int delayH = 500;
int delayL = 500;
int delayBreak = 50;

void setup() {
  // put your setup code here, to run once:
if (led1 == HIGH) {
  digitalWrite (3, HIGH);
}

  pinMode(led1, OUTPUT); // initiates led pin as an output
  pinMode(led2, OUTPUT); // ""
  pinMode(led3, OUTPUT); // ""
  pinMode(led4, OUTPUT); // ""
  pinMode(3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  //flash sequence left to right of LEDs
  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  delay(delayBreak); //provides a delay as per int delayBreak

  //flash sequence right to left of LEDs
  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL);

  delay(delayBreak); //provides a delay as per int delayBreak
}`

```

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:12am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/5 "2024-04-30T04:12:20Z")

</div>

Had a look in File, can't find where to change settings for compiler warnings?

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [April 30, 2024, 4:12am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/6 "2024-04-30T04:12:29Z")

</div>

> [@ardufvdh](#):
>
> ```arduino
> if (led1 == HIGH) {
> digitalWrite (3, HIGH);
> }
> 
> ```

This part is in setup put this part in loop

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [April 30, 2024, 4:13am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/7 "2024-04-30T04:13:44Z")

</div>

> [@ardufvdh](#):
>
> `int led1 = 13;`  
> ...  
> ...
> 
> ```arduino
> if (led1 = HIGH)
> 
> ```

What is `led1`? Looking at the number, it's a pin.

Even if you do the correct compare in the if condition, the condition will always evaluate to false. Reason is that HIGH is defined as 1 and 13 is never equal to 1.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [April 30, 2024, 4:14am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/8 "2024-04-30T04:14:45Z")

</div>

> [@ardufvdh](#):
>
> Had a look in File

It's file / preferences.

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [April 30, 2024, 4:15am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/9 "2024-04-30T04:15:11Z")

</div>

`File -> Preferences -> Compiler warnings `  
Set it to ALL

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:15am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/10 "2024-04-30T04:15:40Z")

</div>

Got it:)

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:16am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/11 "2024-04-30T04:16:57Z")

</div>

So if I change pin 13 to pin 9 it should worK?

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [April 30, 2024, 4:18am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/12 "2024-04-30T04:18:23Z")

</div>

You need to use code like this

```arduino
if (digitalRead(led1) == HIGH) {
  digitalWrite (3, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [April 30, 2024, 4:19am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/13 "2024-04-30T04:19:19Z")

</div>

Also put this part in loop

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:21am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/14 "2024-04-30T04:21:08Z")

</div>

Still no good:(

```arduino
int led1 = 9;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int delayH = 500;
int delayL = 500;
int delayBreak = 50;

void setup() {
  // put your setup code here, to run once:

  pinMode(led1, OUTPUT); // initiates led pin as an output
  pinMode(led2, OUTPUT); // ""
  pinMode(led3, OUTPUT); // ""
  pinMode(led4, OUTPUT); // ""
  pinMode(3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

 if (led1 == HIGH) {
  digitalWrite (3, HIGH);
} 
  
  //flash sequence left to right of LEDs
  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  delay(delayBreak); //provides a delay as per int delayBreak

  //flash sequence right to left of LEDs
  digitalWrite(led4, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led4, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led2, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led2, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

  digitalWrite(led1, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  delay(delayL);

  delay(delayBreak); //provides a delay as per int delayBreak
}

```

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [April 30, 2024, 4:21am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/15 "2024-04-30T04:21:30Z")

</div>

> [@ardufvdh](#):
>
> Trying to get one LED to copy what another LED does?

To make led3 do what led1 is doing with your delay-based coding, you'll have to duplicate the writes:

```arduino
  digitalWrite(led1, HIGH); //digital writes led as a high
  digitalWrite(led3, HIGH); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  digitalWrite(led3, LOW); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

```

You could treat led1 as a button and write:

```arduino
  digitalWrite(led1, HIGH); //digital writes led as a high
  digitalWrite(led3, digitalRead(led1)); //digital writes led as a high
  delay(delayH); //provides a delay as per int delayH
  digitalWrite(led1, LOW); //digital writes led as a low
  digitalWrite(led3, digitalRead(led1)); //digital writes led as a low
  delay(delayL); //provides a delay as per int delayL

```

...but using delays means that you have to make the changes at the same time--in between the delays.

Or maybe I don't understand the behavior you want.

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:23am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/16 "2024-04-30T04:23:50Z")

</div>

Just trying to get an led on pin 3 to copy what led1 does.

---

<div class="post-metadata">

**Author:** ![AresXT](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aresxt/32/1201192_2.png) [@AresXT](https://forum.arduino.cc/u/AresXT)\
**Post date:** [April 30, 2024, 4:24am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/17 "2024-04-30T04:24:09Z")

</div>

Could you explain how do want to light the led your current code will not light up the led in 3 rd pin

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 30, 2024, 4:24am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/18 "2024-04-30T04:24:47Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/6/a/06a5f9f15f0ce361ab01a64e81dcbb2922ae5506.png)

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:26am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/19 "2024-04-30T04:26:55Z")

</div>

Just want pin 3 to replicate whatever led1 is. Might have to use "if else" perhaps??

---

<div class="post-metadata">

**Author:** ![ardufvdh](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardufvdh/32/1064012_2.png) [@ardufvdh](https://forum.arduino.cc/u/ardufvdh)\
**Post date:** [April 30, 2024, 4:28am UTC](https://forum.arduino.cc/t/if-statement-problem/1253835/20 "2024-04-30T04:28:05Z")

</div>

I'll have to leave it a bit, got to drop the boss off at the airport.

Thanks guys.

[Next page](https://forum.arduino.cc/t/if-statement-problem/1253835.md?page=2)
