Confusion about programming Logic

Hello!

I am new to Arduino, and I am starting small and trying to modify basic programs.
Recently I have been blinking 2 leds with the intention of turning on one of the leds with an if statement. It is working, just that in a way that I think it is not supposed to.

Here is my doubt, when I use the if statement I was expecting to get the second led on when the first led turns on and shut down the second led when the first led turns off. Something like if led 12 bigger than zero, turn led 5 on, if led 12 is equal to zero turn led 5 off. Then only logic that I have got to execute well is if led 12 is bigger than zero turn led 5 on, and if led 12 is bigger than zero turn led 5 off, and it doesn't make sense to me. I tried the code in a second board and it is the same behaviour. Could you help me find out whats going on? The full code is below.

Here is the program

int led5 = 5;
int led12 = 12;

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

pinMode(led12, OUTPUT);
pinMode(led5, OUTPUT);

}

void loop() {

digitalWrite(led12, HIGH);
if (led12 > 0) {digitalWrite(led5, HIGH);}
delay(1000);

digitalWrite(led12, LOW);
if (led12 > 0) {digitalWrite(led5, LOW);}
delay(1000);

Please post any code inside the code tags </> - it makes it easier to read and you will get more help.

  if (led12 > 0) 
  {
    digitalWrite(led5, HIGH);
  }

led12 = 12... so this will always be true... you are just comparing 2 integers.

What I think you want is...

  if (digitalRead(led12) > 0) 
  {
    digitalWrite(led5, HIGH);
  }
1 Like

Hi @okaido
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

If you remove both if statements, you will find your code will still do as you have intended.
led12 in your code is a constant, infact it is a pin name/value, and has no real use like you have it in an if statement.
Try this;

int led5 = 5;
int led12 = 12;

void setup()
{
  // put your setup code here, to run once:
  pinMode(led12, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop()
{
  digitalWrite(led12, HIGH);
  digitalWrite(led5, HIGH);
  delay(1000);
  digitalWrite(led12, LOW);
  digitalWrite(led5, LOW);
  delay(1000);
}

Tom... :smiley: :+1: :coffee: :australia:

1 Like

Hello
Take some time and start with the examples provided by the IDE to understand the know how of coding.
Have a nice day and enjoy coding in C++.

1 Like

I would write:

void loop() {

digitalWrite(led12, HIGH);
digitalWrite(led5, digitalRead(led12)); // write copy of 12 to 5
delay(1000);

digitalWrite(led12, LOW);
digitalWrite(led5, digitalRead(led12));
delay(1000);
}
1 Like

Hello
Did you run the sketch?

Yes. (After I fixed it.), TNX, :blush:

The pin is configured for output

So? That doesn't mean you can't read its current state.

So why bother configuring pin for input at all?

Cos I really don't think the OP will notice.

Thank you!! :sunglasses: :raised_hands:

Hello, yes, I ran the sketch but it didn´t help me much figuring out the issue

I think this is right what I want, I will try it. Thank you!

I just tried it, works perfectly! thank you

So it doesn't drive anything - its high impedance (floating). Outputs are always driven hard to 0V or to 5V (hard means with about 40 ohms of internal resistance).
An input has an impedance somewhere up in the 1000000 megaohms region which is not the same thing at all, meaning the pin is basically a voltage sniffer.