# Arduino LED PWM control with a pushbutton used for switching between different brightness settings

**URL:** <https://forum.arduino.cc/t/arduino-led-pwm-control-with-a-pushbutton-used-for-switching-between-different-brightness-settings/1375217>\
**Category:** Programming\
**Created:** [April 21, 2025, 1:09am UTC](https://forum.arduino.cc/t/arduino-led-pwm-control-with-a-pushbutton-used-for-switching-between-different-brightness-settings/1375217 "2025-04-21T01:09:04Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 21, 2025, 1:47am UTC](https://forum.arduino.cc/t/arduino-led-pwm-control-with-a-pushbutton-used-for-switching-between-different-brightness-settings/1375217/2 "2025-04-21T01:47:16Z")

</div>

> [@breadboardboi](#):
>
> ```arduino
> else if (brightnessCount = 1) {
> analogWrite(backLED, 100);
> analogWrite(frontLED, 100);
> Serial.print("1");
> }
> 
> ```

This will always be true because you are using '=' for assignment rather than using '==' testing for equality.

[https://docs.arduino.cc/language-reference/en/structure/arithmetic-operators/assignment/](https://docs.arduino.cc/language-reference/en/structure/arithmetic-operators/assignment/)

versus:

[https://docs.arduino.cc/language-reference/en/structure/comparison-operators/equalTo/](https://docs.arduino.cc/language-reference/en/structure/comparison-operators/equalTo/)

That confusion is one major problem. Another is that you never read the button with a `digitalRead(brightnessButton)` A third is that pin4 is left floating.

Maybe look at the "How to Wire and Program a Button" stuff from here:

> [@Best first 5 topics to read to reach Trust Level 1?](https://forum.arduino.cc/t/best-first-5-topics-to-read-to-reach-trust-level-1/1287592/1):
>
> - [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) by @PerryBebbington -- to learn how to post productive questions.
> - [How to Wire and Program a Button](https://forum.arduino.cc/t/how-to-wire-and-program-a-button/1196000) -- A discussion of the [official Arduino "How to Wire and Program a Button" tutorial](https://docs.arduino.cc/built-in-examples/digital/Button/) that goes with the Arduino IDE's built-in [File/Examples/02Digital/Button.ino](https://github.com/arduino/arduino-examples/blob/main/examples/02.Digital/Button/Button.ino) example. This thread shows some concerns about the tutorial and advice about how to wire up a button safer and easier.
> - [Planning and Implementing an Arduino Program](https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364/2) by @Robin2-- A great way to plan your program to be flexible, easy to read, understand, maintain, and extend. ( Live Sim: [Lesson A](https://wokwi.com/projects/407398005837428737) ... [Lesson G](https://wokwi.com/projects/407397568486835201) )
> - [Demonstration code for several things at the same time](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158) by @Robin2-- An example running a couple blinking LEDS, a button and two servos, all independently. ([Live Simulation](https://wokwi.com/projects/366664987128970241))
> - (Off-site: [Adafruit: Multiasking the Arduino Part 1](https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview), [Part 2](https://learn.adafruit.com/multi-tasking-the-arduino-part-2/overview))
> - [Combining multiple non-blocking codes](https://forum.arduino.cc/t/combining-multiple-non-blocking-codes/1305885)
> 
> - [Flashing multiple LEDs at the same time](https://forum.arduino.cc/t/flashing-multiple-leds-at-the-same-time/1065564) (thread by @PerryBebbington & others) -- several different styles of blinking leds. These are a collection of some natural extensions to the [Blink.ino](https://docs.arduino.cc/built-in-examples/basics/Blink/) and [BlinkWithoutDelay.ino](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/) . There's more than one way to do it. (And if you can blink LEDs, you can run a motor, trigger a device, or do many other tasks) Also, these multiple ways of doing the same thing task might help folks understand the important concepts behind the often-recommended BWOD example.
> - (Off-site: [@nickgammon on blink](https://gammon.com.au/blink))

And for counting, try to understand the State Change Detection example from here:

> [@Wokwi Simulations for Arduino Built-In Examples](https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754/1):
>
> - [How to Wire and Program a Button tutorial](https://docs.arduino.cc/built-in-examples/digital/Button/)
> - [Simulation](https://wokwi.com/projects/409910612492751873)
> - [forum discussion](https://forum.arduino.cc/t/how-to-wire-and-program-a-button/1196000) -- Some ways are easier and safer than others. Read the forum discussion and the InputPullupSerial example.
> - [Debounce on a Pushbutton tutorial](https://docs.arduino.cc/built-in-examples/digital/Debounce/)
> - [Simulation](https://wokwi.com/projects/409926783352705025) -- toggle an LED after the switch stops bouncing.
> - [InputPullupSerial tutorial](https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial/)
> - [Simulation](https://wokwi.com/projects/409927191742706689)
> - [State Change Detection (Edge Detection) for pushbuttons tutorial](https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection)
> - [Simulation](https://wokwi.com/projects/391475222059330561) -- Acts on the changes when a button is pressed or released, rather then the level.
> - [forum discussion](https://forum.arduino.cc/t/state-change-detection-edge-detection-for-pushbuttons/1231632)

The state change detection protects from repeatedly triggering the if() statement for as long as the button is active, instead, acting only when the button changes from being pressed to not-being-pressed & vice versa.

Work through the examples and learn how to understand what the code is doing, and then maybe you can work through your code and make sure it does what you want.

---

_[View the full topic](https://forum.arduino.cc/t/arduino-led-pwm-control-with-a-pushbutton-used-for-switching-between-different-brightness-settings/1375217)._
