# Can someone help me with this code?

**URL:** <https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754>\
**Category:** Programming\
**Created:** [September 30, 2024, 8:32pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754 "2024-09-30T20:32:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Holzwurm56](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holzwurm56/32/1013044_2.png) [@Holzwurm56](https://forum.arduino.cc/u/Holzwurm56)\
**Post date:** [September 30, 2024, 8:32pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/1 "2024-09-30T20:32:54Z")

</div>

Hello,  
I wanted to write a small test program for an ESP32. It should be a running light and when I press button 1 the flashing interval should be 100ms, for button 2 500ms and for button 3 1000ms, unfortunately the interval is always 1000ms no matter which button I press. Can someone tell me what I'm doing wrong?  
Thank you very much  
Hans

```arduino
// Platine zum Experimentieren mit dem ESP32 Super Mini
int Pause;
int Taster1;
int Taster2;
int Taster3;

void setup() {
  pinMode(0, INPUT_PULLDOWN);
  pinMode(1, INPUT_PULLDOWN);
  pinMode(2, INPUT_PULLDOWN);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(20, OUTPUT);
  pinMode(21, OUTPUT);
  Pause = 50;
  Taster1 = LOW;
  Taster2 = LOW;
  Taster3 = LOW;
}

void loop() {
  Taster1 = digitalRead(0);
  Taster2 = digitalRead(1);
  Taster3 = digitalRead(2);
  if (Taster1 == HIGH) Pause = 100;
 
  if (Taster2 == HIGH) Pause = 500;
  
  if (Taster3 == HIGH) Pause = 1000;
  delay(Pause);
  digitalWrite(3, HIGH);
  delay(Pause);
  digitalWrite(4, HIGH);
  delay(Pause);
  digitalWrite(5, HIGH);
  delay(Pause);
  digitalWrite(6, HIGH);
  delay(Pause);
  digitalWrite(7, HIGH);
  delay(Pause);
  digitalWrite(8, HIGH);
  delay(Pause);
  digitalWrite(9, HIGH);
  delay(Pause);
  digitalWrite(10, HIGH);
  delay(Pause);
  digitalWrite(20, HIGH);
  delay(Pause);
  digitalWrite(21,HIGH);
  delay(Pause);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(20, LOW);
  digitalWrite(21, LOW);
  delay(Pause);
  Pause = 0;
}

```

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [September 30, 2024, 8:34pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/2 "2024-09-30T20:34:56Z")

</div>

The sketch is doing exactly what you told it to.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 30, 2024, 9:03pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/3 "2024-09-30T21:03:26Z")

</div>

- **Always** show us a good schematic of your proposed circuit.  
Show us good images of **your** ‘actual’ wiring.  
Give links to components.

---

<div class="post-metadata">

**Author:** ![jremington](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jremington](https://forum.arduino.cc/u/jremington)\
**Post date:** [September 30, 2024, 9:28pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/4 "2024-09-30T21:28:59Z")

</div>

Have you checked that for _your particular_ ESP32 module, GPIO pins 0, 1 and 2 are freely available as inputs, and not allocated for some other function?

> **[ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials](https://randomnerdtutorials.com/esp32-pinout-reference-gpios/)**
>
> The ESP32 comes with 48 GPIOs with multiple functions. This article intends to be a simple and easy to follow reference guide for the ESP32 GPIOs.

---

<div class="post-metadata">

**Author:** ![Holzwurm56](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holzwurm56/32/1013044_2.png) [@Holzwurm56](https://forum.arduino.cc/u/Holzwurm56)\
**Post date:** [October 1, 2024, 6:49am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/5 "2024-10-01T06:49:46Z")

</div>

I forgot to mention that I use the ESP32 Super Mini.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [October 1, 2024, 7:09am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/6 "2024-10-01T07:09:21Z")

</div>

when your code is doing LOW/HIGH illumination on these many pins, with delays, it can not read your button press. If you press and quickly release the button - then it is very unlikely that your sketch could pick it up. Try to press and hold the button.

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [October 1, 2024, 7:16am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/7 "2024-10-01T07:16:36Z")

</div>

> [@Holzwurm56](#):
>
> ` pinMode(0, INPUT_PULLDOWN);`

So your buttons are HIGH by default, when they're NOT pressed (assuming you wired the button between the GPIO and GND, as it should be).

> [@Holzwurm56](#):
>
> ` if (Taster1 == HIGH) Pause = 100;`

This will happen if the button is NOT pressed, since you're looking for the GPIO being HIGH.

Try  
` if (Taster1 == LOW) Pause = 100;`  
(same for the other keys).

Having said that, this way of reading buttons is a recipe for disaster. I'd restructure the whole program. However, it should sort of work, in principle, if you fix the logic flaw.

---

<div class="post-metadata">

**Author:** ![Holzwurm56](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holzwurm56/32/1013044_2.png) [@Holzwurm56](https://forum.arduino.cc/u/Holzwurm56)\
**Post date:** [October 1, 2024, 7:27am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/8 "2024-10-01T07:27:41Z")

</div>

I switched the inputs to 3.3 volts using the button and pressed the buttons long enough. I have now changed the inputs to pins 0, 4 and 5. The program starts with the value of button 1, when I press button 2 it changes to the value of button 2, when I press button 3 it changes to the value of button 3. If no button is pressed, the value of button 1 always works.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [October 1, 2024, 7:33am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/9 "2024-10-01T07:33:09Z")

</div>

> [@rsmls](#):
>
> Having said that, this way of reading buttons is a recipe for disaster. I'd restructure the whole program. However, it should sort of work, in principle, if you fix the logic flaw.

He can easily fix it by creating his own void my\_delay( .. ) which can read the buttons and then doing actual delay(). Not ideal but better than what he have now

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [October 1, 2024, 7:35am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/10 "2024-10-01T07:35:48Z")

</div>

> [@Holzwurm56](#):
>
> I switched the inputs to 3.3 volts using the button and pressed the buttons long enough.

That doesn't sound right. Your buttons should be wired between the GPIO and GND. No 3.3V line involved anywhere. The GPIO's are pulled up by your code.

> [@Holzwurm56](#):
>
> inputs to pins 0

Careful which pins you use. IIRC pin 0 is a special purpose pin (BOOT) on ESP32.

> [@vvb333007](#):
>
> He can easily fix it by creating his own void my\_delay( .. )

Yeah, he could. Like you said, it's still crap, but at least it's a different flavor of crap.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [October 1, 2024, 7:40am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/11 "2024-10-01T07:40:16Z")

</div>

> [@Holzwurm56](#):
>
> the interval is always 1000ms no matter which button I press.

I don't see why it would be 1000ms.

On the first run of loop(), assuming no buttons are pressed, Pause=50 and there are 12x delay(), so that's 600ms in total, not 1000ms.

On further runs of loop(), assuming no buttons are pressed, Pause=0, so the 12x delay() will have (almost) no effect. The LEDs will probably all appear to the eye to glow at low brightness. In reality they are all flashing very briefly at full brightness and loop() will repeat hundreds maybe thousands of times each second.

If a button is pressed, let's say button on pin 1, Pause=500 and the interval would be 6000ms.

My guess is this: the button on pin 2 always reads high. Maybe all 3 buttons read HIGH. Then, Pause=1000 and the interval is 12000ms. But each individual LED is lit 1000ms after the previous one and maybe this is what you meant when you said that the period is 1000ms?

So maybe your buttons are incorrectly wired and read HIGH whether they are pressed or not.

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [October 1, 2024, 7:47am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/12 "2024-10-01T07:47:10Z")

</div>

> [@PaulRB](#):
>
> I don't see why it would be 1000ms.

I think you need to have another look at the code. With no buttons pressed, the pause is first set to 100, then to 500 and ultimately to 1000ms. The button presses are only registered in the brief moment (a few nanoseconds) at the start of loop. After that, it's all delays. So whatever button he presses, it's likely ignored anyway, unless it happens to be pressed just at the start of loop().

> [@PaulRB](#):
>
> Maybe all 3 buttons read HIGH.

Of course they do. They're pulled up.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [October 1, 2024, 7:55am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/13 "2024-10-01T07:55:25Z")

</div>

Ah, yes, @rsmls you are of course correct.

@Holzwurm56 what if you press and keep on holding one of your buttons? Does the period change then? Probably only if you press and hold the button on pin 2. If you press and hold the buttons on pins 1 and 2 at the same time, you may get a different period. If you press and hold all 3 buttons, then I suspect your LEDs will appear to glow dimly at the same time. Have you understood what is wrong yet?

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [October 1, 2024, 10:52am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/14 "2024-10-01T10:52:23Z")

</div>

> [@rsmls](#):
>
> > [@PaulRB](#):
> >
> > Maybe all 3 buttons read HIGH.
> 
> Of course they do. They're pulled up.

I must be missing something, or perhaps post #1 has been edited after all the comments about the buttons being pulled up / default high.

> [@Holzwurm56](#):
>
> ```arduino
> void setup() {
> pinMode(0, INPUT_PULLDOWN);
> pinMode(1, INPUT_PULLDOWN);
> pinMode(2, INPUT_PULLDOWN);
> 
> ```

That code suggests to me that the default state will be low. I assume the buttons are connected between the GPIO pin and Vcc, so they default to low and go high when a button is pressed.

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [October 1, 2024, 11:35am UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/15 "2024-10-01T11:35:56Z")

</div>

> [@Dave\_Lowther](#):
>
> I must be missing something, or perhaps post #1 has been edited after all the comments about the buttons being pulled up / default high.

_Yes, it was edited. The original contained the lines I quoted in #7._

_@Holzwurm56 as you can see, editing your post once people have already responded to it brings the risk of confusion. It's best to avoid this. If you want to post updated code, just reply to your own thread and insert the updated code._  
Edit: disregard the bit in Italics above and please refer to @PaulRB's post below (#16)!

> [@Dave\_Lowther](#):
>
> That code suggests to me that the default state will be low.

It's also code that won't do anything in particular on many Arduino boards since pulldown resistors are not present inside many microcontrollers (notably the ATMega series). On the ESP32, most GPIO's have pulldown resistors, but not all.  
@Holzwurm56 please take note of this. Also, the approach using pulldowns is something I'd not recommend. As said repeatedly before, INPUT\_PULLUP is fine and just connect each button between the GPIO and GND. Switching over to INPUT\_PULLDOWN is not going to fix the structural problems with your code, which remain the same in your updated post.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [October 1, 2024, 12:13pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/16 "2024-10-01T12:13:00Z")

</div>

> [@rsmls](#):
>
> Yes, it was edited. The original contained the lines I quoted in #7.

It doesn't seem to have been edited, although it is possible to edit a post within a couple of minutes of posting it without the forum indicating that an edit was done. The code you quoted in post #7 matches the original post.

I think we all missed that INPUT\_PULLDOWN was used, until @Dave_Lowther spotted it.

@Holzwurm56 we need to see your schematic!

---

<div class="post-metadata">

**Author:** ![rsmls](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@rsmls](https://forum.arduino.cc/u/rsmls)\
**Post date:** [October 1, 2024, 12:20pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/17 "2024-10-01T12:20:11Z")

</div>

> [@PaulRB](#):
>
> I think we all missed that INPUT\_PULLDOWN was used.

I'm sorry, you're right. I even quoted it that way. Not sure how I could have missed it; thanks for pointing it out!

---

<div class="post-metadata">

**Author:** ![Holzwurm56](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holzwurm56/32/1013044_2.png) [@Holzwurm56](https://forum.arduino.cc/u/Holzwurm56)\
**Post date:** [October 1, 2024, 2:44pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/18 "2024-10-01T14:44:42Z")

</div>

I think I have found the error. INPUT\_PULLDOWN does not work for every pin or not at all. I have now installed a pulldown resistor and it works. Thank you for any answer

---

<div class="post-metadata">

**Author:** ![nodemaker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nodemaker/32/1108581_2.png) [@nodemaker](https://forum.arduino.cc/u/nodemaker)\
**Post date:** [October 1, 2024, 3:42pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/19 "2024-10-01T15:42:18Z")

</div>

pull-up resistors and check LOW instead of HIGH? Buttons connecting input to GND.

---

<div class="post-metadata">

**Author:** ![Holzwurm56](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/holzwurm56/32/1013044_2.png) [@Holzwurm56](https://forum.arduino.cc/u/Holzwurm56)\
**Post date:** [October 3, 2024, 2:39pm UTC](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754/20 "2024-10-03T14:39:07Z")

</div>

I have now a small test pcb with inputs connected to 3,3V. When i make new pcb, i connect the inputs to ground.

[Next page](https://forum.arduino.cc/t/can-someone-help-me-with-this-code/1306754.md?page=2)
