How to get LED2 to turn off when LED is on. *faceroll*

This has probably been asked before, but my google-fu is lacking and I’m coming up empty-handed.
I’m EXTREMELY new to Arduino. I’m currently making a shortcut keyboard of sorts. All keys work and I currently have it set to light up an LED when a key is pressed, and the LED turns off when the button is released. I’d like to add a backlight LED that is on all the time but turns off while the keystroke LED in on, then turns back on again when the key is released.

I’m aware that the "If (LED===…) Is incorrect as it doesn’t work. Made sense in my head. My Leonardo hasn’t a clue what I want from it apparently.

I’ve read about the INPUT_PULLUP to save my LED from overcurrent death, but my code doesn’t work so I’m not sure it even works. Also, it’s not an input so I haven’t much of a clue.

#include <Keyboard.h>

#define LED 13
#define LED2 11

int delaytime = 250;

void setup() {
 // put your setup code here, to run once:
 pinMode(2, INPUT_PULLUP); //Bottom left button 1
 pinMode(3, INPUT_PULLUP); //Button 2
 pinMode(4, INPUT_PULLUP); //Button 3
 pinMode(5, INPUT_PULLUP); //Button 4
 pinMode(6, INPUT_PULLUP); //Button 5
 pinMode(7, INPUT_PULLUP); //Button 6
 pinMode(8, INPUT_PULLUP); //Button 7
 pinMode(9, INPUT_PULLUP); //Top Right Button 8
 pinMode(LED, OUTPUT);//set pin as output
 pinMode(LED2, OUTPUT);
}

void loop() {
 
 // put your main code here, to run repeatedly:
 int button2 = digitalRead(2);
 int button3 = digitalRead(3);
 int button4 = digitalRead(4);
 int button5 = digitalRead(5);
 int button6 = digitalRead(6);
 int button7 = digitalRead(7);
 int button8 = digitalRead(8);
 int button9 = digitalRead(9);

 if (LED==LOW){digitalWrite(LED2, HIGH);}\\Yes, it's trash and incorrect. I know.
 if (LED==HIGH){digitalWrite(LED2, LOW);}
 
  
//Button 1
if (button2 == LOW)
{
 digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(194);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED,LOW);
}

//Button 2
if (button3 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(195);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 3
if (button4 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(196);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 4
if (button5 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(197);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 5
if (button6 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(198);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 6
if (button7 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(199);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 7
if (button8 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(200);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

//Button 8
if (button9 == LOW)
{
 digitalWrite(LED, HIGH);
 Keyboard.press(128);
 Keyboard.press(130);
 Keyboard.press(201);
 delay(delaytime);
 Keyboard.releaseAll();
 digitalWrite(LED, LOW);
}

 }

If you know how but would rather I figure it out on my own, please link a resource that can teach me.

There’s gotta be a way to do with software, right?

This is nearly correct

if (LED==LOW){digitalWrite(LED2, HIGH);}\\Yes, it's trash and incorrect. I know.
 if (LED==HIGH){digitalWrite(LED2, LOW);}

but what you are using in your IF test is the number of the LED pin. Try this

if (digitalRead(LED) ==LOW){digitalWrite(LED2, HIGH);}
 if (digitalRead(LED)==HIGH){digitalWrite(LED2, LOW);}

If you use more meaningful names for your variables this sort of problem would have been more obvious. For example instead of

#define LED 13

suppose you had

#define LEDpin 13

You could also simplify your code to this

if (digitalRead(LEDpin) == LOW){
     digitalWrite(LED2pin, HIGH);
}
else { 
     digitalWrite(LED2pin, LOW);
}

And I find it much easier to read the code for IF and WHILE statements when it is spread out on different lines

...R

So I’ve made your recommended changes to the code, but I find LED2pin won’t turn off on keystroke. I also tried to change it a bit to:

if (digitalRead(LEDpin) == HIGH){
     digitalWrite(LED2pin, LOW);
}
else { 
     digitalWrite(LED2pin, HIGH);
}

I even went as far as to try adding “delay(delaytime);”, though I figured it was a long shot.

Code as it currently stands:

#include <Keyboard.h>

#define LEDpin 13
#define LED2pin 11

int delaytime = 250;

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT_PULLUP); //Bottom left button 1
  pinMode(3, INPUT_PULLUP); //Button 2
  pinMode(4, INPUT_PULLUP); //Button 3
  pinMode(5, INPUT_PULLUP); //Button 4
  pinMode(6, INPUT_PULLUP); //Button 5
  pinMode(7, INPUT_PULLUP); //Button 6
  pinMode(8, INPUT_PULLUP); //Button 7
  pinMode(9, INPUT_PULLUP); //Top Right Button 8
  pinMode(LEDpin, OUTPUT);//set pin as output
  pinMode(LED2pin, OUTPUT);
}

void loop() {

  // put your main code here, to run repeatedly:
  int button2 = digitalRead(2);
  int button3 = digitalRead(3);
  int button4 = digitalRead(4);
  int button5 = digitalRead(5);
  int button6 = digitalRead(6);
  int button7 = digitalRead(7);
  int button8 = digitalRead(8);
  int button9 = digitalRead(9);

  if (digitalRead(LEDpin) == HIGH) {
    digitalWrite(LED2pin, LOW);
    delay(delaytime);
  }
  else {
    digitalWrite(LED2pin, HIGH);
  }

  //Button 1
  if (button2 == LOW)
  {
    digitalWrite(LEDpin, HIGH);   // turn the LED on (HIGH is the voltage level)
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(194);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 2
  if (button3 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(195);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 3
  if (button4 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(196);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 4
  if (button5 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(197);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 5
  if (button6 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(198);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 6
  if (button7 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(199);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 7
  if (button8 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(200);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

  //Button 8
  if (button9 == LOW)
  {
    digitalWrite(LEDpin, HIGH);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(201);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
  }

}

Any idea what could be causing it to stay on with keystoke?

Keep in mind that digitalRead() is relatively slow. It's faster and uses less memory to store the pin state in a variable. I can't say whether that matters to your application, but I don't think this is generally best practices.

I also don't see why you even need to determine the pin state. You could simply set both LEDs at the same time:

digitalWrite(LED, HIGH);
digitalWrite(LED2, LOW);

To reduce the amount of duplicated code, you could use a function:

void setLEDs(const byte keyLEDstate) {
  digitalWrite(LED, keyLEDstate);
  // set the backlight
  if (keyLEDstate == HIGH) {
    digitalWrite(LED2, LOW);
  }
  else {
    digitalWrite(LED2, HIGH);
  }
}

It's questionable whether the function is really warranted in this case of only replacing two simple lines of code.

Chocolateer:
I've read about the INPUT_PULLUP to save my LED from overcurrent death, but my code doesn't work so I'm not sure it even works. Also, it's not an input so I haven't much of a clue.

If you set the LED pins as INPUT and then do digitalWrite(LED, HIGH), that will connect the pin to 5 V via an internal ~50 kohm resistor. The resistor will indeed limit the current to the LED. 50k is much higher than the typical current limiting resistor for an LED so it will be quite dim. It's much more common to use the pin in OUTPUT mode with an external resistor, which allows you to select the value you prefer (but don't exceed the current rating of the pin and resistor).

I think your problem is that whenever a button is pressed you switch LED on, delay for 250ms (blocking the processor) and then switch it off. So when you get back to the top of the loop to check LED is ALWAYS off. And so LED2 is always on. LED may get switched back on almost immediately so you don't notice the off period but by then it's too late, you've already done the check!

Steve

slipstick:
I think your problem is that whenever a button is pressed you switch LED on, delay for 250ms (blocking the processor) and then switch it off. So when you get back to the top of the loop to check LED is ALWAYS off. And so LED2 is always on. LED may get switched back on almost immediately so you don't notice the off period but by then it's too late, you've already done the check!

Agreed. I had not looked that far through the original code.

I reckon the backlight must be switched at the same time as the other LED - so something like

  if (button2 == LOW)
  {
    digitalWrite(LEDpin, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(backLightLedPin, LOW);
    Keyboard.press(128);
    Keyboard.press(130);
    Keyboard.press(194);
    delay(delaytime);
    Keyboard.releaseAll();
    digitalWrite(LEDpin, LOW);
    digitalWrite(backLightLedPin, HIGH);
  }

...R

slipstick:
I think your problem is that whenever a button is pressed you switch LED on, delay for 250ms (blocking the processor) and then switch it off. So when you get back to the top of the loop to check LED is ALWAYS off. And so LED2 is always on. LED may get switched back on almost immediately so you don't notice the off period but by then it's too late, you've already done the check!

Steve

So if I'm understanding correctly, having a delay in the "if" will cause the whole loop to pause causing the "if LED LOW, then LED2 HIGH, etc." to not even realized LEDpin was on at all? That does actually make sense. Derp. Thank you.

I added the changes as posted by robin so that each LED state change is written into each button's IF section, and removed the previously mentioned "IF, THEN, ELSE" command. Works properly. Only odd thing is that LEDbacklightpin does not turn on at the start of the code. Only comes on after the first keystroke. But that's led me to another idea. I'm going to add some sort of idle timer so that the backlight will turn off after a set amount of time of inactivity. I think at that point it may be better to set the LED's state as a variable, just like what was recommended by Pert. I'm going to attempt to figure this out on my own. I'm hoping I don't need to make a new thread for this as well. Haha.

Thank you all for your time, knowledge, and assistance in solving the issue.

Cheers!