# Reversing LED state

**URL:** <https://forum.arduino.cc/t/reversing-led-state/1364808>\
**Category:** General Guidance\
**Created:** [March 18, 2025, 9:24am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808 "2025-03-18T09:24:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![damnsam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/damnsam/32/1202345_2.png) [@damnsam](https://forum.arduino.cc/u/damnsam)\
**Post date:** [March 18, 2025, 9:24am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/1 "2025-03-18T09:24:34Z")

</div>

Hi there, I feel a bit silly for asking as I feel like this should be a very straightforward and simple change to make in comparison to what I needed help with earlier, but here I am anyway.

I have been working on a circuit that consists of six FSR sensors, a push button and an LED. With the push of the button, I can stop the recording of data; in the serial monitor, the lines would be blank, and the serial plotter would pause. Now I would like to add an LED so that I can see when the data is being recorded more visually.

The problem is that the LED state is reversed from what I would like it to do. Meaning that the LED is off when the data is being recorded and turning on when the recording stops. Is there a way to reverse the LED state? All attempts I have made to change the states have resulted in the LED not working at all. I have attached the current code and setup below.

Thank you in advance and have a lovely day!

```arduino
// References
// Innitial starting code for reading the FSR's https://forum.arduino.cc/t/separating-fsr-values-and-labelling-graph/1275362 
// Innitial starting code for including a LED and button https://roboticsbackend.com/arduino-turn-led-on-and-off-with-button/  
// Own post on Arduino Forum https://forum.arduino.cc/t/arduino-only-showing-3-out-of-6-outputs-on-serial-plotter/1361263 
// Innitial cirquit reference for the FSR's https://forum.arduino.cc/t/connecting-8-fsrs-to-arduino-mega/618577  
// Innitial cirquit source for the LED and Button https://techexplorations.com/guides/arduino/sensors/button/  

#define LED_PIN 7
#define BUTTON_PIN 8

// Defining pins
const byte PinFsr [] = {A0, A1, A2, A3, A4, A5}; // Location of the six FSR's
const byte Nfsr = sizeof(PinFsr); // Number of bytes occupied

byte prevButtonState = HIGH; // Button being pressed 
byte buttonState = LOW; // Button not being pressed

byte lastButtonState = LOW;
byte loopRunning = LOW; 
byte ledState = LOW;

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

void setup() {
  Serial.begin (9600); // Begin serial communication at a baud rate of 9600
  
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);

  
}

void loop() {

if(loopRunning) {
  
    for (byte n = 0; n < Nfsr; n++) { // Reading data from all FSR's simultaneously
        int fsr = analogRead (PinFsr [n]);
        Serial.print (fsr);Serial.print(" ");
        Serial.print (","); // Separate values with a comma
    }
  }
    
    Serial.println (); // Display values on Serial Plotter and Serial Monitor
    delay (500); // Delay 500 Milliseconds

if (millis() - lastTimeButtonStateChanged > debounceDuration) {
  byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastButtonState) {
     lastTimeButtonStateChanged = millis();
     lastButtonState = buttonState;
     if (buttonState == LOW) {
       ledState = (ledState == HIGH) ? LOW: HIGH;
       digitalWrite(LED_PIN, ledState);
     }
   }
  }

  buttonState = digitalRead(BUTTON_PIN); // If the current button state differs from previous, toggle other way
  if((buttonState==LOW) && (prevButtonState==HIGH)) {
    loopRunning = !loopRunning;
  }

    prevButtonState=buttonState;

      pinMode(LED_PIN, OUTPUT); // LED works as an output
      pinMode(BUTTON_PIN, INPUT); // Button works as an imput
}

```

 ![Schermafbeelding 2025-03-17 152150](https://europe1.discourse-cdn.com/arduino/original/4X/5/0/b/50b72b1ceb84e9d70675825c590a13ba04f9748e.png)

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [March 18, 2025, 10:15am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/2 "2025-03-18T10:15:04Z")

</div>

You only need to set pin mode once in setup() so remove them from loop.  
To change the LED state just reverse the LOW : HIGH  
ledState = (ledState == HIGH) ? LOW: HIGH

Also your LED is **Backwards**

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [March 18, 2025, 10:24am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/3 "2025-03-18T10:24:26Z")

</div>

> [@damnsam](#):
>
> the LED is off when the data is being recorded and turning on when the recording stops. Is there a way to reverse the LED state?

Well, first thing I noticed is you use a timer for the debounce: I suggest you to use a hardware debounce and get rid of that timer.  
Then, please use a better code indentation, it helps you (and us) to ease the code flow interpretation.  
Then, here you handle the button and the led:

```arduino
  byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastButtonState) {
     lastTimeButtonStateChanged = millis();
     lastButtonState = buttonState;
     if (buttonState == LOW) {
       ledState = (ledState == HIGH) ? LOW: HIGH;
       digitalWrite(LED_PIN, ledState);
     }
   }
  }

  buttonState = digitalRead(BUTTON_PIN); // If the current button state differs from previous, toggle other way
  if((buttonState==LOW) && (prevButtonState==HIGH)) {
    loopRunning = !loopRunning;
  }

    prevButtonState=buttonState;

      pinMode(LED_PIN, OUTPUT); // LED works as an output
      pinMode(BUTTON_PIN, INPUT); // Button works as an imput
}

```

I can't get why you added all this stuff here, e.g. reading BUTTON\_PIN twice and a lot of if()s, and redundant variables. And you don't need to use "pinMode()" on the loop(), unless you really need to do so. Get rid of those two last lines.

What you need to do here when the button has been pressed is just "reverse" the operating flag (loopRunning) and LED output accordingly. And the best tip is to make use of comments to clarify what that code portion does (describe it to you first, because if you take an old code you could have forgot the logic behind, but also for us to better understand why you used certain instructions and variables).  
Ad use "boolean" variables if you need just flags, like "loopRunning".

So, you don't need all that mess, try just this block I wrote "on the fly" for you:

```arduino
  // Ignore the button unless the "debounce time" is over
  if (millis() - lastTimeButtonStateChanged > debounceDuration) {
    // Ok, what's the button state?
    byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastButtonState) {
      // State has changed, record it
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
    }
    // Now if the button has been pressed, reverse the loop status and LED
    if (buttonState == HIGH) {
      loopRunning = !loopRunning;
      digitalWrite(LED_PIN, loopRunning);
    }
  }

```

PS: when in trouble or you need some more deeper debug procedure, I always advise to even use emulators like "Wokwi" or "TinkerCAD Circuits" to make debug sessions easier and quicker than using the "real" board...

---

<div class="post-metadata">

**Author:** ![damnsam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/damnsam/32/1202345_2.png) [@damnsam](https://forum.arduino.cc/u/damnsam)\
**Post date:** [March 18, 2025, 11:11am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/4 "2025-03-18T11:11:44Z")

</div>

@jim-p @docdoc Thank you both for your observations and suggestions!

- I have switched around the LED to be in the correct position (not one of my brightest moments)
- I have tried to change the LED state by reversing LOW : HIGH; the LED stopped doing anything altogether.
- I made an attempt to make the code neater by using indentations; I'm sorry for the mess; coding etiquettes (and coding in general) are still quite new to me.
- For now, I will keep in the software debounce as I have no idea how to do a hardware one, but I will look into that.
- I tried to implement the suggestions and code provided, but the LED is not working at all. I have included the adjusted code below. Probably made a typo somewhere.

```arduino
// References
// Innitial starting code for reading the FSR's https://forum.arduino.cc/t/separating-fsr-values-and-labelling-graph/1275362
// Innitial starting code for including a LED and button https://roboticsbackend.com/arduino-turn-led-on-and-off-with-button/
// Own post on Arduino Forum https://forum.arduino.cc/t/arduino-only-showing-3-out-of-6-outputs-on-serial-plotter/1361263
// Innitial cirquit reference for the FSR's https://forum.arduino.cc/t/connecting-8-fsrs-to-arduino-mega/618577
// Innitial cirquit source for the LED and Button https://techexplorations.com/guides/arduino/sensors/button/

#define LED_PIN 7
#define BUTTON_PIN 8

// Defining pins
const byte PinFsr[] = { A0, A1, A2, A3, A4, A5 }; // Location of the six FSR's
const byte Nfsr = sizeof(PinFsr); // Number of bytes occupied

byte prevButtonState = HIGH; // Button being pressed
byte buttonState = LOW; // Button not being pressed

byte lastButtonState = LOW;
boolean (loopRunning) = LOW;
byte ledState = LOW;

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

void setup() {
  Serial.begin(9600); // Begin serial communication at a baud rate of 9600

  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  if (loopRunning) {
    for (byte n = 0; n < Nfsr; n++) { // Reading data from all FSR's simultaneously
      int fsr = analogRead(PinFsr[n]);
      Serial.print(fsr);
      Serial.print(" ");
      Serial.print(","); // Separate values with a comma
    }
  }

  Serial.println(); // Display values on Serial Plotter and Serial Monitor
  delay(500); // Delay 500 Milliseconds

  if (millis() - lastTimeButtonStateChanged > debounceDuration) { // Wait for debounce time to pass before reading button state
    byte buttonState = digitalRead(BUTTON_PIN); // Read button state
    if (buttonState != lastButtonState) { // Record state change
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
    }

    if (buttonState == HIGH) { // If button is pressed, reverse loop and LED state
      loopRunning = !loopRunning;
      digitalWrite(LED_PIN, loopRunning);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 18, 2025, 11:21am UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/5 "2025-03-18T11:21:17Z")

</div>

> [@damnsam](#):
>
> but the LED is not working at all.

I'm tuning in late. If your LED is literally on when it should be off and vice versa, this should be easy.

```arduino
      loopRunning = !loopRunning;
      digitalWrite(LED_PIN, loopRunning);

```

You could flip the controlling variable after writing to the LED:

```arduino
      digitalWrite(LED_PIN, loopRunning);
      loopRunning = !loopRunning;

```

or

```arduino
     loopRunning = !loopRunning;
     digitalWrite(LED_PIN, loopRunning ? LOW : HIGH);

```

and if that is exactly wrong, switch the rolls of LOW and HIGH, viz:

```arduino
     loopRunning = !loopRunning;
     digitalWrite(LED_PIN, loopRunning ? HIGH : LOW);

```

HTH and is that what you meant?

BTW I'll spare other comments, but

```arduino
boolean loopRunning = false;

```

_true_ and _false_ for Boolean variables; use LOW and HIGH only with _digitslWrite_() and _digitalRead_().

When you can explain why, you'll know more about the mysteries that be.

a7

---

<div class="post-metadata">

**Author:** ![damnsam](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/damnsam/32/1202345_2.png) [@damnsam](https://forum.arduino.cc/u/damnsam)\
**Post date:** [March 18, 2025, 12:03pm UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/6 "2025-03-18T12:03:53Z")

</div>

> [@alto777](#):
>
> I'm tuning in late. If your LED is literally on when it should be off and vice versa, this should be easy.

Glad to have you. And yes that is exactly the problem.

I have tried all your suggestions and adjusted the boolean (thank you for the short explanation, I do get the difference now)

Sadly, the LED is still not doing anything ☹

EDIT: I switched the LED back around on the breadboard and it seems to be working now 😃 I wish I could award all of you with the green checkmark. Nevertheless, thank you all very much your your time and patience

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [March 18, 2025, 12:22pm UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/7 "2025-03-18T12:22:04Z")

</div>

> [@alto777](#):
>
> You could flip the controlling variable after writing to the LED:

That's right. I said it was written "on the fly", so a small mistake is possible. 😉

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [March 18, 2025, 12:32pm UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/8 "2025-03-18T12:32:34Z")

</div>

> [@damnsam](#):
>
> I switched the LED back around on the breadboard and it seems to be working now

Yes, I have seen some LEDs that actually have the anode on the flat side but it's rare

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 14, 2025, 12:33pm UTC](https://forum.arduino.cc/t/reversing-led-state/1364808/9 "2025-09-14T12:33:24Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
