IF OUTPUT IS HIGH DO CODE

What I’m trying to do is if ouput in high then do code which is turn on led2 when 1st led is on

#include <wprogram.h>
#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led1, INPUT);

}


void loop() {

  if (led1 == HIGH) {
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  }

  delay(250);
  digitalWrite(led1, HIGH);
  
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led1, LOW);

  digitalWrite(led3, LOW);



}

I, Septillion, hereby grant you to do so!

But really, what is the problem? Does not work isn't an answer :wink:

AND WHY DO YOU SCREAMMMMMMMMMMMMMMMMMMMM?!

(deleted)

Where does led1 ever get set HIGH ?

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led1, INPUT);

Is led1 an INPUT or an OUTPUT ?

Note that you can digitalRead() the state of a pin set to be an OUTPUT but not the other way round

spycatcher2k:

if (led1 == HIGH)

Perhaps

if (digitalRead(led1) == HIGH)
#include <wprogram.h>
#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led1, INPUT);

}


void loop() {

if (led1 == HIGH)
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  }

  delay(250);
  digitalWrite(led1, HIGH);
  
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led1, LOW);

  digitalWrite(led3, LOW);



}
#include <wprogram.h>
#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led1, INPUT);

}


void loop() {

if (digitalRead(led1) == HIGH)
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  }

  delay(250);
  digitalWrite(led1, HIGH);
  
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led1, LOW);

  digitalWrite(led3, LOW);
i


}

both of these code dont work as I tried them already

hipradeep@hotmail.co.uk:
dont work

What did I tell you :wink:

septillion:
Does not work isn't an answer :wink:

Tell us what does happen!

Can you please answer the questions in reply #3 ?

septillion:
What did I tell you :wink:
Tell us what does happen!

Error compiling for board Arduino Uno

hipradeep@hotmail.co.uk:
Error compiling for board Arduino Uno

Is the error a secret?

void loop() {

if (digitalRead(led1) == HIGH)
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  }

There’s your loop().
Anything after that is Not in loop() and is Not in a function.

You need to fix your pairings of { and } so that all code is within a pair of them, and also within the final } that ends loop().
CTRL-T should help you spot the pairing errors.

UKHeliBob:
Where does led1 ever get set HIGH ?

  pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led1, INPUT);



Is led1 an INPUT or an OUTPUT ?

Note that you can digitalRead() the state of a pin set to be an OUTPUT but not the other way round

I want led 1 to output HIGH but if it is HIGH then it will turn on led2

CrossRoads:

void loop() {

if (digitalRead(led1) == HIGH)
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  }



There's your loop().
Anything after that is Not in loop() and is Not in a function.

You need to fix your pairings of { and } so that all code is within a pair of them, and also within the final } that ends loop().
CTRL-T should help you spot the pairing errors.
#include <wprogram.h>
#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led1, INPUT);

}

void loop() {
  if (digitalRead(led1) == HIGH)
    delay(250);
  digitalWrite(led2, HIGH);
  delay(300);
  digitalWrite(led2, LOW);
  delay(250);
  digitalWrite(led1, HIGH);
  digitalWrite(led3, HIGH);
  delay(300);
  digitalWrite(led1, LOW);
  digitalWrite(led3, LOW);
}

I did auto format but still get an error when uploading

still get an error

you can share it - we’re friends.

#include <wprogram.h>

I haven’t seen that one for a long time. Which version of the IDE did you resurrect?

hipradeep@hotmail.co.uk:
I want led 1 to output

Then why on earth do you make it an input then?

hipradeep@hotmail.co.uk:
but still get an error when uploading

And that error is still top secret, isn't it?

PS One thing what's not top secret is your e-mail address by the way. Not very smart to use that as a public username :wink:

AWOL:
you can share it - we're friends.

that error is gone now but it doesnt do what i want it to.
i want the if statement to check if led1 is on and when its on led2 turns on!!!
but led2 is blinking regardless of led1 on or off.

  if (digitalRead(led1) == HIGH)
    delay(250);
  digitalWrite(led2, HIGH);

The only thing that's in the if is the delay() :wink:

septillion:

  if (digitalRead(led1) == HIGH)

delay(250);
  digitalWrite(led2, HIGH);



The only thing that's in the if is the delay() ;)
#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

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

}

void loop() {
  
  delay(250);
  digitalWrite(led1, LOW);
  digitalWrite(led3, HIGH);
  delay(300);
  if (digitalRead(led1) == HIGH)
    delay(250);
    digitalWrite(led2, HIGH);
    delay(300);
    digitalWrite(led2, LOW);
  digitalWrite(led1, LOW);
  digitalWrite(led3, LOW);
  
}

Still blinking regardless of what led1 is doing.

That's the same code with different indentation, isn't it?

Still, the only thing in the if() is the delay() :wink: Arduino is C/C++, not Python. The indentation is an aid for you but the compiler doesn't give a f* :wink:

#include <wiring_private.h>
#include <pins_arduino.h>

int led1 = 11;
int led2 = 8;
int led3 = 3;
int contact0 = 0;

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

}

void loop() {
  
  delay(250);
  digitalWrite(led1, HIGH);
  
  delay(300);
  if (digitalRead(led1) == HIGH){
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(300);
    
  }
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  
}

Here is the right answer.
THANK YOU